Java Code Examples for java.time.Instant.getEpochSecond()

The following are Jave code examples for showing how to use getEpochSecond() of the java.time.Instant class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: device-modbus   File: Scheduler.java   Source Code and License Vote up 5 votes
@Scheduled(fixedRateString = "${schedule.interval}")
public void schedule()
{
	synchronized (scheduleContextQueue)
	{
		// Instant is in epoch time
		Instant nowInstant = Instant.now();
		long nowEpoch = nowInstant.getEpochSecond();

		// logger.debug("tick " + nowInstant.toString());

		while (scheduleContextQueue.peek() != null && scheduleContextQueue.peek().getNextTime().toEpochSecond() <= nowEpoch)
		{
			try {
				// pop the schedule context off the queue
				ScheduleContext scheduleContext = scheduleContextQueue.remove();

				logger.debug("executing schedule " + scheduleContext.getInfo() + " at " + scheduleContext.getNextTime());
				
				// run the events for the schedule
				scheduleEventExecutor.execute(scheduleContext.getScheduleEvents());

				// update the context 
				scheduleContext.updateNextTime();
				scheduleContext.updateIterations();

				// if the schedule is not complete, enqueue it.
				if(scheduleContext.isComplete()) {
					logger.info("schedule " + scheduleContext.getInfo() + " is complete." + scheduleContext.toString());
				} else {
					logger.debug("queueing schedule " + scheduleContext.getInfo());
					scheduleContextQueue.add(scheduleContext);
				}
			} catch (Exception e) {
				logger.error("exception while scheduling schedule contects" + e);
			}
		}
	}
}
 
Example 2
Project: device-bluetooth   File: Scheduler.java   Source Code and License Vote up 5 votes
@Scheduled(fixedRateString = "${schedule.interval}")
public void schedule() {
  synchronized (scheduleContextQueue) {
    // Instant is in epoch time
    Instant nowInstant = Instant.now();
    long nowEpoch = nowInstant.getEpochSecond();

    // logger.debug("tick " + nowInstant.toString());

    while (scheduleContextQueue.peek() != null
        && scheduleContextQueue.peek().getNextTime().toEpochSecond() <= nowEpoch) {
      try {
        // pop the schedule context off the queue
        ScheduleContext scheduleContext = scheduleContextQueue.remove();

        logger.debug("executing schedule " + scheduleContext.getInfo() + " at "
            + scheduleContext.getNextTime());

        // run the events for the schedule
        scheduleEventExecutor.execute(scheduleContext.getScheduleEvents());

        // update the context
        scheduleContext.updateNextTime();
        scheduleContext.updateIterations();

        // if the schedule is not complete, enqueue it.
        if (scheduleContext.isComplete()) {
          logger.info("schedule " + scheduleContext.getInfo() + " is complete."
              + scheduleContext.toString());
        } else {
          logger.debug("queueing schedule " + scheduleContext.getInfo());
          scheduleContextQueue.add(scheduleContext);
        }
      } catch (Exception e) {
        logger.error("exception while scheduling schedule contects" + e);
      }
    }
  }
}
 
Example 3
Project: device-opcua-java   File: Scheduler.java   Source Code and License Vote up 5 votes
@Scheduled(fixedRateString = "${schedule.interval}")
public void schedule() {
  synchronized (scheduleContextQueue) {
    // Instant is in epoch time
    Instant nowInstant = Instant.now();
    long nowEpoch = nowInstant.getEpochSecond();

    // logger.debug("tick " + nowInstant.toString());

    while (scheduleContextQueue.peek() != null
        && scheduleContextQueue.peek().getNextTime().toEpochSecond() <= nowEpoch) {
      try {
        // pop the schedule context off the queue
        ScheduleContext scheduleContext = scheduleContextQueue.remove();

        logger.debug("executing schedule " + scheduleContext.getInfo() + " at "
            + scheduleContext.getNextTime());

        // run the events for the schedule
        scheduleEventExecutor.execute(scheduleContext.getScheduleEvents());

        // update the context
        scheduleContext.updateNextTime();
        scheduleContext.updateIterations();

        // if the schedule is not complete, enqueue it.
        if (scheduleContext.isComplete()) {
          logger.info("schedule " + scheduleContext.getInfo() + " is complete."
              + scheduleContext.toString());
        } else {
          logger.debug("queueing schedule " + scheduleContext.getInfo());
          scheduleContextQueue.add(scheduleContext);
        }
      } catch (Exception e) {
        logger.error("exception while scheduling schedule contects" + e);
      }
    }
  }
}
 
