コード例 #1
0
 @Override
 public ScheduleStatus map(int index, ResultSet r, StatementContext ctx) throws SQLException {
   return ScheduleStatus.of(
       ScheduleTime.of(
           Instant.ofEpochSecond(r.getLong("next_schedule_time")),
           Instant.ofEpochSecond(r.getLong("next_run_time"))),
       getOptionalLong(r, "last_session_time").transform(it -> Instant.ofEpochSecond(it)));
 }
コード例 #2
0
ファイル: ValueInfo.java プロジェクト: kliv/cs-studio
  public void update(final VType value) {
    display_info = Optional.ofNullable(ValueUtil.displayOf(value));
    if (display_info.isPresent()) value_dbl = ValueUtil.numericValueOf(value).doubleValue();

    if (value instanceof VNumber)
      value_str = Double.toString(((VNumber) value).getValue().doubleValue());
    else if (value instanceof VEnum) {
      final VEnum ev = (VEnum) value;
      if (ev.getIndex() >= 0 && ev.getIndex() < ev.getLabels().size())
        value_str = ev.getLabels().get(ev.getIndex());
      else value_str = Integer.toString(ev.getIndex());
    } else if (value instanceof VString) value_str = ((VString) value).getValue();
    else value_str = Objects.toString(value);

    final Time vtime = ValueUtil.timeOf(value);
    if (vtime == null) return;

    final Timestamp stamp = vtime.getTimestamp();
    final Instant new_time = Instant.ofEpochSecond(stamp.getSec(), stamp.getNanoSec());
    if (time != null) {
      final Duration duration = Duration.between(time, new_time);
      final double period = duration.getSeconds() + duration.getNano() * 1e-9;
      value_period.add(period);
    } else value_period.reset();
    time = new_time;
  }
コード例 #3
0
ファイル: LogFiles.java プロジェクト: czohshima/digdag
  public static LogFileHandle buildLogFileHandleFromFileName(String fileName, long fileSize) {
    // TODO use regexp for reliable parsing logic
    String[] taskNameAndRest = fileName.split("@", 2);
    if (taskNameAndRest.length < 2) {
      return null;
    }
    String taskName = taskNameAndRest[0];

    String[] timeAndRest = taskNameAndRest[1].split("\\.", 2);
    if (timeAndRest.length < 2) {
      return null;
    }

    Instant firstLogTime;
    try {
      long sec = Long.parseLong(timeAndRest[0].substring(0, 8), 16);
      int nsec = Integer.parseInt(timeAndRest[0].substring(8, 16), 16);
      firstLogTime = Instant.ofEpochSecond(sec, nsec);
    } catch (NumberFormatException ex) {
      return null;
    }

    String agentId =
        timeAndRest[1].substring(0, timeAndRest[1].length() - LOG_GZ_FILE_SUFFIX.length());

    return LogFileHandle.builder()
        .fileName(fileName)
        .fileSize(fileSize)
        .taskName(taskName)
        .firstLogTime(firstLogTime)
        .agentId(agentId)
        .direct(Optional.absent())
        .build();
  }
コード例 #4
0
  /** Read info from data server */
  public void read(RPCClientImpl client, double REQUEST_TIMEOUT) throws Exception {

    PVStructure pvRequest = createRequest();

    PVInt keyField = pvRequest.getIntField("key");
    keyField.put(key);
    PVString patternField = pvRequest.getStringField("pattern");
    patternField.put(pattern);

    PVStructure pvResult = client.request(pvRequest, REQUEST_TIMEOUT);

    // process result

    PVStructureArray infos = pvResult.getStructureArrayField("channels");

    StructureArrayData data = new StructureArrayData();
    infos.get(0, infos.getLength(), data);

    this.chNames = new String[data.data.length];
    this.starts = new Instant[data.data.length];
    this.ends = new Instant[data.data.length];

    for (int i = 0; i < data.data.length; i++) {

      PVStructure info = data.data[i];

      chNames[i] = info.getStringField("name").get();

      int start_secs = info.getIntField("start_sec").get();
      int start_nano = info.getIntField("start_nano").get();
      starts[i] = Instant.ofEpochSecond(start_secs, start_nano);

      int end_secs = info.getIntField("end_sec").get();
      int end_nano = info.getIntField("end_nano").get();
      ends[i] = Instant.ofEpochSecond(end_secs, end_nano);
    }
  }
