-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* easy to use best effort toInstant --------- Co-authored-by: Morten Haraldsen <[email protected]>
- Loading branch information
Showing
5 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.ethlo.time.internal; | ||
|
||
/** | ||
* CREDIT: <a href="https://howardhinnant.github.io/date_algorithms.html">Public domain math for converting between epoch and date-time</a> | ||
*/ | ||
public class DateTimeMath | ||
{ | ||
public static long daysFromCivil(int y, final int m, final int d) | ||
{ | ||
// Returns number of days since civil 1970-01-01. Negative values indicate | ||
// days prior to 1970-01-01. | ||
// Preconditions: y-m-d represents a date in the civil (Gregorian) calendar | ||
// m is in [1, 12] | ||
// d is in [1, last_day_of_month(y, m)] | ||
// y is "approximately" in | ||
// [numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366] | ||
// Exact range of validity is: | ||
// [civil_from_days(numeric_limits<Int>::min()), | ||
// civil_from_days(numeric_limits<Int>::max()-719468)] | ||
y -= m <= 2 ? 1 : 0; | ||
final long era = (y >= 0 ? y : y - 399) / 400; | ||
final long yoe = y - era * 400; // [0, 399] | ||
final long doy = (153L * (m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1; // [0, 365] | ||
final long doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096] | ||
return era * 146097 + doe - 719468; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.ethlo.time; | ||
|
||
/*- | ||
* #%L | ||
* Internet Time Utility | ||
* %% | ||
* Copyright (C) 2017 Morten Haraldsen (ethlo) | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class LenientParseTests | ||
{ | ||
@Test | ||
public void testParseTimestampAndUseTemporalAccessor() | ||
{ | ||
final String s = "2018-11-01T14:45:59.123456789+04:00"; | ||
final DateTime a = ITU.parseLenient(s); | ||
assertThat(a.getFractionDigits()).isEqualTo(9); | ||
assertThat(Instant.from(a)).isEqualTo(OffsetDateTime.parse(s).toInstant()); | ||
} | ||
|
||
@Test | ||
public void testParseTimestamp() | ||
{ | ||
final String s = "2018-11-01T14:45:59.12356789+04:00"; | ||
final DateTime a = ITU.parseLenient(s); | ||
assertThat(a.toInstant()).isEqualTo(OffsetDateTime.parse(s).toInstant()); | ||
} | ||
|
||
@Test | ||
public void testParseYearMonth() | ||
{ | ||
final String s = "2018-11"; | ||
final DateTime a = ITU.parseLenient(s); | ||
assertThat(a.toInstant()).isEqualTo(OffsetDateTime.parse(s + "-01T00:00:00Z").toInstant()); | ||
} | ||
|
||
@Test | ||
public void testParseYearDayMonth() | ||
{ | ||
final String s = "1997-11-30"; | ||
final DateTime a = ITU.parseLenient(s); | ||
assertThat(a.toInstant()).isEqualTo(OffsetDateTime.parse(s + "T00:00:00Z").toInstant()); | ||
} | ||
|
||
@Test | ||
public void testParseYearMonthHourMinutes() | ||
{ | ||
final String s = "2018-11-27T12:30"; | ||
final DateTime a = ITU.parseLenient(s); | ||
assertThat(a.toInstant()).isEqualTo(OffsetDateTime.parse(s + ":00Z").toInstant()); | ||
} | ||
} |