aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/calendarcommon2/Time.java
blob: f0af248e09335886366d33e7bad04cbfeaef28e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.calendarcommon2;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Helper class to make migration out of android.text.format.Time smoother.
 */
public class Time {

    public static final String TIMEZONE_UTC = "UTC";

    private static final int EPOCH_JULIAN_DAY = 2440588;
    private static final long HOUR_IN_MILLIS = 60 * 60 * 1000;
    private static final long DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS;

    private static final String FORMAT_ALL_DAY_PATTERN = "yyyyMMdd";
    private static final String FORMAT_TIME_PATTERN = "yyyyMMdd'T'HHmmss";
    private static final String FORMAT_TIME_UTC_PATTERN = "yyyyMMdd'T'HHmmss'Z'";
    private static final String FORMAT_LOG_TIME_PATTERN = "EEE, MMM dd, yyyy hh:mm a";

    /*
     * Define symbolic constants for accessing the fields in this class. Used in
     * getActualMaximum().
     */
    public static final int SECOND = 1;
    public static final int MINUTE = 2;
    public static final int HOUR = 3;
    public static final int MONTH_DAY = 4;
    public static final int MONTH = 5;
    public static final int YEAR = 6;
    public static final int WEEK_DAY = 7;
    public static final int YEAR_DAY = 8;
    public static final int WEEK_NUM = 9;

    public static final int SUNDAY = 0;
    public static final int MONDAY = 1;
    public static final int TUESDAY = 2;
    public static final int WEDNESDAY = 3;
    public static final int THURSDAY = 4;
    public static final int FRIDAY = 5;
    public static final int SATURDAY = 6;

    private final GregorianCalendar mCalendar;

    private int year;
    private int month;
    private int monthDay;
    private int hour;
    private int minute;
    private int second;

    private int yearDay;
    private int weekDay;

    private String timezone;
    private boolean allDay;

    /**
     * Enabling this flag will apply appropriate dst transition logic when calling either
     * {@code toMillis()} or {@code normalize()} and their respective *ApplyDst() equivalents. <br>
     * When this flag is enabled, the following calls would be considered equivalent:
     * <ul>
     *     <li>{@code a.t.f.Time#normalize(true)} and {@code #normalize()}</li>
     *     <li>{@code a.t.f.Time#toMillis(true)} and {@code #toMillis()}</li>
     *     <li>{@code a.t.f.Time#normalize(false)} and {@code #normalizeApplyDst()}</li>
     *     <li>{@code a.t.f.Time#toMillis(false)} and {@code #toMillisApplyDst()}</li>
     * </ul>
     * When the flag is disabled, both {@code toMillis()} and {@code normalize()} will ignore any
     * dst transitions unless minutes or hours were added to the time (the default behavior of the
     * a.t.f.Time class). <br>
     *
     * NOTE: currently, this flag is disabled because there are no direct manipulations of the day,
     * hour, or minute fields. All of the accesses are correctly done via setters and they rely on
     * a private normalize call in their respective classes to achieve their expected behavior.
     * Additionally, using any of the {@code #set()} methods or {@code #parse()} will result in
     * normalizing by ignoring DST, which is what the default behavior is for the a.t.f.Time class.
     */
    static final boolean APPLY_DST_CHANGE_LOGIC = false;
    private int mDstChangedByField = -1;

    public Time() {
        this(TimeZone.getDefault().getID());
    }

    public Time(String timezone) {
        if (timezone == null) {
            throw new NullPointerException("timezone cannot be null.");
        }
        this.timezone = timezone;
        // Although the process's default locale is used here, #clear() will explicitly set the
        // first day of the week to MONDAY to match with the expected a.t.f.Time implementation.
        mCalendar = new GregorianCalendar(getTimeZone(), Locale.getDefault());
        clear(this.timezone);
    }

    private void readFieldsFromCalendar() {
        year = mCalendar.get(Calendar.YEAR);
        month = mCalendar.get(Calendar.MONTH);
        monthDay = mCalendar.get(Calendar.DAY_OF_MONTH);
        hour = mCalendar.get(Calendar.HOUR_OF_DAY);
        minute = mCalendar.get(Calendar.MINUTE);
        second = mCalendar.get(Calendar.SECOND);
    }