コード例 #5
0
  private static void useLocalDate() {
    LocalDate date = LocalDate.of(2014, 3, 18);
    int year = date.getYear(); // 2014
    Month month = date.getMonth(); // MARCH
    int day = date.getDayOfMonth(); // 18
    DayOfWeek dow = date.getDayOfWeek(); // TUESDAY
    int len = date.lengthOfMonth(); // 31 (days in March)
    boolean leap = date.isLeapYear(); // false (not a leap year)
    System.out.println(date);

    int y = date.get(ChronoField.YEAR);
    int m = date.get(ChronoField.MONTH_OF_YEAR);
    int d = date.get(ChronoField.DAY_OF_MONTH);

    LocalTime time = LocalTime.of(13, 45, 20); // 13:45:20
    int hour = time.getHour(); // 13
    int minute = time.getMinute(); // 45
    int second = time.getSecond(); // 20
    System.out.println(time);

    LocalDateTime dt1 = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45, 20); // 2014-03-18T13:45
    LocalDateTime dt2 = LocalDateTime.of(date, time);
    LocalDateTime dt3 = date.atTime(13, 45, 20);
    LocalDateTime dt4 = date.atTime(time);
    LocalDateTime dt5 = time.atDate(date);
    System.out.println(dt1);

    LocalDate date1 = dt1.toLocalDate();
    System.out.println(date1);
    LocalTime time1 = dt1.toLocalTime();
    System.out.println(time1);

    Instant instant = Instant.ofEpochSecond(44 * 365 * 86400);
    Instant now = Instant.now();

    Duration d1 = Duration.between(LocalTime.of(13, 45, 10), time);
    Duration d2 = Duration.between(instant, now);
    System.out.println(d1.getSeconds());
    System.out.println(d2.getSeconds());

    Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
    System.out.println(threeMinutes);

    JapaneseDate japaneseDate = JapaneseDate.from(date);
    System.out.println(japaneseDate);
  }
コード例 #6
0
ファイル: JavatimeTest.java プロジェクト: FauxFaux/jdk9-jdk
  public static void main(String[] args) throws Throwable {

    int N = 10000;
    long t1970 = new java.util.Date(70, 0, 01).getTime();
    Random r = new Random();
    for (int i = 0; i < N; i++) {
      int days = r.nextInt(50) * 365 + r.nextInt(365);
      long secs = t1970 + days * 86400 + r.nextInt(86400);
      int nanos = r.nextInt(NANOS_PER_SECOND);
      int nanos_ms = nanos / 1000000 * 1000000; // millis precision
      long millis = secs * 1000 + r.nextInt(1000);
      LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
      LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
      Instant inst = Instant.ofEpochSecond(secs, nanos);
      Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
      ///////////// java.util.Date /////////////////////////
      Date jud = new java.util.Date(millis);
      Instant inst0 = jud.toInstant();
      if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
      }
      // roundtrip only with millis precision
      Date jud0 = Date.from(inst_ms);
      if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
      }
      //////////// java.util.GregorianCalendar /////////////
      GregorianCalendar cal = new GregorianCalendar();
      // non-roundtrip of tz name between j.u.tz and j.t.zid
      cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
      cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
      cal.setFirstDayOfWeek(Calendar.MONDAY);
      cal.setMinimalDaysInFirstWeek(4);
      cal.setTimeInMillis(millis);
      ZonedDateTime zdt0 = cal.toZonedDateTime();
      if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli()
          || !cal.equals(GregorianCalendar.from(zdt0))) {
        System.out.println("cal:" + cal);
        System.out.println("zdt:" + zdt0);
        System.out.println("calNew:" + GregorianCalendar.from(zdt0));
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
      }
      inst0 = cal.toInstant();
      if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: gcal -> zdt");
      }
      ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
      GregorianCalendar cal0 = GregorianCalendar.from(zdt);
      if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis()
          || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
      }
    }

    ///////////// java.util.TimeZone /////////////////////////
    for (String zidStr : TimeZone.getAvailableIDs()) {
      // TBD: tzdt intergration
      if (zidStr.startsWith("SystemV")
          || zidStr.contains("Riyadh8")
          || zidStr.equals("US/Pacific-New")
          || zidStr.equals("EST")
          || zidStr.equals("HST")
          || zidStr.equals("MST")) {
        continue;
      }
      ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
      if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
        throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
      }
      TimeZone tz = TimeZone.getTimeZone(zidStr);
      // no round-trip for alias and "GMT"
      if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId()))
          && !ZoneId.SHORT_IDS.containsKey(zidStr)
          && !zidStr.startsWith("GMT")) {
        throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
      }
    }
    System.out.println("Passed!");
  }
