Skip to content

Commit a2b1197

Browse files
committed
add week command
1 parent 431b75e commit a2b1197

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/main/java/io/github/benas/unixstream/UnixStream.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.io.*;
66
import java.nio.file.*;
77
import java.time.LocalDate;
8+
import java.time.temporal.WeekFields;
89
import java.util.Comparator;
10+
import java.util.Locale;
911
import java.util.Objects;
1012
import java.util.function.Predicate;
1113
import java.util.stream.Stream;
@@ -83,6 +85,18 @@ static UnixStream<String> date() {
8385
return new UnixStreamImpl<>(Stream.of(LocalDate.now().toString()));
8486
}
8587

88+
/**
89+
* Create a new UnixStream with the current week.
90+
*
91+
* @return a new UnixStream with the current week.
92+
*/
93+
static UnixStream<String> week() {
94+
LocalDate now = LocalDate.now();
95+
WeekFields weekFields = WeekFields.of(Locale.getDefault());
96+
int weekNumber = now.get(weekFields.weekOfWeekBasedYear());
97+
return new UnixStreamImpl<>(Stream.of(String.valueOf(weekNumber)));
98+
}
99+
86100
/**
87101
* Create a new UnixStream with the given input.
88102
*
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.benas.unixstream.components;
2+
3+
import io.github.benas.unixstream.UnixStream;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
public class WeekTest {
13+
14+
@Test
15+
public void week() throws IOException {
16+
UnixStream<String> stream = UnixStream.week();
17+
18+
List<String> strings = stream.collect(Collectors.toList());
19+
assertThat(strings).hasSize(1);
20+
int week = Integer.parseInt(strings.get(0));
21+
22+
assertThat(week).isBetween(1, 52);
23+
}
24+
}

0 commit comments

Comments
 (0)