use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class ZonedDateTime method create.
/**
* Obtains an instance of {@code ZonedDateTime} using seconds from the
* epoch of 1970-01-01T00:00:00Z.
*
* @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z
* @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999
* @param zone the time-zone, not null
* @return the zoned date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
ZoneRules rules = zone.getRules();
// TODO: rules should be queryable by epochSeconds
Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond);
ZoneOffset offset = rules.getOffset(instant);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
return new ZonedDateTime(ldt, offset, zone);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class LocalDate method atStartOfDay.
/**
* Returns a zoned date-time from this date at the earliest valid time according
* to the rules in the time-zone.
* <p>
* Time-zone rules, such as daylight savings, mean that not every local date-time
* is valid for the specified zone, thus the local date-time may not be midnight.
* <p>
* In most cases, there is only one valid offset for a local date-time.
* In the case of an overlap, there are two valid offsets, and the earlier one is used,
* corresponding to the first occurrence of midnight on the date.
* In the case of a gap, the zoned date-time will represent the instant just after the gap.
* <p>
* If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
* <p>
* To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
* followed by {@link LocalDateTime#atZone(ZoneId)}.
*
* @param zone the zone ID to use, not null
* @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
*/
public ZonedDateTime atStartOfDay(ZoneId zone) {
Objects.requireNonNull(zone, "zone");
// need to handle case where there is a gap from 11:30 to 00:30
// standard ZDT factory would result in 01:00 rather than 00:30
LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
if (zone instanceof ZoneOffset == false) {
ZoneRules rules = zone.getRules();
ZoneOffsetTransition trans = rules.getTransition(ldt);
if (trans != null && trans.isGap()) {
ldt = trans.getDateTimeAfter();
}
}
return ZonedDateTime.of(ldt, zone);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class LocalDateTime method ofInstant.
//-------------------------------------------------------------------------
/**
* Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID.
* <p>
* This creates a local date-time based on the specified instant.
* First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
* which is simple as there is only one valid offset for each instant.
* Then, the instant and offset are used to calculate the local date-time.
*
* @param instant the instant to create the date-time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the local date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
Objects.requireNonNull(instant, "instant");
Objects.requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class OffsetDateTime method ofInstant.
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
* <p>
* This creates an offset date-time with the same instant as that specified.
* Finding the offset from UTC/Greenwich is simple as there is only one valid
* offset for each instant.
*
* @param instant the instant to create the date-time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the offset date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
Objects.requireNonNull(instant, "instant");
Objects.requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
return new OffsetDateTime(ldt, offset);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class TCKZoneRules method test_London_preTimeZones.
public void test_London_preTimeZones() {
ZoneRules test = europeLondon();
ZonedDateTime old = createZDT(1800, 1, 1, ZoneOffset.UTC);
Instant instant = old.toInstant();
ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(0, -1, -15);
assertEquals(test.getOffset(instant), offset);
checkOffset(test, old.toLocalDateTime(), offset, 1);
assertEquals(test.getStandardOffset(instant), offset);
assertEquals(test.getDaylightSavings(instant), Duration.ZERO);
assertEquals(test.isDaylightSavings(instant), false);
}
Aggregations