Example 4
Project: device-bacnet   File: Scheduler.java   Source Code and License Vote up 5 votes
@Scheduled(fixedRateString = "${schedule.interval}")
public void schedule() {
  synchronized (scheduleContextQueue) {
    // Instant is in epoch time
    Instant nowInstant = Instant.now();
    long nowEpoch = nowInstant.getEpochSecond();

    // logger.debug("tick " + nowInstant.toString());

    while (scheduleContextQueue.peek() != null
        && scheduleContextQueue.peek().getNextTime().toEpochSecond() <= nowEpoch) {
      try {
        // pop the schedule context off the queue
        ScheduleContext scheduleContext = scheduleContextQueue.remove();

        logger.debug("executing schedule " + scheduleContext.getInfo() + " at "
            + scheduleContext.getNextTime());

        // run the events for the schedule
        scheduleEventExecutor.execute(scheduleContext.getScheduleEvents());

        // update the context
        scheduleContext.updateNextTime();
        scheduleContext.updateIterations();

        // if the schedule is not complete, enqueue it.
        if (scheduleContext.isComplete()) {
          logger.info("schedule " + scheduleContext.getInfo() + " is complete."
              + scheduleContext.toString());
        } else {
          logger.debug("queueing schedule " + scheduleContext.getInfo());
          scheduleContextQueue.add(scheduleContext);
        }
      } catch (Exception e) {
        logger.error("exception while scheduling schedule contects" + e);
      }
    }
  }
}
 
Example 5
Project: OpenJSharp   File: ZoneRules.java   Source Code and License Vote up 5 votes
/**
 * Gets the standard offset for the specified instant in this zone.
 * <p>
 * This provides access to historic information on how the standard offset
 * has changed over time.
 * The standard offset is the offset before any daylight saving time is applied.
 * This is typically the offset applicable during winter.
 *
 * @param instant  the instant to find the offset information for, not null, but null
 *  may be ignored if the rules have a single offset for all instants
 * @return the standard offset, not null
 */
public ZoneOffset getStandardOffset(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return standardOffsets[0];
    }
    long epochSec = instant.getEpochSecond();
    int index  = Arrays.binarySearch(standardTransitions, epochSec);
    if (index < 0) {
        // switch negative insert position to start of matched range
        index = -index - 2;
    }
    return standardOffsets[index + 1];
}
 
Example 6
Project: dibd   File: ArticleFactory.java   Source Code and License Vote up 5 votes
/**
 * Creates a new Article object. www input, NNTP POST.
 * No message_id
 * 
 * @param thread_id - null if thread
 * @param a_name	may be null
 * @param subject	may be null
 * @param message	may be null
 * @param group
 * @param fileName may be null
 * @param contentType may be null
 * @return never null
 */
public static ArticleWebInput crAWebInput(Integer thread_id, String a_name, String subject, String message, Group group, String fileName, String contentType){
	Art a = new Art();
	
	if (a_name != null)
		if (a_name.isEmpty())
			a_name = null;
		else
			a.a_name = escapeString(a_name);
	
	if (subject != null)
		if (subject.isEmpty())
			subject = null;
		else
			a.subject = escapeString(subject);
	
	if (message != null)
		if (message.isEmpty())
			message = null;
		else
			message = message.replaceAll("\\s+$", ""); //we are more accurate with message. UTF-8 0 byte may appear..
	
	a.thread_id = thread_id;
	a.message = message;
	a.groupId = group.getInternalID(); //for input
	a.groupName = group.getName();
	Instant nowEpoch = Instant.now();        
	a.post_time = nowEpoch.getEpochSecond();
	a.msgID_host = Config.inst().get(Config.HOSTNAME, null);
	
	//TODO:check that .xxx is write and contentType == null with filename 
	a.fileName = fileName; //TODO make it .xxx - for very rare formats
	a.fileFormat = contentType; //if not null file exist
	
	generateHash(a);
	
	
	return new Article(a);
}
 
Example 7
Project: jdk8u-jdk   File: ZoneRules.java   Source Code and License Vote up 5 votes
/**
 * Gets the offset applicable at the specified instant in these rules.
 * <p>
 * The mapping from an instant to an offset is simple, there is only
 * one valid offset for each instant.
 * This method returns that offset.
 *
 * @param instant  the instant to find the offset for, not null, but null
 *  may be ignored if the rules have a single offset for all instants
 * @return the offset, not null
 */
