All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zipkin2.internal.DateUtil Maven / Gradle / Ivy

There is a newer version: 3.4.2
Show newest version
/*
 * Copyright The OpenZipkin Authors
 * SPDX-License-Identifier: Apache-2.0
 */
package zipkin2.internal;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public final class DateUtil {
  static final TimeZone UTC = TimeZone.getTimeZone("UTC");

  /** For bucketed data floored to the day. For example, dependency links. */
  public static long midnightUTC(long epochMillis) {
    Calendar day = Calendar.getInstance(UTC);
    day.setTimeInMillis(epochMillis);
    day.set(Calendar.MILLISECOND, 0);
    day.set(Calendar.SECOND, 0);
    day.set(Calendar.MINUTE, 0);
    day.set(Calendar.HOUR_OF_DAY, 0);
    return day.getTimeInMillis();
  }

  public static List epochDays(long endTs, long lookback) {
    long to = midnightUTC(endTs);
    long startMillis = endTs - (lookback != 0 ? lookback : endTs);
    long from = startMillis <= 0 ? 0 : midnightUTC(startMillis); // >= 1970

    List days = new ArrayList<>();
    for (long time = from; time <= to; time += TimeUnit.DAYS.toMillis(1)) {
      days.add(time);
    }
    return days;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy