Skip to content

Commit 6a9a5a8

Browse files
author
Nadeesh TV
committed
8143413: add toEpochSecond methods for efficient access
Reviewed-by: rriggs, scolebourne
1 parent 1a8918d commit 6a9a5a8

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed

jdk/src/java.base/share/classes/java/time/LocalDate.java

+27
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ public final class LocalDate
147147
* This could be used by an application as a "far future" date.
148148
*/
149149
public static final LocalDate MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
150+
/**
151+
* The epoch year {@code LocalDate}, '1970-01-01'.
152+
*/
153+
public static final LocalDate EPOCH = LocalDate.of(1970, 1, 1);
150154

151155
/**
152156
* Serialization version.
@@ -1864,6 +1868,29 @@ public long toEpochDay() {
18641868
return total - DAYS_0000_TO_1970;
18651869
}
18661870

1871+
/**
1872+
* Converts this {@code LocalDate} to the number of seconds since the epoch
1873+
* of 1970-01-01T00:00:00Z.
1874+
* <p>
1875+
* This combines this local date with the specified time and
1876+
* offset to calculate the epoch-second value, which is the
1877+
* number of elapsed seconds from 1970-01-01T00:00:00Z.
1878+
* Instants on the time-line after the epoch are positive, earlier
1879+
* are negative.
1880+
*
1881+
* @param time the local time, not null
1882+
* @param offset the zone offset, not null
1883+
* @return the number of seconds since the epoch of 1970-01-01T00:00:00Z, may be negative
1884+
* @since 9
1885+
*/
1886+
public long toEpochSecond(LocalTime time, ZoneOffset offset) {
1887+
Objects.requireNonNull(time, "time");
1888+
Objects.requireNonNull(offset, "offset");
1889+
long secs = toEpochDay() * SECONDS_PER_DAY + time.toSecondOfDay();
1890+
secs -= offset.getTotalSeconds();
1891+
return secs;
1892+
}
1893+
18671894
//-----------------------------------------------------------------------
18681895
/**
18691896
* Compares this date to another date.

jdk/src/java.base/share/classes/java/time/LocalTime.java

+24
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,30 @@ public long toNanoOfDay() {
14901490
return total;
14911491
}
14921492

1493+
/**
1494+
* Converts this {@code LocalTime} to the number of seconds since the epoch
1495+
* of 1970-01-01T00:00:00Z.
1496+
* <p>
1497+
* This combines this local time with the specified date and
1498+
* offset to calculate the epoch-second value, which is the
1499+
* number of elapsed seconds from 1970-01-01T00:00:00Z.
1500+
* Instants on the time-line after the epoch are positive, earlier
1501+
* are negative.
1502+
*
1503+
* @param date the local date, not null
1504+
* @param offset the zone offset, not null
1505+
* @return the number of seconds since the epoch of 1970-01-01T00:00:00Z, may be negative
1506+
* @since 9
1507+
*/
1508+
public long toEpochSecond(LocalDate date, ZoneOffset offset) {
1509+
Objects.requireNonNull(date, "date");
1510+
Objects.requireNonNull(offset, "offset");
1511+
long epochDay = date.toEpochDay();
1512+
long secs = epochDay * 86400 + toSecondOfDay();
1513+
secs -= offset.getTotalSeconds();
1514+
return secs;
1515+
}
1516+
14931517
//-----------------------------------------------------------------------
14941518
/**
14951519
* Compares this time to another time.

jdk/src/java.base/share/classes/java/time/OffsetTime.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1232,6 +1232,28 @@ private long toEpochNano() {
12321232
return nod - offsetNanos;
12331233
}
12341234

1235+
/**
1236+
* Converts this {@code OffsetTime} to the number of seconds since the epoch
1237+
* of 1970-01-01T00:00:00Z.
1238+
* <p>
1239+
* This combines this offset time with the specified date to calculate the
1240+
* epoch-second value, which is the number of elapsed seconds from
1241+
* 1970-01-01T00:00:00Z.
1242+
* Instants on the time-line after the epoch are positive, earlier
1243+
* are negative.
1244+
*
1245+
* @param date the localdate, not null
1246+
* @return the number of seconds since the epoch of 1970-01-01T00:00:00Z, may be negative
1247+
* @since 9
1248+
*/
1249+
public long toEpochSecond(LocalDate date) {
1250+
Objects.requireNonNull(date, "date");
1251+
long epochDay = date.toEpochDay();
1252+
long secs = epochDay * 86400 + time.toSecondOfDay();
1253+
secs -= offset.getTotalSeconds();
1254+
return secs;
1255+
}
1256+
12351257
//-----------------------------------------------------------------------
12361258
/**
12371259
* Compares this {@code OffsetTime} to another time.

jdk/test/java/time/tck/java/time/TCKLocalDate.java

+25
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,31 @@ public void test_toEpochDay() {
21562156
assertEquals(LocalDate.of(-1, 12, 31).toEpochDay(), -678942 - 40587);
21572157
}
21582158

2159+
//-----------------------------------------------------------------------
2160+
// toEpochSecond
2161+
//-----------------------------------------------------------------------
2162+
@DataProvider(name="epochSecond")
2163+
Object[][] provider_toEpochSecond() {
2164+
return new Object[][] {
2165+
{LocalDate.of(1858, 11, 17).toEpochSecond(LocalTime.MIDNIGHT, OFFSET_PONE), -3506720400L},
2166+
{LocalDate.of(1, 1, 1).toEpochSecond(LocalTime.NOON, OFFSET_PONE), -62135557200L},
2167+
{LocalDate.of(1995, 9, 27).toEpochSecond(LocalTime.of(5, 30), OFFSET_PTWO), 812172600L},
2168+
{LocalDate.of(1970, 1, 1).toEpochSecond(LocalTime.MIDNIGHT, OFFSET_MTWO), 7200L},
2169+
{LocalDate.of(-1, 12, 31).toEpochSecond(LocalTime.NOON, OFFSET_PONE), -62167266000L},
2170+
{LocalDate.of(1, 1, 1).toEpochSecond(LocalTime.MIDNIGHT, OFFSET_PONE),
2171+
Instant.ofEpochSecond(-62135600400L).getEpochSecond()},
2172+
{LocalDate.of(1995, 9, 27).toEpochSecond(LocalTime.NOON, OFFSET_PTWO),
2173+
Instant.ofEpochSecond(812196000L).getEpochSecond()},
2174+
{LocalDate.of(1995, 9, 27).toEpochSecond(LocalTime.of(5, 30), OFFSET_MTWO),
2175+
LocalDateTime.of(1995, 9, 27, 5, 30).toEpochSecond(OFFSET_MTWO)},
2176+
};
2177+
}
2178+
2179+
@Test(dataProvider="epochSecond")
2180+
public void test_toEpochSecond(long actual, long expected) {
2181+
assertEquals(actual, expected);
2182+
}
2183+
21592184
//-----------------------------------------------------------------------
21602185
// compareTo()
21612186
//-----------------------------------------------------------------------

jdk/test/java/time/tck/java/time/TCKLocalTime.java

+26
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,32 @@ public void test_toSecondOfDay() {
24332433
}
24342434
}
24352435

2436+
//-----------------------------------------------------------------------
2437+
// toEpochSecond()
2438+
//--------------------------------------------------------------------------
2439+
@DataProvider(name="epochSecond")
2440+
Object[][] provider__toEpochSecond() {
2441+
return new Object[][] {
2442+
{LocalTime.of(0, 0).toEpochSecond(LocalDate.of(1970, 1, 1), OFFSET_PTWO), -7200L},
2443+
{LocalTime.of(11, 30).toEpochSecond(LocalDate.of(1965, 12, 31), OFFSET_PTWO), -126282600L},
2444+
{LocalTime.of(11, 30).toEpochSecond(LocalDate.of(1995, 5, 3), OFFSET_MTWO), 799507800L},
2445+
{LocalTime.of(0, 0).toEpochSecond(LocalDate.of(1970, 1, 1), OFFSET_PTWO),
2446+
Instant.ofEpochSecond(-7200).getEpochSecond()},
2447+
{LocalTime.of(11, 30).toEpochSecond(LocalDate.of(1969, 12, 31), OFFSET_MTWO),
2448+
Instant.ofEpochSecond(-37800L).getEpochSecond()},
2449+
{LocalTime.of(11, 30).toEpochSecond(LocalDate.of(1970, 1, 1), OFFSET_PTWO),
2450+
LocalDateTime.of(1970, 1, 1, 11, 30).toEpochSecond(OFFSET_PTWO)},
2451+
};
2452+
}
2453+
2454+
@Test(dataProvider="epochSecond")
2455+
public void test_toEpochSecond(long actual, long expected) {
2456+
assertEquals(actual, expected);
2457+
}
2458+
2459+
//-----------------------------------------------------------------------
2460+
// toSecondOfDay_fromNanoOfDay_symmetry()
2461+
//-----------------------------------------------------------------------
24362462
@Test
24372463
public void test_toSecondOfDay_fromNanoOfDay_symmetry() {
24382464
LocalTime t = LocalTime.of(0, 0);

jdk/test/java/time/tck/java/time/TCKOffsetTime.java

+24
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public class TCKOffsetTime extends AbstractDateTimeTest {
134134
private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza");
135135
private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
136136
private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
137+
private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2);
137138
private static final LocalDate DATE = LocalDate.of(2008, 12, 3);
138139
private OffsetTime TEST_11_30_59_500_PONE;
139140

@@ -1148,6 +1149,29 @@ public void test_format_formatter_null() {
11481149
OffsetTime.of(11, 30, 0, 0, OFFSET_PONE).format(null);
11491150
}
11501151

1152+
//-----------------------------------------------------------------------
1153+
// toEpochSecond()
1154+
//-----------------------------------------------------------------------
1155+
@DataProvider(name="epochSecond")
1156+
Object[][] provider_toEpochSecond() {
1157+
return new Object[][] {
1158+
{OffsetTime.of(0, 0, 0, 0, OFFSET_PTWO).toEpochSecond(LocalDate.of(1970, 1, 1)), -7200L},
1159+
{OffsetTime.of(11, 30, 0, 0, OFFSET_MTWO).toEpochSecond(LocalDate.of(1995, 9, 27)), 812208600L},
1160+
{OffsetTime.of(0, 0, 0, 0, OFFSET_PONE).toEpochSecond(LocalDate.of(1970, 1, 1)),
1161+
Instant.ofEpochSecond(-3600).getEpochSecond()},
1162+
{OffsetTime.of(11, 30, 0, 0, OFFSET_PTWO).toEpochSecond(LocalDate.of(1965, 12, 31)),
1163+
Instant.ofEpochSecond(-126282600L).getEpochSecond()},
1164+
{OffsetTime.of(11, 30, 0, 0, OFFSET_MTWO).toEpochSecond(LocalDate.of(1970, 1, 1)),
1165+
OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(11, 30), OFFSET_MTWO)
1166+
.toEpochSecond()},
1167+
};
1168+
}
1169+
1170+
@Test(dataProvider="epochSecond")
1171+
public void test_toEpochSecond(long actual, long expected) {
1172+
assertEquals(actual, expected);
1173+
}
1174+
11511175
//-----------------------------------------------------------------------
11521176
// compareTo()
11531177
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)