case DAYS: long secsD = Math.floorDiv(context.posixTime, 86400) * 86400; return Moment.of(secsD, TimeScale.POSIX); case HOURS: long secsH = Math.floorDiv(context.posixTime, 3600) * 3600; return Moment.of(secsH, TimeScale.POSIX); case MINUTES: long secsM = Math.floorDiv(context.posixTime, 60) * 60; return Moment.of(secsM, TimeScale.POSIX); case SECONDS: result = Moment.of(context.posixTime, 0, TimeScale.POSIX); break; case MILLISECONDS: int f3 = (context.getNanosecond() / MIO) * MIO; result = Moment.of(context.posixTime, f3, TimeScale.POSIX); break; case MICROSECONDS: int f6 = (context.getNanosecond() / 1000) * 1000; result = Moment.of(context.posixTime, f6, TimeScale.POSIX); break; case NANOSECONDS: if (context.isLeapSecond() && LeapSeconds.getInstance().isEnabled()) { return result.plus(1, SI.SECONDS); } else { return result;
import static net.time4j.PlainDate.DAY_OF_WEEK; import static net.time4j.Weekday.FRIDAY; File myFile = new File(""); long timeSinceCreateFile = myFile.lastModified(); // conversion to global timestamp in Time4J-format Moment fileTSP = TemporalTypes.MILLIS_SINCE_UNIX.transform(timeSinceCreateFile); // what ever you need (or just TZID timezone = Timezone.ofSystem().getID();) TZID timezone = AMERICA.MONTREAL; // "next friday" is a local time condition => convert to local timestamp PlainTimestamp localTSP = fileTSP.toZonalTimestamp(timezone); PlainTime walltime2130 = PlainTime.of(21, 30); // move to next time 21:30 (possibly on next day) and then to next or same Friday localTSP = localTSP.with(PlainTime.COMPONENT.setToNext(walltime2130)) .with(DAY_OF_WEEK.setToNextOrSame(FRIDAY)); // convert current time to local timestamp and compare at 21:30 boolean downloadNeeded = SystemClock.inZonalView(timezone).now().isAfter(localTSP);
@Override public Instant from(Moment moment) { return Instant.ofEpochSecond(moment.getPosixTime(), moment.getNanosecond()); }
@Override public int getInt(ChronoElement<Integer> element) { if (this.moment.isLeapSecond() && (element == SECOND_OF_MINUTE)) { return 60; } int value = this.timestamp.getInt(element); if (value == Integer.MIN_VALUE) { value = this.moment.getInt(element); } return value; }
@Override public CalendarYear createFrom( TimeSource<?> clock, AttributeQuery attributes ) { Timezone zone; if (attributes.contains(Attributes.TIMEZONE_ID)) { zone = Timezone.of(attributes.get(Attributes.TIMEZONE_ID)); } else if (attributes.get(Attributes.LENIENCY, Leniency.SMART).isLax()) { zone = Timezone.ofSystem(); } else { return null; } int y = Moment.from(clock.currentTime()).toZonalTimestamp(zone.getID()).getYear(); return CalendarYear.of(y); }
return Moment.from(UnixTime.class.cast(entity)).transformForParse(scale); } else if (entity.contains(LongElement.POSIX_TIME)) { long posixTime = entity.get(LongElement.POSIX_TIME).longValue(); fraction = entity.get(IntElement.FRACTION).intValue(); return Moment.of(posixTime, fraction, POSIX).transformForParse(scale); test = result.plus(1, SECONDS); } else { test = new Moment( result.getNanosecond(), result.getPosixTime() + 1);
moment.isLeapSecond() && isNonIsoOffset(timezone, moment) ) { if (moment.isAfter(START_LS_CHECK)) { if ( (this.element == SECOND_OF_MINUTE) && (this.extractValue() == 60) ) { if (moment.isLeapSecond()) { return moment; } else if (isNonIsoOffset(timezone, moment)) { + timezone.getOffset(moment)); } else if (getMaxSecondOfMinute(moment) == 60) { return moment.plus( Math.subtractExact(60, this.extractOld(moment)), SECONDS); return moment.plus(amount, SECONDS); case 1000: return moment.plus( Math.multiplyExact(MIO, amount), NANOSECONDS); case MIO: return moment.plus( Math.multiplyExact(1000, amount), NANOSECONDS);
Moment moment = Moment.of(secs, nano, TimeScale.POSIX); moment = moment.plus(1, SI.SECONDS); if (!moment.isLeapSecond()) { throw new IllegalArgumentException("Parsed leap second is invalid.");
utc = Iso8601Format.ofMoment(dateStyle, decimalStyle, precision, ZonalOffset.UTC); buffer.append(infinityStyle.displayPast(utc, Moment.axis())); } else { printer.print(start, buffer); PlainTimestamp tsp2 = end.toZonalTimestamp(offset); PlainDate d1 = start.toZonalTimestamp(offset).getCalendarDate(); PlainDate d2 = tsp2.getCalendarDate(); if (end.isLeapSecond()) { for (ElementPosition position : positions) { if (position.getElement() == PlainTime.SECOND_OF_MINUTE) { utc = Iso8601Format.ofMoment(dateStyle, decimalStyle, precision, ZonalOffset.UTC); buffer.append(infinityStyle.displayFuture(utc, Moment.axis())); } else { printer.print(end, buffer);
/** * <p>Equivalent to {@code Moment.of(elapsedTime, 0, scale)}. </p> * * @param elapsedTime elapsed seconds on given time scale * @param scale time scale reference * @return new moment instance * @throws IllegalArgumentException if elapsed time is out of range limits * beyond year +/-999,999,999 or out of time scale range * @throws IllegalStateException if time scale is not POSIX but * leap second support is switched off by configuration * @see LeapSeconds#isEnabled() */ /*[deutsch] * <p>Entspricht {@code Moment.of(elapsedTime, 0, scale)}. </p> * * @param elapsedTime elapsed seconds on given time scale * @param scale time scale reference * @return new moment instance * @throws IllegalArgumentException if elapsed time is out of range limits * beyond year +/-999,999,999 or out of time scale range * @throws IllegalStateException if time scale is not POSIX but * leap second support is switched off by configuration * @see LeapSeconds#isEnabled() */ public static Moment of( long elapsedTime, TimeScale scale ) { return Moment.of(elapsedTime, 0, scale); }
@Override public <V> V getMaximum(ChronoElement<V> element) { V max; if (this.timestamp.contains(element)) { max = this.timestamp.getMaximum(element); } else { max = this.moment.getMaximum(element); } if ( (element == SECOND_OF_MINUTE) && (this.timestamp.getYear() >= 1972) ) { PlainTimestamp ts = this.timestamp.with(element, max); if (!this.zone.isInvalid(ts, ts)) { Moment transformed = ts.in(this.zone); Moment test = transformed.plus(1, SI.SECONDS); if (test.isLeapSecond()) { return element.getType().cast(Integer.valueOf(60)); } } } return max; }
@Override public Moment withValue( Moment context, Integer value, boolean lenient ) { if (value == null) { throw new IllegalArgumentException("Missing fraction value."); } if (LeapSeconds.getInstance().isEnabled()) { return Moment.of( context.getElapsedTime(TimeScale.UTC), value.intValue(), TimeScale.UTC); } else { return Moment.of( context.getPosixTime(), value.intValue(), TimeScale.POSIX); } }
if (this.isLeapSecond()) { return new Moment( this.getNanosecond(), this.posixTime ); return this; case TAI: return new Moment( this.getNanosecond(scale), Math.addExact( this.getElapsedTime(scale), POSIX_UTC_DELTA - UTC_TAI_DELTA) ); case TT: case UT: return new Moment( this.getNanosecond(scale), Math.addExact( this.getElapsedTime(scale), POSIX_UTC_DELTA) ); case GPS: return new Moment( this.getNanosecond(), Math.addExact( this.getElapsedTime(GPS), POSIX_GPS_DELTA) );
private static Moment moveEventuallyToLS(Moment adjusted) { PlainDate date = adjusted.getDateUTC(); PlainTime time = adjusted.getTimeUTC(); if ( (LeapSeconds.getInstance().getShift(date) == 1) && (time.getHour() == 23) && (time.getMinute() == 59) && (time.getSecond() == 59) ) { return adjusted.plus(1, SI.SECONDS); } else { return adjusted; } }
@Override public Moment addTo( Moment context, long amount ) { if (this.unit.compareTo(TimeUnit.SECONDS) >= 0) { long secs = Math.multiplyExact(amount, this.unit.toSeconds(1)); return Moment.of( Math.addExact(context.getPosixTime(), secs), context.getNanosecond(), POSIX ); } else { // MILLIS, MICROS, NANOS long nanos = Math.multiplyExact(amount, this.unit.toNanos(1)); long sum = Math.addExact(context.getNanosecond(), nanos); int nano = (int) Math.floorMod(sum, MRD); long second = Math.floorDiv(sum, MRD); return Moment.of( Math.addExact(context.getPosixTime(), second), nano, POSIX ); } }
@Override public Moment createFrom( TimeSource<?> clock, AttributeQuery attributes ) { return Moment.from(clock.currentTime()); }
if (!end.isAfter(start)) { throw new IllegalArgumentException("End is not after start."); PlainTimestamp s = start.toZonalTimestamp(offset); PlainTimestamp e = end.toZonalTimestamp(offset);