    private void writeFieldsToCalendar() {
        clearCalendar();
        mCalendar.set(year, month, monthDay, hour, minute, second);
        mCalendar.set(Calendar.MILLISECOND, 0);
    }

    private boolean isInDst() {
        return mCalendar.getTimeZone().inDaylightTime(mCalendar.getTime());
    }

    public void add(int field, int amount) {
        final boolean wasDstBefore = isInDst();
        mCalendar.add(getCalendarField(field), amount);
        if (APPLY_DST_CHANGE_LOGIC && wasDstBefore != isInDst()
                && (field == MONTH_DAY || field == HOUR || field == MINUTE)) {
            mDstChangedByField = field;
        }
    }

    public void set(long millis) {
        clearCalendar();
        mCalendar.setTimeInMillis(millis);
        readFieldsFromCalendar();
    }

    public void set(Time other) {
        clearCalendar();
        mCalendar.setTimeZone(other.getTimeZone());
        mCalendar.setTimeInMillis(other.mCalendar.getTimeInMillis());
        readFieldsFromCalendar();
    }

    public void set(int day, int month, int year) {
        clearCalendar();
        mCalendar.set(year, month, day);
        readFieldsFromCalendar();
    }

    public void set(int second, int minute, int hour, int day, int month, int year) {
        clearCalendar();
        mCalendar.set(year, month, day, hour, minute, second);
        readFieldsFromCalendar();
    }

    public long setJulianDay(int julianDay) {
        long millis = (julianDay - EPOCH_JULIAN_DAY) * DAY_IN_MILLIS;
        mCalendar.setTimeInMillis(millis);
        readFieldsFromCalendar();

        // adjust day approximation, set the time to 12am, and re-normalize
        monthDay += julianDay - getJulianDay(millis, getGmtOffset());
        hour = 0;
        minute = 0;
        second = 0;
        writeFieldsToCalendar();
        return normalize();
    }

    public static int getJulianDay(long begin, long gmtOff) {
        return android.text.format.Time.getJulianDay(begin, gmtOff);
    }

    public int getWeekNumber() {
        return mCalendar.get(Calendar.WEEK_OF_YEAR);
    }

    private int getCalendarField(int field) {
        switch (field) {
            case SECOND: return Calendar.SECOND;
            case MINUTE: return Calendar.MINUTE;
            case HOUR: return Calendar.HOUR_OF_DAY;
            case MONTH_DAY: return Calendar.DAY_OF_MONTH;
            case MONTH: return Calendar.MONTH;
            case YEAR: return Calendar.YEAR;
            case WEEK_DAY: return Calendar.DAY_OF_WEEK;
            case YEAR_DAY: return Calendar.DAY_OF_YEAR;
            case WEEK_NUM: return Calendar.WEEK_OF_YEAR;
            default:
                throw new RuntimeException("bad field=" + field);
        }
    }

    public int getActualMaximum(int field) {
        return mCalendar.getActualMaximum(getCalendarField(field));
    }

    public void switchTimezone(String timezone) {
        long msBefore = mCalendar.getTimeInMillis();
        mCalendar.setTimeZone(TimeZone.getTimeZone(timezone));
        mCalendar.setTimeInMillis(msBefore);
        mDstChangedByField = -1;
        readFieldsFromCalendar();
    }

    /**
     * @param apply whether to apply dst logic on the ms or not; if apply is true, it is equivalent
     *              to calling the normalize or toMillis APIs in a.t.f.Time with ignoreDst=false
     */
    private long getDstAdjustedMillis(boolean apply, long ms) {
        if (APPLY_DST_CHANGE_LOGIC) {
            if (apply && mDstChangedByField == MONTH_DAY) {
                return isInDst() ? (ms + HOUR_IN_MILLIS) : (ms - HOUR_IN_MILLIS);
            } else if (!apply && (mDstChangedByField == HOUR || mDstChangedByField == MINUTE)) {
                return isInDst() ? (ms - HOUR_IN_MILLIS) : (ms + HOUR_IN_MILLIS);
            }
        }
        return ms;
    }

    private long normalizeInternal() {
        final long ms = mCalendar.getTimeInMillis();
        readFieldsFromCalendar();
        return ms;
    }

    public long normalize() {
        return getDstAdjustedMillis(false, normalizeInternal());
    }

    long normalizeApplyDst() {
        return getDstAdjustedMillis(true, normalizeInternal());
    }

    public void parse(String time) {
        if (time == null) {
            throw new NullPointerException("time string is null");
        }
        parseInternal(time);
        writeFieldsToCalendar();
    }

    public String format2445() {
        writeFieldsToCalendar();
        final SimpleDateFormat sdf = new SimpleDateFormat(
                allDay ? FORMAT_ALL_DAY_PATTERN
                       : (TIMEZONE_UTC.equals(getTimezone()) ? FORMAT_TIME_UTC_PATTERN
                                                             : FORMAT_TIME_PATTERN));
        sdf.setTimeZone(getTimeZone());
        return sdf.format(mCalendar.getTime());
    }

    public long toMillis() {
        return getDstAdjustedMillis(false, mCalendar.getTimeInMillis());
    }

    long toMillisApplyDst() {
        return getDstAdjustedMillis(true, mCalendar.getTimeInMillis());
    }

    private TimeZone getTimeZone() {
        return timezone != null ? TimeZone.getTimeZone(timezone) : TimeZone.getDefault();
    }

    public int compareTo(Time other) {
        return mCalendar.compareTo(other.mCalendar);
    }

    private void clearCalendar() {
        mDstChangedByField = -1;
        mCalendar.clear();
        mCalendar.set(Calendar.HOUR_OF_DAY, 0); // HOUR_OF_DAY doesn't get reset with #clear
        mCalendar.setTimeZone(getTimeZone());
        // set fields for week number computation according to ISO 8601.
        mCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        mCalendar.setMinimalDaysInFirstWeek(4);
    }

    public void clear(String timezoneId) {
        clearCalendar();
        readFieldsFromCalendar();
        setTimezone(timezoneId);
    }

    public int getYear() {
        return mCalendar.get(Calendar.YEAR);
    }

    public void setYear(int year) {
        this.year = year;
        mCalendar.set(Calendar.YEAR, year);
    }

    public int getMonth() {
        return mCalendar.get(Calendar.MONTH);
    }

    public void setMonth(int month) {
        this.month = month;
        mCalendar.set(Calendar.MONTH, month);
    }

    public int getDay() {
        return mCalendar.get(Calendar.DAY_OF_MONTH);
    }

    public void setDay(int day) {
        this.monthDay = day;
        mCalendar.set(Calendar.DAY_OF_MONTH, day);
    }

    public int getHour() {
        return mCalendar.get(Calendar.HOUR_OF_DAY);
    }

    public void setHour(int hour) {
        this.hour = hour;
        mCalendar.set(Calendar.HOUR_OF_DAY, hour);
    }

    public int getMinute() {
        return mCalendar.get(Calendar.MINUTE);
    }

    public void setMinute(int minute) {
        this.minute = minute;
        mCalendar.set(Calendar.MINUTE, minute);
    }

    public int getSecond() {
        return mCalendar.get(Calendar.SECOND);
    }

    public void setSecond(int second) {
        this.second = second;
        mCalendar.set(Calendar.SECOND, second);
    }

    public String getTimezone() {
        return mCalendar.getTimeZone().getID();
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
        mCalendar.setTimeZone(getTimeZone());
    }

    public int getYearDay() {
        // yearDay in a.t.f.Time's implementation starts from 0, whereas Calendar's starts from 1.
        return mCalendar.get(Calendar.DAY_OF_YEAR) - 1;
    }

    public void setYearDay(int yearDay) {
        this.yearDay = yearDay;
        // yearDay in a.t.f.Time's implementation starts from 0, whereas Calendar's starts from 1.
        mCalendar.set(Calendar.DAY_OF_YEAR, yearDay + 1);
    }

