TimeUtils.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.utils;

  18. import java.nio.file.attribute.FileTime;
  19. import java.time.Instant;
  20. import java.util.Date;
  21. import java.util.concurrent.TimeUnit;

  22. import org.apache.commons.io.file.attribute.FileTimes;

  23. /**
  24.  * Utility class for handling time-related types and conversions.
  25.  * <p>
  26.  * Understanding UNIX vs NTFS timestamps:
  27.  * </p>
  28.  * <ul>
  29.  * <li>A <a href="https://en.wikipedia.org/wiki/Unix_time">UNIX timestamp</a> is a primitive long starting at the UNIX Epoch on January 1st, 1970 at Coordinated
  30.  * Universal Time (UTC)</li>
  31.  * <li>An <a href="https://learn.microsoft.com/en-us/windows/win32/sysinfo/file-times">NTFS timestamp</a> is a file time is a 64-bit value that represents the
  32.  * number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).</li>
  33.  * </ul>
  34.  *
  35.  * @since 1.23
  36.  */
  37. public final class TimeUtils {

  38.     /** The amount of 100-nanosecond intervals in one millisecond. */
  39.     static final long HUNDRED_NANOS_PER_MILLISECOND = TimeUnit.MILLISECONDS.toNanos(1) / 100;

  40.     /**
  41.      * <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724290%28v=vs.85%29.aspx">Windows File Times</a>
  42.      * <p>
  43.      * A file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated
  44.      * Universal Time (UTC). This is the offset of Windows time 0 to UNIX epoch in 100-nanosecond intervals.
  45.      * </p>
  46.      */
  47.     static final long WINDOWS_EPOCH_OFFSET = -116444736000000000L;

  48.     /**
  49.      * Tests whether a FileTime can be safely represented in the standard UNIX time.
  50.      *
  51.      * <p>
  52.      * TODO ? If the FileTime is null, this method always returns true.
  53.      * </p>
  54.      *
  55.      * @param time the FileTime to evaluate, can be null.
  56.      * @return true if the time exceeds the minimum or maximum UNIX time, false otherwise.
  57.      * @deprecated use {@link FileTimes#isUnixTime(FileTime)}
  58.      */
  59.     @Deprecated
  60.     public static boolean isUnixTime(final FileTime time) {
  61.         return FileTimes.isUnixTime(time);
  62.     }

  63.     /**
  64.      * Tests whether a given number of seconds (since Epoch) can be safely represented in the standard UNIX time.
  65.      *
  66.      * @param seconds the number of seconds (since Epoch) to evaluate.
  67.      * @return true if the time can be represented in the standard UNIX time, false otherwise.
  68.      * @deprecated Use {@link FileTimes#isUnixTime(long)}
  69.      */
  70.     @Deprecated
  71.     public static boolean isUnixTime(final long seconds) {
  72.         return FileTimes.isUnixTime(seconds);
  73.     }

  74.     /**
  75.      * Converts NTFS time (100 nanosecond units since 1 January 1601) to Java time.
  76.      *
  77.      * @param ntfsTime the NTFS time in 100 nanosecond units.
  78.      * @return the Date.
  79.      * @deprecated Use {@link FileTimes#ntfsTimeToDate(long)}.
  80.      */
  81.     @Deprecated
  82.     public static Date ntfsTimeToDate(final long ntfsTime) {
  83.         return FileTimes.ntfsTimeToDate(ntfsTime);
  84.     }

  85.     /**
  86.      * Converts NTFS time (100-nanosecond units since 1 January 1601) to a FileTime.
  87.      *
  88.      * @param ntfsTime the NTFS time in 100-nanosecond units.
  89.      * @return the FileTime.
  90.      * @see FileTimes#toNtfsTime(FileTime)
  91.      * @deprecated Use {@link FileTimes#ntfsTimeToFileTime(long)}.
  92.      */
  93.     @Deprecated
  94.     public static FileTime ntfsTimeToFileTime(final long ntfsTime) {
  95.         return FileTimes.ntfsTimeToFileTime(ntfsTime);
  96.     }

  97.     /**
  98.      * Converts {@link FileTime} to a {@link Date}. If the provided FileTime is {@code null}, the returned Date is also {@code null}.
  99.      *
  100.      * @param fileTime the file time to be converted.
  101.      * @return a {@link Date} which corresponds to the supplied time, or {@code null} if the time is {@code null}.
  102.      * @see FileTimes#toFileTime(Date)
  103.      * @deprecated Use {@link FileTimes#toDate(FileTime)}.
  104.      */
  105.     @Deprecated
  106.     public static Date toDate(final FileTime fileTime) {
  107.         return FileTimes.toDate(fileTime);
  108.     }

  109.     /**
  110.      * Converts {@link Date} to a {@link FileTime}. If the provided Date is {@code null}, the returned FileTime is also {@code null}.
  111.      *
  112.      * @param date the date to be converted.
  113.      * @return a {@link FileTime} which corresponds to the supplied date, or {@code null} if the date is {@code null}.
  114.      * @see FileTimes#toDate(FileTime)
  115.      * @deprecated Use {@link FileTimes#toFileTime(Date)}.
  116.      */
  117.     @Deprecated
  118.     public static FileTime toFileTime(final Date date) {
  119.         return FileTimes.toFileTime(date);
  120.     }

  121.     /**
  122.      * Converts a {@link Date} to NTFS time.
  123.      *
  124.      * @param date the Date.
  125.      * @return the NTFS time.
  126.      * @deprecated Use {@link FileTimes#toNtfsTime(Date)}.
  127.      */
  128.     @Deprecated
  129.     public static long toNtfsTime(final Date date) {
  130.         return FileTimes.toNtfsTime(date);
  131.     }

  132.     /**
  133.      * Converts a {@link FileTime} to NTFS time (100-nanosecond units since 1 January 1601).
  134.      *
  135.      * @param fileTime the FileTime.
  136.      * @return the NTFS time in 100-nanosecond units.
  137.      * @see FileTimes#ntfsTimeToFileTime(long)
  138.      * @deprecated Use {@link FileTimes#toNtfsTime(FileTime)}.
  139.      */
  140.     @Deprecated
  141.     public static long toNtfsTime(final FileTime fileTime) {
  142.         return FileTimes.toNtfsTime(fileTime);
  143.     }

  144.     /**
  145.      * Converts Java time (milliseconds since Epoch) to NTFS time.
  146.      *
  147.      * @param javaTime the Java time.
  148.      * @return the NTFS time.
  149.      * @deprecated Use {@link FileTimes#toNtfsTime(long)}
  150.      */
  151.     @Deprecated
  152.     public static long toNtfsTime(final long javaTime) {
  153.         return FileTimes.toNtfsTime(javaTime);
  154.     }

  155.     /**
  156.      * Converts {@link FileTime} to standard UNIX time.
  157.      *
  158.      * @param fileTime the original FileTime.
  159.      * @return the UNIX timestamp.
  160.      */
  161.     public static long toUnixTime(final FileTime fileTime) {
  162.         return FileTimes.toUnixTime(fileTime);
  163.     }

  164.     /**
  165.      * Truncates a FileTime to 100-nanosecond precision.
  166.      *
  167.      * @param fileTime the FileTime to be truncated.
  168.      * @return the truncated FileTime.
  169.      */
  170.     public static FileTime truncateToHundredNanos(final FileTime fileTime) {
  171.         final Instant instant = fileTime.toInstant();
  172.         return FileTime.from(Instant.ofEpochSecond(instant.getEpochSecond(), instant.getNano() / 100 * 100));
  173.     }

  174.     /**
  175.      * Converts standard UNIX time (in seconds, UTC/GMT) to {@link FileTime}.
  176.      *
  177.      * @param time UNIX timestamp (in seconds, UTC/GMT).
  178.      * @return the corresponding FileTime.
  179.      * @deprecated Use {@link FileTimes#fromUnixTime(long)}
  180.      */
  181.     @Deprecated
  182.     public static FileTime unixTimeToFileTime(final long time) {
  183.         return FileTimes.fromUnixTime(time);
  184.     }

  185.     /** Private constructor to prevent instantiation of this utility class. */
  186.     private TimeUtils() {
  187.     }
  188. }