• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-mirana / #102

01 Feb 2025 03:15PM UTC coverage: 87.085% (+0.3%) from 86.782%
#102

push

trydofor
📝 opt default hit message #49

192 of 193 new or added lines in 1 file covered. (99.48%)

1 existing line in 1 file now uncovered.

7053 of 8099 relevant lines covered (87.08%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.62
/src/main/java/pro/fessional/mirana/time/DateLocaling.java
1
package pro.fessional.mirana.time;
2

3
import java.time.DayOfWeek;
4
import java.time.Instant;
5
import java.time.LocalDateTime;
6
import java.time.ZoneId;
7
import java.time.ZonedDateTime;
8

9
/**
10
 * Convert the timezone of LocalDateTime and ZonedDateTime,
11
 * based on the System timezone to Viewer timezone by default.
12
 *
13
 * @author trydofor
14
 * @since 2019-10-16
15
 */
16
public class DateLocaling {
×
17

18
    /**
19
     * convert epoch millis at UTC
20
     */
21
    public static LocalDateTime utcLdt(long epochMilli) {
22
        return useLdt(epochMilli, ThreadNow.utcZoneId());
1✔
23
    }
24

25
    /**
26
     * convert epoch millis at System timezone
27
     */
28
    public static LocalDateTime sysLdt(long epochMilli) {
29
        return useLdt(epochMilli, ThreadNow.sysZoneId());
1✔
30
    }
31

32
    /**
33
     * convert epoch millis at zoneId
34
     */
35
    public static LocalDateTime useLdt(long epochMilli, ZoneId zone) {
36
        final Instant ins = Instant.ofEpochMilli(epochMilli);
1✔
37
        return LocalDateTime.ofInstant(ins, zone);
1✔
38
    }
39

40
    /**
41
     * get epoch millis at UTC
42
     */
43
    public static long utcEpoch(LocalDateTime ldt) {
44
        return useEpoch(ldt, ThreadNow.utcZoneId());
1✔
45
    }
46

47
    /**
48
     * get epoch millis at System timezone
49
     */
50
    public static long sysEpoch(LocalDateTime ldt) {
51
        return useEpoch(ldt, ThreadNow.sysZoneId());
1✔
52
    }
53

54
    /**
55
     * get epoch millis at zoneId
56
     */
57
    public static long useEpoch(LocalDateTime ldt, ZoneId zone) {
58
        return ZonedDateTime.of(ldt, zone).toInstant().toEpochMilli();
1✔
59
    }
60

61
    /**
62
     * current local datetime at zoneId
63
     */
64
    public static LocalDateTime dateTime(ZoneId at) {
65
        return ZonedDateTime.now(at).toLocalDateTime();
1✔
66
    }
67

68

69
    /**
70
     * current local 0:0:0.0 at zoneId
71
     */
72
    public static LocalDateTime today(ZoneId at) {
73
        return ZonedDateTime.now(at).toLocalDate().atStartOfDay();
1✔
74
    }
75

76
    /**
77
     * 1st date(0:0:0.0) of current month at zoneId
78
     */
79
    public static LocalDateTime month(ZoneId at) {
80
        return ZonedDateTime.now(at).toLocalDate().withDayOfMonth(1).atStartOfDay();
1✔
81
    }
82

83
    /**
84
     * MONDAY(0:0:0.0) of current week at zoneId
85
     */
86
    public static LocalDateTime monday(ZoneId at) {
87
        return week(at, DayOfWeek.MONDAY);
1✔
88
    }
89

90
    /**
91
     * SUNDAY(0:0:0.0) of current week at zoneId
92
     */
93
    public static LocalDateTime sunday(ZoneId at) {
94
        return week(at, DayOfWeek.SUNDAY);
1✔
95
    }
96

97
    /**
98
     * Week day(0:0:0.0) of current week at zoneId
99
     */
100
    public static LocalDateTime week(ZoneId at, DayOfWeek day) {
101
        LocalDateTime ldt = ZonedDateTime.now(at).toLocalDate().atStartOfDay();
1✔
102
        int v = ldt.getDayOfWeek().getValue(); // 0:Monday; 6:Sunday
1✔
103
        int m = day.getValue();
1✔
104
        if (m > v) {
1✔
UNCOV
105
            return ldt.plusDays((m - v - 7));
×
106
        }
107
        else if (m < v) {
1✔
108
            return ldt.plusDays((m - v));
1✔
109
        }
110
        else {
111
            return ldt;
1✔
112
        }
113
    }
114

115
    // ////////// system //////////
116

117
    /**
118
     * local datetime at System timezone
119
     */
120
    public static ZonedDateTime sysZdt(LocalDateTime ldt) {
121
        if (ldt == null) return null;
1✔
122
        return ldt.atZone(ThreadNow.sysZoneId());
1✔
123
    }
124

125
    /**
126
     * local datetime at System timezone
127
     */
128
    public static LocalDateTime sysLdt(ZonedDateTime zdt) {
129
        return local(zdt, ThreadNow.sysZoneId());
1✔
130
    }
131

132
    /**
133
     * datetime at System timezone
134
     */
135
    public static ZonedDateTime sysZdt(ZonedDateTime zdt) {
136
        return zoned(zdt, ThreadNow.sysZoneId());
1✔
137
    }
138

139
    /**
140
     * viewer local datetime at System timezone
141
     */
142
    public static LocalDateTime sysLdt(ZoneId viewer, LocalDateTime ldt) {
143
        return local(viewer, ldt, ThreadNow.sysZoneId());
1✔
144
    }
145

146
    /**
147
     * viewer datetime at System timezone
148
     */
149
    public static ZonedDateTime sysZdt(ZoneId viewer, LocalDateTime ldt) {
150
        return zoned(viewer, ldt, ThreadNow.sysZoneId());
1✔
151
    }
152

153
    // ////////// viewer //////////
154

155
    /**
156
     * local datetime at viewer timezone
157
     */
158
    public static LocalDateTime useLdt(ZonedDateTime zdt, ZoneId viewer) {
159
        return local(zdt, viewer);
1✔
160
    }
161

162
    /**
163
     * datetime at viewer timezone
164
     */
165
    public static ZonedDateTime useZdt(ZonedDateTime zdt, ZoneId viewer) {
166
        return zoned(zdt, viewer);
1✔
167
    }
168

169
    /**
170
     * system local datetime at viewer timezone
171
     */
172
    public static LocalDateTime useLdt(LocalDateTime ldt, ZoneId viewer) {
173
        return local(ThreadNow.sysZoneId(), ldt, viewer);
1✔
174
    }
175

176
    /**
177
     * system datetime at viewer timezone
178
     */
179
    public static ZonedDateTime useZdt(LocalDateTime ldt, ZoneId viewer) {
180
        return zoned(ThreadNow.sysZoneId(), ldt, viewer);
1✔
181
    }
182

183

184
    // ////////// locate //////////
185

186
    /**
187
     * convert local datetime from `at` to `to`
188
     */
189
    public static LocalDateTime local(ZoneId at, LocalDateTime ldt, ZoneId to) {
190
        if (ldt == null) return null;
1✔
191
        if (to == null || at.equals(to)) return ldt;
1✔
192
        return ldt.atZone(at).withZoneSameInstant(to).toLocalDateTime();
1✔
193
    }
194

195
    /**
196
     * convert datetime from `at` to `to`
197
     */
198
    public static ZonedDateTime zoned(ZoneId at, LocalDateTime ldt, ZoneId to) {
199
        if (ldt == null) return null;
1✔
200
        if (to == null || at.equals(to)) return ldt.atZone(at);
1✔
201
        return ldt.atZone(at).withZoneSameInstant(to);
1✔
202
    }
203

204
    /**
205
     * convert datetime to `to`
206
     */
207
    public static LocalDateTime local(ZonedDateTime zdt, ZoneId to) {
208
        if (zdt == null) return null;
1✔
209
        if (to == null) return zdt.toLocalDateTime();
1✔
210

211
        if (zdt.getZone().equals(to)) {
1✔
212
            return zdt.toLocalDateTime();
×
213
        }
214
        else {
215
            return zdt.withZoneSameInstant(to).toLocalDateTime();
1✔
216
        }
217
    }
218

219
    /**
220
     * convert datetime to `to`
221
     */
222
    public static ZonedDateTime zoned(ZonedDateTime zdt, ZoneId to) {
223
        if (zdt == null) return null;
1✔
224
        if (to == null) return zdt;
1✔
225

226
        if (zdt.getZone().equals(to)) {
1✔
227
            return zdt;
1✔
228
        }
229
        else {
230
            return zdt.withZoneSameInstant(to);
1✔
231
        }
232
    }
233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc