termux-app

Форк
0
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

26
package com.termux.shared.file.filesystem;
27

28
import androidx.annotation.NonNull;
29

30
import java.text.SimpleDateFormat;
31
import java.util.Calendar;
32
import java.util.Objects;
33
import 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

51
public 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
     */
58
    private final TimeUnit unit;
59

60
    /**
61
     * The value since the epoch; can be negative.
62
     */
63
    private final long value;
64
    
65

66
    /**
67
     * The value return by toString (created lazily)
68
     */
69
    private String valueAsString;
70

71
    /**
72
     * Initializes a new instance of this class.
73
     */
74
    private FileTime(long value, TimeUnit unit) {
75
        this.value = value;
76
        this.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
     */
91
    public static FileTime from(long value, @NonNull TimeUnit unit) {
92
        Objects.requireNonNull(unit, "unit");
93
        return 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
     */
105
    public static FileTime fromMillis(long value) {
106
        return 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
     */
122
    public long to(TimeUnit unit) {
123
        Objects.requireNonNull(unit, "unit");
124
        return 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
     */
136
    public long toMillis() {
137
        return unit.toMillis(value);
138
    }
139

140
    @NonNull
141
    @Override
142
    public String toString() {
143
        return getDate(toMillis(), "yyyy.MM.dd HH:mm:ss.SSS z");
144
    }
145

146
    public static String getDate(long milliSeconds, String format) {
147
        try {
148
            Calendar calendar = Calendar.getInstance();
149
            calendar.setTimeInMillis(milliSeconds);
150
            return new SimpleDateFormat(format).format(calendar.getTime());
151
        } catch(Exception e) {
152
            return Long.toString(milliSeconds);
153
        }
154
    }
155

156
}
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.