termux-app
156 строк · 5.1 Кб
1/*
2* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation. Oracle designates this
8* particular file as subject to the "Classpath" exception as provided
9* by Oracle in the LICENSE file that accompanied this code.
10*
11* This code is distributed in the hope that it will be useful, but WITHOUT
12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14* version 2 for more details (a copy is included in the LICENSE file that
15* accompanied this code).
16*
17* You should have received a copy of the GNU General Public License version
18* 2 along with this work; if not, write to the Free Software Foundation,
19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20*
21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22* or visit www.oracle.com if you need additional information or have any
23* questions.
24*/
25
26package com.termux.shared.file.filesystem;
27
28import androidx.annotation.NonNull;
29
30import java.text.SimpleDateFormat;
31import java.util.Calendar;
32import java.util.Objects;
33import java.util.concurrent.TimeUnit;
34
35/**
36* Represents the value of a file's time stamp attribute. For example, it may
37* represent the time that the file was last
38* {@link FileAttributes#lastModifiedTime() modified},
39* {@link FileAttributes#lastAccessTime() accessed},
40* or {@link FileAttributes#creationTime() created}.
41*
42* <p> Instances of this class are immutable.
43*
44* https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:libcore/ojluni/src/main/java/java/nio/file/attribute/FileTime.java
45*
46* @since 1.7
47* @see java.nio.file.Files#setLastModifiedTime
48* @see java.nio.file.Files#getLastModifiedTime
49*/
50
51public final class FileTime {
52/**
53* The unit of granularity to interpret the value. Null if
54* this {@code FileTime} is converted from an {@code Instant},
55* the {@code value} and {@code unit} pair will not be used
56* in this scenario.
57*/
58private final TimeUnit unit;
59
60/**
61* The value since the epoch; can be negative.
62*/
63private final long value;
64
65
66/**
67* The value return by toString (created lazily)
68*/
69private String valueAsString;
70
71/**
72* Initializes a new instance of this class.
73*/
74private FileTime(long value, TimeUnit unit) {
75this.value = value;
76this.unit = unit;
77}
78
79/**
80* Returns a {@code FileTime} representing a value at the given unit of
81* granularity.
82*
83* @param value
84* the value since the epoch (1970-01-01T00:00:00Z); can be
85* negative
86* @param unit
87* the unit of granularity to interpret the value
88*
89* @return a {@code FileTime} representing the given value
90*/
91public static FileTime from(long value, @NonNull TimeUnit unit) {
92Objects.requireNonNull(unit, "unit");
93return new FileTime(value, unit);
94}
95
96/**
97* Returns a {@code FileTime} representing the given value in milliseconds.
98*
99* @param value
100* the value, in milliseconds, since the epoch
101* (1970-01-01T00:00:00Z); can be negative
102*
103* @return a {@code FileTime} representing the given value
104*/
105public static FileTime fromMillis(long value) {
106return new FileTime(value, TimeUnit.MILLISECONDS);
107}
108
109/**
110* Returns the value at the given unit of granularity.
111*
112* <p> Conversion from a coarser granularity that would numerically overflow
113* saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE}
114* if positive.
115*
116* @param unit
117* the unit of granularity for the return value
118*
119* @return value in the given unit of granularity, since the epoch
120* since the epoch (1970-01-01T00:00:00Z); can be negative
121*/
122public long to(TimeUnit unit) {
123Objects.requireNonNull(unit, "unit");
124return unit.convert(this.value, this.unit);
125}
126
127/**
128* Returns the value in milliseconds.
129*
130* <p> Conversion from a coarser granularity that would numerically overflow
131* saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE}
132* if positive.
133*
134* @return the value in milliseconds, since the epoch (1970-01-01T00:00:00Z)
135*/
136public long toMillis() {
137return unit.toMillis(value);
138}
139
140@NonNull
141@Override
142public String toString() {
143return getDate(toMillis(), "yyyy.MM.dd HH:mm:ss.SSS z");
144}
145
146public static String getDate(long milliSeconds, String format) {
147try {
148Calendar calendar = Calendar.getInstance();
149calendar.setTimeInMillis(milliSeconds);
150return new SimpleDateFormat(format).format(calendar.getTime());
151} catch(Exception e) {
152return Long.toString(milliSeconds);
153}
154}
155
156}
157