public ZoneOffset getOffset(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return standardOffsets[0];
    }
    long epochSec = instant.getEpochSecond();
    // check if using last rules
    if (lastRules.length > 0 &&
            epochSec > savingsInstantTransitions[savingsInstantTransitions.length - 1]) {
        int year = findYear(epochSec, wallOffsets[wallOffsets.length - 1]);
        ZoneOffsetTransition[] transArray = findTransitionArray(year);
        ZoneOffsetTransition trans = null;
        for (int i = 0; i < transArray.length; i++) {
            trans = transArray[i];
            if (epochSec < trans.toEpochSecond()) {
                return trans.getOffsetBefore();
            }
        }
        return trans.getOffsetAfter();
    }

    // using historic rules
    int index  = Arrays.binarySearch(savingsInstantTransitions, epochSec);
    if (index < 0) {
        // switch negative insert position to start of matched range
        index = -index - 2;
    }
    return wallOffsets[index + 1];
}
 
Example 8
Project: jdk8u-jdk   File: ZoneRules.java   Source Code and License Vote up 5 votes
/**
 * Gets the standard offset for the specified instant in this zone.
 * <p>
 * This provides access to historic information on how the standard offset
 * has changed over time.
 * The standard offset is the offset before any daylight saving time is applied.
 * This is typically the offset applicable during winter.
 *
 * @param instant  the instant to find the offset information for, not null, but null
 *  may be ignored if the rules have a single offset for all instants
 * @return the standard offset, not null
 */
public ZoneOffset getStandardOffset(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return standardOffsets[0];
    }
    long epochSec = instant.getEpochSecond();
    int index  = Arrays.binarySearch(standardTransitions, epochSec);
    if (index < 0) {
        // switch negative insert position to start of matched range
        index = -index - 2;
    }
    return standardOffsets[index + 1];
}
 
Example 9
Project: openjdk-jdk10   File: ZoneRules.java   Source Code and License Vote up 5 votes
/**
 * Gets the offset applicable at the specified instant in these rules.
 * <p>
 * The mapping from an instant to an offset is simple, there is only
 * one valid offset for each instant.
 * This method returns that offset.
 *
 * @param instant  the instant to find the offset for, not null, but null
 *  may be ignored if the rules have a single offset for all instants
 * @return the offset, not null
 */
public ZoneOffset getOffset(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return standardOffsets[0];
    }
    long epochSec = instant.getEpochSecond();
    // check if using last rules
    if (lastRules.length > 0 &&
            epochSec > savingsInstantTransitions[savingsInstantTransitions.length - 1]) {
        int year = findYear(epochSec, wallOffsets[wallOffsets.length - 1]);
        ZoneOffsetTransition[] transArray = findTransitionArray(year);
        ZoneOffsetTransition trans = null;
        for (int i = 0; i < transArray.length; i++) {
            trans = transArray[i];
            if (epochSec < trans.toEpochSecond()) {
                return trans.getOffsetBefore();
            }
        }
        return trans.getOffsetAfter();
    }

    // using historic rules
    int index  = Arrays.binarySearch(savingsInstantTransitions, epochSec);
    if (index < 0) {
        // switch negative insert position to start of matched range
        index = -index - 2;
    }
    return wallOffsets[index + 1];
}
 
Example 10
Project: rkt-launcher   File: Time.java   Source Code and License Vote up 4 votes
@Override
public Long convert(final Instant value) {
  return value.getEpochSecond() * NANOS_PER_SECOND + value.getNano();
}
 
Example 11
Project: nextcloud-java-api   File: InstantXmlAdapter.java   Source Code and License Vote up 4 votes
@Override
public Long marshal(Instant instant)
{
    return instant.getEpochSecond();
}
 
Example 12
Project: openjdk-jdk10   File: TestClock_System.java   Source Code and License Vote up 4 votes
private static String formatTime(String prefix, Instant time) {
    return prefix + ": " + time + " - seconds: "
                    + time.getEpochSecond() + ", nanos: "
                    + time.getNano();
}
 