コード例 #7
0
 private Instant importTimeStampFrom(String line) {
   return Instant.ofEpochSecond(Long.valueOf(line));
 }
コード例 #8
0
  @Test
  public void http() throws InterruptedException {
    String tokenHeaderName = WERVAL.application().config().string(JWT.HTTP_HEADER_CONFIG_KEY);
    JWT jwt = WERVAL.application().plugin(JWT.class);

    // Unauthorized access to authenticated resource
    when().get("/authenticated").then().statusCode(UNAUTHORIZED_CODE);

    // Login
    String token =
        given()
            .body("{\"email\":\"[email protected]\",\"password\":\"admin-password\"}")
            .contentType(APPLICATION_JSON)
            .when()
            .post("/login")
            .then()
            .statusCode(OK_CODE)
            .header(tokenHeaderName, notNullValue())
            .log()
            .all()
            .extract()
            .header(tokenHeaderName);

    // Authenticated access
    given().header(tokenHeaderName, token).when().get("/authenticated").then().statusCode(OK_CODE);

    // Authorized access
    given().header(tokenHeaderName, token).when().get("/authorized").then().statusCode(OK_CODE);

    // Gather time related claims from token
    ZoneId utc = ZoneId.of("UTC");
    Map<String, Object> claims = jwt.claimsOfToken(token);
    ZonedDateTime iat =
        ZonedDateTime.ofInstant(Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_ISSUED_AT)), utc);
    ZonedDateTime nbf =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_NOT_BEFORE)), utc);
    ZonedDateTime exp =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_EXPIRATION)), utc);

    // Wait at least one second before renewal so new dates will be different
    Thread.sleep(1200);

    // Renew token
    String renewed =
        given()
            .header(tokenHeaderName, token)
            .when()
            .post("/renew")
            .then()
            .statusCode(OK_CODE)
            .header(tokenHeaderName, notNullValue())
            .log()
            .all()
            .extract()
            .header(tokenHeaderName);

    // Gather time related claims from renewed token
    claims = jwt.claimsOfToken(renewed);
    ZonedDateTime renewedIat =
        ZonedDateTime.ofInstant(Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_ISSUED_AT)), utc);
    ZonedDateTime renewedNbf =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_NOT_BEFORE)), utc);
    ZonedDateTime renewedExp =
        ZonedDateTime.ofInstant(
            Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_EXPIRATION)), utc);

    // Assert renewed token time related claims are greater than the ones in the original token
    assertTrue(renewedIat.isAfter(iat));
    assertTrue(renewedNbf.isAfter(nbf));
    assertTrue(renewedExp.isAfter(exp));
  }