|
1 |
/*
|
|
2 |
** Module : DatetimetzNowTest.java
|
|
3 |
** Abstract : Tests for NOW and the clock functions that share its code.
|
|
4 |
**
|
|
5 |
** Copyright (c) 2026, Golden Code Development Corporation.
|
|
6 |
**
|
|
7 |
** -#- -I- --Date-- ---------------------------------Description---------------------------------
|
|
8 |
** 001 SP 20261110 Created initial version.
|
|
9 |
*/
|
|
10 |
|
|
11 |
/*
|
|
12 |
** This program is free software: you can redistribute it and/or modify
|
|
13 |
** it under the terms of the GNU Affero General Public License as
|
|
14 |
** published by the Free Software Foundation, either version 3 of the
|
|
15 |
** License, or (at your option) any later version.
|
|
16 |
**
|
|
17 |
** This program is distributed in the hope that it will be useful,
|
|
18 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 |
** GNU Affero General Public License for more details.
|
|
21 |
**
|
|
22 |
** You may find a copy of the GNU Affero GPL version 3 at the following
|
|
23 |
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
|
|
24 |
**
|
|
25 |
** Additional terms under GNU Affero GPL version 3 section 7:
|
|
26 |
**
|
|
27 |
** Under Section 7 of the GNU Affero GPL version 3, the following additional
|
|
28 |
** terms apply to the works covered under the License. These additional terms
|
|
29 |
** are non-permissive additional terms allowed under Section 7 of the GNU
|
|
30 |
** Affero GPL version 3 and may not be removed by you.
|
|
31 |
**
|
|
32 |
** 0. Attribution Requirement.
|
|
33 |
**
|
|
34 |
** You must preserve all legal notices or author attributions in the covered
|
|
35 |
** work or Appropriate Legal Notices displayed by works containing the covered
|
|
36 |
** work. You may not remove from the covered work any author or developer
|
|
37 |
** credit already included within the covered work.
|
|
38 |
**
|
|
39 |
** 1. No License To Use Trademarks.
|
|
40 |
**
|
|
41 |
** This license does not grant any license or rights to use the trademarks
|
|
42 |
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
|
|
43 |
** of Golden Code Development Corporation. You are not authorized to use the
|
|
44 |
** name Golden Code, FWD, or the names of any author or contributor, for
|
|
45 |
** publicity purposes without written authorization.
|
|
46 |
**
|
|
47 |
** 2. No Misrepresentation of Affiliation.
|
|
48 |
**
|
|
49 |
** You may not represent yourself as Golden Code Development Corporation or FWD.
|
|
50 |
**
|
|
51 |
** You may not represent yourself for publicity purposes as associated with
|
|
52 |
** Golden Code Development Corporation, FWD, or any author or contributor to
|
|
53 |
** the covered work, without written authorization.
|
|
54 |
**
|
|
55 |
** 3. No Misrepresentation of Source or Origin.
|
|
56 |
**
|
|
57 |
** You may not represent the covered work as solely your work. All modified
|
|
58 |
** versions of the covered work must be marked in a reasonable way to make it
|
|
59 |
** clear that the modified work is not originating from Golden Code Development
|
|
60 |
** Corporation or FWD. All modified versions must contain the notices of
|
|
61 |
** attribution required in this license.
|
|
62 |
*/
|
|
63 |
|
|
64 |
package com.goldencode.p2j.util;
|
|
65 |
|
|
66 |
import static org.junit.jupiter.api.Assertions.*;
|
|
67 |
|
|
68 |
import java.util.*;
|
|
69 |
|
|
70 |
import org.junit.jupiter.api.*;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Tests for {@link datetimetz#now()} and the clock functions that share its code: {@link datetime#now()}
|
|
74 |
* ({@code INIT NOW} of a DATETIME, NOW compared with a DATETIME),
|
|
75 |
* {@link datetime#millisecondsSinceMidnight()} (MTIME), {@link date#today()},
|
|
76 |
* {@link date#secondsSinceMidnight()} (TIME) and {@link date#getDefaultTimeZoneOffset()} (TIMEZONE).
|
|
77 |
* <p>
|
|
78 |
* The expected values follow the JDK's rules for the JVM default time zone. OpenEdge uses the machine's
|
|
79 |
* zone the same way; the {@code SESSION:TIMEZONE} cases follow the OpenEdge results of {@code dtz_now.p}.
|
|
80 |
*/
|
|
81 |
public class DatetimetzNowTest
|
|
82 |
{
|
|
83 |
/** Milliseconds per minute. */
|
|
84 |
private static final long MS_PER_MINUTE = 60_000L;
|
|
85 |
|
|
86 |
/** Clock slack allowed between the bounds a test reads and the value under test. */
|
|
87 |
private static final long SLACK_MS = 50L;
|
|
88 |
|
|
89 |
/**
|
|
90 |
* JVM default zones the tests run under: whole-hour, half-hour, 45-minute and +/-14:00 offsets, zones
|
|
91 |
* with DST, and the zones whose DST data makes raw offset plus DST savings differ from the real offset.
|
|
92 |
*/
|
|
93 |
private static final String[] ZONES =
|
|
94 |
{
|
|
95 |
"UTC", "Europe/Bucharest", "America/New_York", "America/St_Johns", "Asia/Kolkata", "Asia/Kathmandu",
|
|
96 |
"Australia/Lord_Howe", "Pacific/Chatham", "Pacific/Kiritimati", "Pacific/Pago_Pago",
|
|
97 |
"America/Vancouver", "Africa/Casablanca", "Africa/El_Aaiun", "Africa/Windhoek"
|
|
98 |
};
|
|
99 |
|
|
100 |
/** {@code SESSION:TIMEZONE} values checked against OpenEdge by {@code dtz_now.p}. */
|
|
101 |
private static final int[] SESSION_OFFSETS = { 60, -300, 330, 345, 765, 840, -840, -570, -60 };
|
|
102 |
|
|
103 |
/** The default time zone at the start of each test. */
|
|
104 |
private TimeZone savedZone;
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Saves the default time zone and clears {@code SESSION:TIMEZONE}.
|
|
108 |
*/
|
|
109 |
@BeforeEach
|
|
110 |
public void saveZone()
|
|
111 |
{
|
|
112 |
savedZone = TimeZone.getDefault();
|
|
113 |
date.setTimeZone(new integer());
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Restores the default time zone and clears {@code SESSION:TIMEZONE}.
|
|
118 |
*/
|
|
119 |
@AfterEach
|
|
120 |
public void restoreZone()
|
|
121 |
{
|
|
122 |
TimeZone.setDefault(savedZone);
|
|
123 |
date.setTimeZone(new integer());
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* With {@code SESSION:TIMEZONE} unset, NOW is the current instant and carries the JVM zone's offset at
|
|
128 |
* that instant.
|
|
129 |
*/
|
|
130 |
@Test
|
|
131 |
public void nowIsCurrentInstantWithJvmZoneOffset()
|
|
132 |
{
|
|
133 |
List<String> failures = new ArrayList<>();
|
|
134 |
for (String id : ZONES)
|
|
135 |
{
|
|
136 |
TimeZone zone = TimeZone.getTimeZone(id);
|
|
137 |
TimeZone.setDefault(zone);
|
|
138 |
|
|
139 |
long before = System.currentTimeMillis();
|
|
140 |
datetimetz now = datetimetz.now();
|
|
141 |
long after = System.currentTimeMillis();
|
|
142 |
|
|
143 |
long instant = instantOf(now);
|
|
144 |
int expectedOffset = zone.getOffset(instant) / (int) MS_PER_MINUTE;
|
|
145 |
if (instant < before - SLACK_MS || instant > after + SLACK_MS || now.getOffset() != expectedOffset)
|
|
146 |
{
|
|
147 |
failures.add(id + ": instant off by " + (instant - before) + " ms, offset " + now.getOffset() +
|
|
148 |
" instead of " + expectedOffset);
|
|
149 |
}
|
|
150 |
}
|
|
151 |
assertEquals(Collections.emptyList(), failures);
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* With {@code SESSION:TIMEZONE} unset, TIMEZONE is the offset NOW carries.
|
|
156 |
*/
|
|
157 |
@Test
|
|
158 |
public void timezoneIsOffsetOfNow()
|
|
159 |
{
|
|
160 |
List<String> failures = new ArrayList<>();
|
|
161 |
for (String id : ZONES)
|
|
162 |
{
|
|
163 |
TimeZone.setDefault(TimeZone.getTimeZone(id));
|
|
164 |
int timezone = date.getDefaultTimeZoneOffset().intValue();
|
|
165 |
int nowOffset = datetimetz.now().getOffset();
|
|
166 |
if (timezone != nowOffset)
|
|
167 |
{
|
|
168 |
failures.add(id + ": TIMEZONE " + timezone + ", TIMEZONE(NOW) " + nowOffset);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
assertEquals(Collections.emptyList(), failures);
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* With {@code SESSION:TIMEZONE} unset, the DATETIME form of NOW (MTIME, {@code INIT NOW}) and TODAY give
|
|
176 |
* the JVM zone's wall clock.
|
|
177 |
*/
|
|
178 |
@Test
|
|
179 |
public void datetimeNowAndTodayAreJvmZoneWallClock()
|
|
180 |
{
|
|
181 |
List<String> failures = new ArrayList<>();
|
|
182 |
for (String id : ZONES)
|
|
183 |
{
|
|
184 |
TimeZone zone = TimeZone.getTimeZone(id);
|
|
185 |
TimeZone.setDefault(zone);
|
|
186 |
|
|
187 |
long before = wallClock(zone, System.currentTimeMillis());
|
|
188 |
datetime now = datetime.now();
|
|
189 |
date today = date.today();
|
|
190 |
long after = wallClock(zone, System.currentTimeMillis());
|
|
191 |
|
|
192 |
long wall = localMillisOf(now);
|
|
193 |
if (wall < before - SLACK_MS || wall > after + SLACK_MS)
|
|
194 |
{
|
|
195 |
failures.add(id + ": datetime.now() off by " + (wall - before) + " ms");
|
|
196 |
}
|
|
197 |
if (today.longValue() != dayNumber(before) && today.longValue() != dayNumber(after))
|
|
198 |
{
|
|
199 |
failures.add(id + ": TODAY " + today.longValue() + " instead of " + dayNumber(before));
|
|
200 |
}
|
|
201 |
}
|
|
202 |
assertEquals(Collections.emptyList(), failures);
|
|
203 |
}
|
|
204 |
|
|
205 |
/**
|
|
206 |
* With {@code SESSION:TIMEZONE} unset, TIME and MTIME are the seconds and milliseconds of the JVM zone's
|
|
207 |
* wall clock.
|
|
208 |
*/
|
|
209 |
@Test
|
|
210 |
public void timeAndMtimeAreJvmZoneWallClock()
|
|
211 |
{
|
|
212 |
List<String> failures = new ArrayList<>();
|
|
213 |
for (String id : ZONES)
|
|
214 |
{
|
|
215 |
TimeZone zone = TimeZone.getTimeZone(id);
|
|
216 |
TimeZone.setDefault(zone);
|
|
217 |
|
|
218 |
long before = Math.floorMod(wallClock(zone, System.currentTimeMillis()), date.MILLIS_PER_DAY);
|
|
219 |
long mtime = datetime.millisecondsSinceMidnight().longValue();
|
|
220 |
long time = date.secondsSinceMidnight().longValue();
|
|
221 |
long after = Math.floorMod(wallClock(zone, System.currentTimeMillis()), date.MILLIS_PER_DAY);
|
|
222 |
|
|
223 |
if (todDistance(mtime, before) > SLACK_MS && todDistance(mtime, after) > SLACK_MS)
|
|
224 |
{
|
|
225 |
failures.add(id + ": MTIME off by " + (mtime - before) + " ms");
|
|
226 |
}
|
|
227 |
if (todDistance(time * 1000, before) > 1000 + SLACK_MS &&
|
|
228 |
todDistance(time * 1000, after) > 1000 + SLACK_MS)
|
|
229 |
{
|
|
230 |
failures.add(id + ": TIME off by " + (time * 1000 - before) + " ms");
|
|
231 |
}
|
|
232 |
}
|
|
233 |
assertEquals(Collections.emptyList(), failures);
|
|
234 |
}
|
|
235 |
|
|
236 |
/**
|
|
237 |
* With {@code SESSION:TIMEZONE} set, NOW is the current instant, carries the session offset, and
|
|
238 |
* TIMEZONE returns it. OpenEdge does this for every offset {@code dtz_now.p} tries.
|
|
239 |
*/
|
|
240 |
@Test
|
|
241 |
public void nowCarriesSessionOffset()
|
|
242 |
{
|
|
243 |
List<String> failures = new ArrayList<>();
|
|
244 |
for (String id : new String[] { "Europe/Bucharest", "Africa/Casablanca" })
|
|
245 |
{
|
|
246 |
TimeZone.setDefault(TimeZone.getTimeZone(id));
|
|
247 |
for (int offset : SESSION_OFFSETS)
|
|
248 |
{
|
|
249 |
date.setTimeZone(new integer(offset));
|
|
250 |
|
|
251 |
long before = System.currentTimeMillis();
|
|
252 |
datetimetz now = datetimetz.now();
|
|
253 |
long after = System.currentTimeMillis();
|
|
254 |
|
|
255 |
long instant = instantOf(now);
|
|
256 |
int timezone = date.getDefaultTimeZoneOffset().intValue();
|
|
257 |
if (instant < before - SLACK_MS || instant > after + SLACK_MS ||
|
|
258 |
now.getOffset() != offset ||
|
|
259 |
timezone != offset)
|
|
260 |
{
|
|
261 |
failures.add(id + " session " + offset + ": instant off by " + (instant - before) +
|
|
262 |
" ms, TIMEZONE(NOW) " + now.getOffset() + ", TIMEZONE " + timezone);
|
|
263 |
}
|
|
264 |
}
|
|
265 |
}
|
|
266 |
assertEquals(Collections.emptyList(), failures);
|
|
267 |
}
|
|
268 |
|
|
269 |
/**
|
|
270 |
* With {@code SESSION:TIMEZONE} set, the DATETIME form of NOW (MTIME, {@code INIT NOW}, NOW compared with
|
|
271 |
* a DATETIME), MTIME and TODAY follow the session zone's wall clock, as in OpenEdge.
|
|
272 |
*/
|
|
273 |
@Test
|
|
274 |
public void datetimeNowMtimeAndTodayFollowSessionZone()
|
|
275 |
{
|
|
276 |
List<String> failures = new ArrayList<>();
|
|
277 |
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Bucharest"));
|
|
278 |
for (int offset : SESSION_OFFSETS)
|
|
279 |
{
|
|
280 |
date.setTimeZone(new integer(offset));
|
|
281 |
|
|
282 |
long before = System.currentTimeMillis() + offset * MS_PER_MINUTE;
|
|
283 |
datetime now = datetime.now();
|
|
284 |
long mtime = datetime.millisecondsSinceMidnight().longValue();
|
|
285 |
date today = date.today();
|
|
286 |
long after = System.currentTimeMillis() + offset * MS_PER_MINUTE;
|
|
287 |
|
|
288 |
long wall = localMillisOf(now);
|
|
289 |
if (wall < before - SLACK_MS || wall > after + SLACK_MS)
|
|
290 |
{
|
|
291 |
failures.add("session " + offset + ": datetime.now() off by " + (wall - before) + " ms");
|
|
292 |
}
|
|
293 |
if (todDistance(mtime, Math.floorMod(before, date.MILLIS_PER_DAY)) > SLACK_MS &&
|
|
294 |
todDistance(mtime, Math.floorMod(after, date.MILLIS_PER_DAY)) > SLACK_MS)
|
|
295 |
{
|
|
296 |
failures.add("session " + offset + ": MTIME off by " +
|
|
297 |
(mtime - Math.floorMod(before, date.MILLIS_PER_DAY)) + " ms");
|
|
298 |
}
|
|
299 |
if (today.longValue() != dayNumber(before) && today.longValue() != dayNumber(after))
|
|
300 |
{
|
|
301 |
failures.add("session " + offset + ": TODAY " + today.longValue() + " instead of " +
|
|
302 |
dayNumber(before));
|
|
303 |
}
|
|
304 |
}
|
|
305 |
assertEquals(Collections.emptyList(), failures);
|
|
306 |
}
|
|
307 |
|
|
308 |
/**
|
|
309 |
* With {@code SESSION:TIMEZONE} set, TIME follows the session zone's wall clock, as in OpenEdge.
|
|
310 |
*/
|
|
311 |
@Test
|
|
312 |
public void timeFollowsSessionZone()
|
|
313 |
{
|
|
314 |
List<String> failures = new ArrayList<>();
|
|
315 |
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Bucharest"));
|
|
316 |
for (int offset : SESSION_OFFSETS)
|
|
317 |
{
|
|
318 |
date.setTimeZone(new integer(offset));
|
|
319 |
|
|
320 |
long shift = offset * MS_PER_MINUTE;
|
|
321 |
long before = Math.floorMod(System.currentTimeMillis() + shift, date.MILLIS_PER_DAY);
|
|
322 |
long time = date.secondsSinceMidnight().longValue();
|
|
323 |
long after = Math.floorMod(System.currentTimeMillis() + shift, date.MILLIS_PER_DAY);
|
|
324 |
|
|
325 |
if (todDistance(time * 1000, before) > 1000 + SLACK_MS &&
|
|
326 |
todDistance(time * 1000, after) > 1000 + SLACK_MS)
|
|
327 |
{
|
|
328 |
failures.add("session " + offset + ": TIME off by " + (time * 1000 - before) + " ms");
|
|
329 |
}
|
|
330 |
}
|
|
331 |
assertEquals(Collections.emptyList(), failures);
|
|
332 |
}
|
|
333 |
|
|
334 |
/**
|
|
335 |
* With {@code SESSION:TIMEZONE} set, assigning NOW to a DATETIME keeps the instant and gives the wall
|
|
336 |
* clock of the machine zone, not of the session zone, as in OpenEdge.
|
|
337 |
*/
|
|
338 |
@Test
|
|
339 |
public void datetimeAssignedFromNowUsesMachineZone()
|
|
340 |
{
|
|
341 |
List<String> failures = new ArrayList<>();
|
|
342 |
TimeZone zone = TimeZone.getTimeZone("Europe/Bucharest");
|
|
343 |
TimeZone.setDefault(zone);
|
|
344 |
for (int offset : SESSION_OFFSETS)
|
|
345 |
{
|
|
346 |
date.setTimeZone(new integer(offset));
|
|
347 |
|
|
348 |
long before = wallClock(zone, System.currentTimeMillis());
|
|
349 |
datetime assigned = new datetime();
|
|
350 |
assigned.assign(datetimetz.now());
|
|
351 |
long after = wallClock(zone, System.currentTimeMillis());
|
|
352 |
|
|
353 |
long wall = localMillisOf(assigned);
|
|
354 |
if (wall < before - SLACK_MS || wall > after + SLACK_MS)
|
|
355 |
{
|
|
356 |
failures.add("session " + offset + ": DATETIME = NOW off by " + (wall - before) + " ms");
|
|
357 |
}
|
|
358 |
}
|
|
359 |
assertEquals(Collections.emptyList(), failures);
|
|
360 |
}
|
|
361 |
|
|
362 |
/**
|
|
363 |
* NOW for a given instant takes its offset, day and time from that instant: across the DST changes of
|
|
364 |
* 2026 (Europe/Bucharest whole hour, Australia/Lord_Howe 30 minutes), across midnight (2025-11-10), and
|
|
365 |
* with SESSION:TIMEZONE set to -840 and 345.
|
|
366 |
*/
|
|
367 |
@Test
|
|
368 |
public void nowAtUsesOneInstant()
|
|
369 |
{
|
|
370 |
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Bucharest"));
|
|
371 |
assertEquals("2461130/10799999/120", parts(datetimetz.nowAt(1774745999999L)));
|
|
372 |
assertEquals("2461130/14400000/180", parts(datetimetz.nowAt(1774746000000L)));
|
|
373 |
assertEquals("2461340/14399999/180", parts(datetimetz.nowAt(1792889999999L)));
|
|
374 |
assertEquals("2461340/10800000/120", parts(datetimetz.nowAt(1792890000000L)));
|
|
375 |
assertEquals("2460991/86399999/120", parts(datetimetz.nowAt(1762811999999L)));
|
|
376 |
assertEquals("2460992/0/120", parts(datetimetz.nowAt(1762812000000L)));
|
|
377 |
assertEquals("2461340/10800000", parts(datetime.nowAt(1792890000000L)));
|
|
378 |
|
|
379 |
TimeZone.setDefault(TimeZone.getTimeZone("Australia/Lord_Howe"));
|
|
380 |
assertEquals("2461137/7199999/660", parts(datetimetz.nowAt(1775314799999L)));
|
|
381 |
assertEquals("2461137/5400000/630", parts(datetimetz.nowAt(1775314800000L)));
|
|
382 |
assertEquals("2461319/7199999/630", parts(datetimetz.nowAt(1791041399999L)));
|
|
383 |
assertEquals("2461319/9000000/660", parts(datetimetz.nowAt(1791041400000L)));
|
|
384 |
|
|
385 |
date.setTimeZone(new integer(-840));
|
|
386 |
assertEquals("2460991/28800000/-840", parts(datetimetz.nowAt(1762812000000L)));
|
|
387 |
assertEquals("2460991/28800000", parts(datetime.nowAt(1762812000000L)));
|
|
388 |
date.setTimeZone(new integer(345));
|
|
389 |
assertEquals("2460992/13500000/345", parts(datetimetz.nowAt(1762812000000L)));
|
|
390 |
}
|
|
391 |
|
|
392 |
/**
|
|
393 |
* NOW for a fixed instant (2025-11-10T22:00:00Z) carries the real offset of zones whose raw offset plus
|
|
394 |
* DST savings differs from it, and the wall clock of that offset; TIMEZONE agrees with it.
|
|
395 |
*/
|
|
396 |
@Test
|
|
397 |
public void nowAtUsesRealOffsetInIrregularDstZones()
|
|
398 |
{
|
|
399 |
long utc = 1762812000000L;
|
|
400 |
TimeZone.setDefault(TimeZone.getTimeZone("Africa/Casablanca"));
|
|
401 |
assertEquals("2460991/82800000/60", parts(datetimetz.nowAt(utc)));
|
|
402 |
assertEquals("2460991/82800000", parts(datetime.nowAt(utc)));
|
|
403 |
assertEquals(60, date.currentOffset(utc));
|
|
404 |
|
|
405 |
TimeZone.setDefault(TimeZone.getTimeZone("Africa/El_Aaiun"));
|
|
406 |
assertEquals("2460991/82800000/60", parts(datetimetz.nowAt(utc)));
|
|
407 |
|
|
408 |
TimeZone.setDefault(TimeZone.getTimeZone("Africa/Windhoek"));
|
|
409 |
assertEquals("2460992/0/120", parts(datetimetz.nowAt(utc)));
|
|
410 |
assertEquals("2460992/0", parts(datetime.nowAt(utc)));
|
|
411 |
assertEquals(120, date.currentOffset(utc));
|
|
412 |
}
|
|
413 |
|
|
414 |
/**
|
|
415 |
* Gets the day number, time and, for a datetime-tz, offset of a value.
|
|
416 |
*
|
|
417 |
* @param value
|
|
418 |
* The value.
|
|
419 |
*
|
|
420 |
* @return {@code day/time} or {@code day/time/offset}.
|
|
421 |
*/
|
|
422 |
private static String parts(datetime value)
|
|
423 |
{
|
|
424 |
String text = value.longValue() + "/" + value.getTime();
|
|
425 |
return value instanceof datetimetz ? text + "/" + ((datetimetz) value).getOffset() : text;
|
|
426 |
}
|
|
427 |
|
|
428 |
/**
|
|
429 |
* Gets the instant a datetime-tz value stands for.
|
|
430 |
*
|
|
431 |
* @param value
|
|
432 |
* The value.
|
|
433 |
*
|
|
434 |
* @return Milliseconds since the Java epoch, UTC.
|
|
435 |
*/
|
|
436 |
private static long instantOf(datetimetz value)
|
|
437 |
{
|
|
438 |
return localMillisOf(value) - value.getOffset() * MS_PER_MINUTE;
|
|
439 |
}
|
|
440 |
|
|
441 |
/**
|
|
442 |
* Gets the wall clock a datetime value holds, as milliseconds since the Java epoch in its own zone.
|
|
443 |
*
|
|
444 |
* @param value
|
|
445 |
* The value.
|
|
446 |
*
|
|
447 |
* @return The day number and time of day of the value, in milliseconds.
|
|
448 |
*/
|
|
449 |
private static long localMillisOf(datetime value)
|
|
450 |
{
|
|
451 |
return (value.longValue() - date.JULIAN_EPOCH_OFFSET) * date.MILLIS_PER_DAY + value.getTime();
|
|
452 |
}
|
|
453 |
|
|
454 |
/**
|
|
455 |
* Gets the wall clock of a zone at an instant, by the JDK's zone rules.
|
|
456 |
*
|
|
457 |
* @param zone
|
|
458 |
* The zone.
|
|
459 |
* @param utc
|
|
460 |
* The instant, in milliseconds since the Java epoch.
|
|
461 |
*
|
|
462 |
* @return The wall clock, in milliseconds since the Java epoch in that zone.
|
|
463 |
*/
|
|
464 |
private static long wallClock(TimeZone zone, long utc)
|
|
465 |
{
|
|
466 |
return utc + zone.getOffset(utc);
|
|
467 |
}
|
|
468 |
|
|
469 |
/**
|
|
470 |
* Gets the 4GL day number of a wall clock.
|
|
471 |
*
|
|
472 |
* @param local
|
|
473 |
* The wall clock, in milliseconds since the Java epoch.
|
|
474 |
*
|
|
475 |
* @return The day number.
|
|
476 |
*/
|
|
477 |
private static long dayNumber(long local)
|
|
478 |
{
|
|
479 |
return Math.floorDiv(local, date.MILLIS_PER_DAY) + date.JULIAN_EPOCH_OFFSET;
|
|
480 |
}
|
|
481 |
|
|
482 |
/**
|
|
483 |
* Gets the distance between two times of day, across midnight.
|
|
484 |
*
|
|
485 |
* @param a
|
|
486 |
* Milliseconds of the day.
|
|
487 |
* @param b
|
|
488 |
* Milliseconds of the day.
|
|
489 |
*
|
|
490 |
* @return The distance in milliseconds, at most half a day.
|
|
491 |
*/
|
|
492 |
private static long todDistance(long a, long b)
|
|
493 |
{
|
|
494 |
long d = Math.floorMod(a - b, date.MILLIS_PER_DAY);
|
|
495 |
return Math.min(d, date.MILLIS_PER_DAY - d);
|
|
496 |
}
|
|
497 |
}
|