Example 13
Project: openjdk-jdk10   File: TestClock_System.java   Source Code and License Vote up 4 votes
static void testWithOffset(String name, long offset, Clock clock)
        throws IllegalAccessException {
    offsetField.set(clock, offset);
    long beforeMillis = System.currentTimeMillis();
    final Instant instant = clock.instant();
    long afterMillis = System.currentTimeMillis();
    long actualOffset = offsetField.getLong(clock);
    long instantMillis = instant.getEpochSecond() * MILLIS_IN_SECOND
            + instant.getNano() / NANOS_IN_MILLI;
    if (instantMillis < beforeMillis || instantMillis > afterMillis) {
        throw new RuntimeException(name
                + ": Invalid instant: " + instant
                + " (~" + instantMillis + "ms)"
                + " when time in millis is in ["
                + beforeMillis + ", " + afterMillis
                + "] and offset in seconds is " + offset);
    }
    Answer isOffLimits = isOffLimits(beforeMillis / MILLIS_IN_SECOND,
            afterMillis / MILLIS_IN_SECOND, offset);
    switch (isOffLimits) {
        case YES:
            if (actualOffset == offset) {
                throw new RuntimeException(name
                        + ": offset was offlimit but was not recomputed "
                        + " when time in millis is in ["
                        + beforeMillis + ", " + afterMillis
                        + "] and offset in seconds was " + offset);
            }
            break;
        case NO:
            if (actualOffset != offset) {
                throw new RuntimeException(name
                        + ": offset was not offlimit but was recomputed.");
            }
            break;
        default:
            break;
    }
    if (distance(actualOffset, instant.getEpochSecond()) >= MAX_OFFSET) {
        throw new RuntimeException(name + ": Actual offset is too far off:"
                + " offset=" + actualOffset
                + "instant.seconds=" + instant.getEpochSecond());
    }
    long adjustment = (instant.getEpochSecond() - actualOffset) * NANOS_IN_SECOND
            + instant.getNano();
    validateAdjustment(name, actualOffset, beforeMillis, afterMillis, adjustment);
}
 
Example 14
Project: OpenJSharp   File: ZoneRules.java   Source Code and License Vote up 4 votes
/**
 * Gets the previous transition before the specified instant.
 * <p>
 * This returns details of the previous transition after the specified instant.
 * For example, if the instant represents a point where "summer" daylight saving time
 * applies, then the method will return the transition from the previous "winter" time.
 *
 * @param instant  the instant to get the previous transition after, not null, but null
 *  may be ignored if the rules have a single offset for all instants
 * @return the previous transition after the specified instant, null if this is before the first transition
 */
public ZoneOffsetTransition previousTransition(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return null;
    }
    long epochSec = instant.getEpochSecond();
    if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) {
        epochSec += 1;  // allow rest of method to only use seconds
    }

    // check if using last rules
    long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1];
    if (lastRules.length > 0 && epochSec > lastHistoric) {
        // search year the instant is in
        ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1];
        int year = findYear(epochSec, lastHistoricOffset);
        ZoneOffsetTransition[] transArray = findTransitionArray(year);
        for (int i = transArray.length - 1; i >= 0; i--) {
            if (epochSec > transArray[i].toEpochSecond()) {
                return transArray[i];
            }
        }
        // use last from preceding year
        int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset);
        if (--year > lastHistoricYear) {
            transArray = findTransitionArray(year);
            return transArray[transArray.length - 1];
        }
        // drop through
    }

    // using historic rules
    int index  = Arrays.binarySearch(savingsInstantTransitions, epochSec);
    if (index < 0) {
        index = -index - 1;
    }
    if (index <= 0) {
        return null;
    }
    return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]);
}
 
Example 15
Project: openjdk-jdk10   File: ZoneRules.java   Source Code and License Vote up 4 votes
/**
 * Gets the previous transition before the specified instant.
 * <p>
 * This returns details of the previous transition before the specified instant.
 * For example, if the instant represents a point where "summer" daylight saving time
 * applies, then the method will return the transition from the previous "winter" time.
 *
 * @param instant  the instant to get the previous transition after, not null, but null
 *  may be ignored if the rules have a single offset for all instants
 * @return the previous transition before the specified instant, null if this is before the first transition
 */
public ZoneOffsetTransition previousTransition(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return null;
    }
    long epochSec = instant.getEpochSecond();
    if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) {
        epochSec += 1;  // allow rest of method to only use seconds
    }

    // check if using last rules
    long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1];
    if (lastRules.length > 0 && epochSec > lastHistoric) {
        // search year the instant is in
        ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1];
        int year = findYear(epochSec, lastHistoricOffset);
        ZoneOffsetTransition[] transArray = findTransitionArray(year);
        for (int i = transArray.length - 1; i >= 0; i--) {
            if (epochSec > transArray[i].toEpochSecond()) {
                return transArray[i];
            }
        }
        // use last from preceding year
        int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset);
        if (--year > lastHistoricYear) {
            transArray = findTransitionArray(year);
            return transArray[transArray.length - 1];
        }
        // drop through
    }

    // using historic rules
    int index  = Arrays.binarySearch(savingsInstantTransitions, epochSec);
    if (index < 0) {
        index = -index - 1;
    }
    if (index <= 0) {
        return null;
    }
    return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]);
}
 