    public int getWeekDay() {
        // weekDay in a.t.f.Time's implementation starts from 0, whereas Calendar's starts from 1.
        return mCalendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    public void setWeekDay(int weekDay) {
        this.weekDay = weekDay;
        // weekDay in a.t.f.Time's implementation starts from 0, whereas Calendar's starts from 1.
        mCalendar.set(Calendar.DAY_OF_WEEK, weekDay + 1);
    }

    public boolean isAllDay() {
        return allDay;
    }

    public void setAllDay(boolean allDay) {
        this.allDay = allDay;
    }

    public long getGmtOffset() {
        return mCalendar.getTimeZone().getOffset(mCalendar.getTimeInMillis()) / 1000;
    }

    private void parseInternal(String s) {
        int len = s.length();
        if (len < 8) {
            throw new IllegalArgumentException("String is too short: \"" + s +
                    "\" Expected at least 8 characters.");
        } else if (len > 8 && len < 15) {
            throw new IllegalArgumentException("String is too short: \"" + s
                    + "\" If there are more than 8 characters there must be at least 15.");
        }

        // year
        int n = getChar(s, 0, 1000);
        n += getChar(s, 1, 100);
        n += getChar(s, 2, 10);
        n += getChar(s, 3, 1);
        year = n;

        // month
        n = getChar(s, 4, 10);
        n += getChar(s, 5, 1);
        n--;
        month = n;

        // day of month
        n = getChar(s, 6, 10);
        n += getChar(s, 7, 1);
        monthDay = n;

        if (len > 8) {
            checkChar(s, 8, 'T');
            allDay = false;

            // hour
            n = getChar(s, 9, 10);
            n += getChar(s, 10, 1);
            hour = n;

            // min
            n = getChar(s, 11, 10);
            n += getChar(s, 12, 1);
            minute = n;

            // sec
            n = getChar(s, 13, 10);
            n += getChar(s, 14, 1);
            second = n;

            if (len > 15) {
                // Z
                checkChar(s, 15, 'Z');
                timezone = TIMEZONE_UTC;
            }
        } else {
            allDay = true;
            hour = 0;
            minute = 0;
            second = 0;
        }

        weekDay = 0;
        yearDay = 0;
    }

    private void checkChar(String s, int spos, char expected) {
        final char c = s.charAt(spos);
        if (c != expected) {
            throw new IllegalArgumentException(String.format(
                    "Unexpected character 0x%02d at pos=%d.  Expected 0x%02d (\'%c\').",
                    (int) c, spos, (int) expected, expected));
        }
    }

    private int getChar(String s, int spos, int mul) {
        final char c = s.charAt(spos);
        if (Character.isDigit(c)) {
            return Character.getNumericValue(c) * mul;
        } else {
            throw new IllegalArgumentException("Parse error at pos=" + spos);
        }
    }

    // NOTE: only used for outputting time to error logs
    public String format() {
        final SimpleDateFormat sdf =
                new SimpleDateFormat(FORMAT_LOG_TIME_PATTERN, Locale.getDefault());
        return sdf.format(mCalendar.getTime());
    }

    // NOTE: only used in tests
    public boolean parse3339(String time) {
        android.text.format.Time tmp = generateInstance();
        boolean success = tmp.parse3339(time);
        copyAndWriteInstance(tmp);
        return success;
    }

    // NOTE: only used in tests
    public String format3339(boolean allDay) {
        return generateInstance().format3339(allDay);
    }

    private android.text.format.Time generateInstance() {
        android.text.format.Time tmp = new android.text.format.Time(timezone);
        tmp.set(second, minute, hour, monthDay, month, year);

        tmp.yearDay = yearDay;
        tmp.weekDay = weekDay;

        tmp.timezone = timezone;
        tmp.gmtoff = getGmtOffset();
        tmp.allDay = allDay;
        tmp.set(mCalendar.getTimeInMillis());
        if (tmp.allDay && (tmp.hour != 0 || tmp.minute != 0 || tmp.second != 0)) {
            // Time SDK expects hour, minute, second to be 0 if allDay is true
            tmp.hour = 0;
            tmp.minute = 0;
            tmp.second = 0;
        }

        return tmp;
    }

    private void copyAndWriteInstance(android.text.format.Time time) {
        year = time.year;
        month = time.month;
        monthDay = time.monthDay;
        hour = time.hour;
        minute = time.minute;
        second = time.second;

        yearDay = time.yearDay;
        weekDay = time.weekDay;

        timezone = time.timezone;
        allDay = time.allDay;

        writeFieldsToCalendar();
    }
}