-
Notifications
You must be signed in to change notification settings - Fork 13
/
TimePeriodsA.java
114 lines (107 loc) · 4.7 KB
/
TimePeriodsA.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.cj.interview.preparation;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
public class TimePeriodsA implements TimePeriods {
final int TIME_PERIOD_LAST_7_DAYS = 0;
final int TIME_PERIOD_LAST_30_DAYS = 1;
final int TIME_PERIOD_LAST_QUARTER = 2;
final int TIME_PERIOD_LAST_YEAR = 3;
final int TIME_PERIOD_YEAR_TO_DATE = 4;
final int TIME_PERIOD_QUARTER_TO_DATE = 5;
final int TIME_PERIOD_COUNT = 6;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
@Override
public List<String> debugTimePeriods(Instant now, ZoneId zoneId) {
List<String> lines = new ArrayList<>();
for (int i = 0; i < TIME_PERIOD_COUNT; i++) {
ZonedDateTime start = timePeriodStart(i, now, zoneId);
ZonedDateTime end = timePeriodEnd(i, now, zoneId);
String formattedStart = dateTimeFormatter.format(start);
String formattedEnd = dateTimeFormatter.format(end);
String apiParameters = String.format("start=%s&end=%s", formattedStart, formattedEnd);
String timePeriodName = timePeriodDisplayName(i);
String debugLine = String.format("%-15s -> %s", timePeriodName, apiParameters);
lines.add(debugLine);
}
return lines;
}
public String timePeriodDisplayName(int index) {
switch (index) {
case TIME_PERIOD_LAST_7_DAYS:
return "Last 7 Days";
case TIME_PERIOD_LAST_30_DAYS:
return "Last 30 Days";
case TIME_PERIOD_LAST_QUARTER:
return "Last Quarter";
case TIME_PERIOD_LAST_YEAR:
return "Last Year";
case TIME_PERIOD_YEAR_TO_DATE:
return "Year to Date";
case TIME_PERIOD_QUARTER_TO_DATE:
return "Quarter to Date";
default:
String message = String.format("Invalid index %d for time period", index);
throw new RuntimeException(message);
}
}
public ZonedDateTime timePeriodStart(int index, Instant now, ZoneId zoneId) {
ZonedDateTime today = truncateToDay(now, zoneId);
switch (index) {
case TIME_PERIOD_LAST_7_DAYS:
return today.minusDays(7);
case TIME_PERIOD_LAST_30_DAYS:
return today.minusDays(30);
case TIME_PERIOD_LAST_QUARTER: {
int month = today.getMonthValue();
int quarter = (month + 2) / 3;
int monthAtBeginningOfQuarter = quarter * 3 - 2;
ZonedDateTime endQuarter = today.withDayOfMonth(1).withMonth(monthAtBeginningOfQuarter);
return endQuarter.minusMonths(3);
}
case TIME_PERIOD_LAST_YEAR:
int year = today.getYear();
return today.withYear(year - 1).withDayOfYear(1);
case TIME_PERIOD_YEAR_TO_DATE:
return today.withDayOfYear(1);
case TIME_PERIOD_QUARTER_TO_DATE: {
int month = today.getMonthValue();
int quarter = (month + 2) / 3;
int monthAtBeginningOfQuarter = quarter * 3 - 2;
return today.withDayOfMonth(1).withMonth(monthAtBeginningOfQuarter);
}
default:
String message = String.format("Invalid index %d for time period", index);
throw new RuntimeException(message);
}
}
public ZonedDateTime timePeriodEnd(int index, Instant now, ZoneId zoneId) {
ZonedDateTime today = truncateToDay(now, zoneId);
switch (index) {
case TIME_PERIOD_LAST_7_DAYS:
case TIME_PERIOD_LAST_30_DAYS:
case TIME_PERIOD_YEAR_TO_DATE:
case TIME_PERIOD_QUARTER_TO_DATE:
return today;
case TIME_PERIOD_LAST_QUARTER: {
int month = today.getMonthValue();
int quarter = (month + 2) / 3;
int monthAtBeginningOfQuarter = quarter * 3 - 2;
return today.withDayOfMonth(1).withMonth(monthAtBeginningOfQuarter);
}
case TIME_PERIOD_LAST_YEAR:
int year = today.getYear();
return today.withYear(year).withDayOfYear(1);
default:
String message = String.format("Invalid index %d for time period", index);
throw new RuntimeException(message);
}
}
ZonedDateTime truncateToDay(Instant now, ZoneId zoneId) {
return ZonedDateTime.ofInstant(now, zoneId).truncatedTo(ChronoUnit.DAYS);
}
}