Example 16
Project: jdk8u-jdk   File: ZoneRules.java   Source Code and License Vote up 4 votes
/**
 * Gets the previous transition before the specified instant.
 * <p>
 * This returns details of the previous transition after the specified instant.
 * For example, if the instant represents a point where "summer" daylight saving time
 * applies, then the method will return the transition from the previous "winter" time.
 *
 * @param instant  the instant to get the previous transition after, not null, but null
 *  may be ignored if the rules have a single offset for all instants
 * @return the previous transition after the specified instant, null if this is before the first transition
 */
public ZoneOffsetTransition previousTransition(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return null;
    }
    long epochSec = instant.getEpochSecond();
    if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) {
        epochSec += 1;  // allow rest of method to only use seconds
    }

    // check if using last rules
    long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1];
    if (lastRules.length > 0 && epochSec > lastHistoric) {
        // search year the instant is in
        ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1];
        int year = findYear(epochSec, lastHistoricOffset);
        ZoneOffsetTransition[] transArray = findTransitionArray(year);
        for (int i = transArray.length - 1; i >= 0; i--) {
            if (epochSec > transArray[i].toEpochSecond()) {
                return transArray[i];
            }
        }
        // use last from preceding year
        int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset);
        if (--year > lastHistoricYear) {
            transArray = findTransitionArray(year);
            return transArray[transArray.length - 1];
        }
        // drop through
    }

    // using historic rules
    int index  = Arrays.binarySearch(savingsInstantTransitions, epochSec);
    if (index < 0) {
        index = -index - 1;
    }
    if (index <= 0) {
        return null;
    }
    return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]);
}
 
Example 17
Project: openjdk-jdk10   File: Timestamp.java   Source Code and License Vote up 3 votes
/**
 * Obtains an instance of {@code Timestamp} from an {@link Instant} object.
 * <p>
 * {@code Instant} can store points on the time-line further in the future
 * and further in the past than {@code Date}. In this scenario, this method
 * will throw an exception.
 *
 * @param instant  the instant to convert
 * @return an {@code Timestamp} representing the same point on the time-line as
 *  the provided instant
 * @exception NullPointerException if {@code instant} is null.
 * @exception IllegalArgumentException if the instant is too large to
 *  represent as a {@code Timestamp}
 * @since 1.8
 */
public static Timestamp from(Instant instant) {
    try {
        Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
        stamp.nanos = instant.getNano();
        return stamp;
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
Example 18
Project: ClassroomFlipkart   File: Timestamp.java   Source Code and License Vote up 3 votes
/**
 * Obtains an instance of {@code Timestamp} from an {@link Instant} object.
 * <p>
 * {@code Instant} can store points on the time-line further in the future
 * and further in the past than {@code Date}. In this scenario, this method
 * will throw an exception.
 *
 * @param instant  the instant to convert
 * @return an {@code Timestamp} representing the same point on the time-line as
 *  the provided instant
 * @exception NullPointerException if {@code instant} is null.
 * @exception IllegalArgumentException if the instant is too large to
 *  represent as a {@code Timesamp}
 * @since 1.8
 */
public static Timestamp from(Instant instant) {
    try {
        Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
        stamp.nanos = instant.getNano();
        return stamp;
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
Example 19
Project: jdk8u-jdk   File: Timestamp.java   Source Code and License Vote up 3 votes
/**
 * Obtains an instance of {@code Timestamp} from an {@link Instant} object.
 * <p>
 * {@code Instant} can store points on the time-line further in the future
 * and further in the past than {@code Date}. In this scenario, this method
 * will throw an exception.
 *
 * @param instant  the instant to convert
 * @return an {@code Timestamp} representing the same point on the time-line as
 *  the provided instant
 * @exception NullPointerException if {@code instant} is null.
 * @exception IllegalArgumentException if the instant is too large to
 *  represent as a {@code Timesamp}
 * @since 1.8
 */
public static Timestamp from(Instant instant) {
    try {
        Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
        stamp.nanos = instant.getNano();
        return stamp;
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
Example 20
Project: OpenJSharp   File: Timestamp.java   Source Code and License Vote up 3 votes
/**
 * Obtains an instance of {@code Timestamp} from an {@link Instant} object.
 * <p>
 * {@code Instant} can store points on the time-line further in the future
 * and further in the past than {@code Date}. In this scenario, this method
 * will throw an exception.
 *
 * @param instant  the instant to convert
 * @return an {@code Timestamp} representing the same point on the time-line as
 *  the provided instant
 * @exception NullPointerException if {@code instant} is null.
 * @exception IllegalArgumentException if the instant is too large to
 *  represent as a {@code Timesamp}
 * @since 1.8
 */
public static Timestamp from(Instant instant) {
    try {
        Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
        stamp.nanos = instant.getNano();
        return stamp;
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException(ex);
    }
}