annotate src/org/tmatesoft/hg/core/HgDate.java @ 212:edb2e2829352 gradle

Project uses Gradle for build.
author Alexander Kitaev <kitaev@gmail.com>
date Mon, 09 May 2011 11:49:23 +0200
parents 644ee58c9f16
children 4252faa556cd
rev   line source
211
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.core;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.util.Calendar;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.util.Formatter;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.util.Locale;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.util.TimeZone;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 /**
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 * Compound object to keep time and time zone of a change. Time zone is not too useful unless you'd like to indicate where
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 * the change was made (original <em>hg</em> shows date of a change in its original time zone)
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 *
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 * @author Artem Tikhomirov
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 * @author TMate Software Ltd.
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 */
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 public final class HgDate implements Comparable<HgDate>, Cloneable {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 private final long time;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 private final TimeZone tzone;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 /**
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 * @param millis UTC, milliseconds
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 * @param timezone zone offset in seconds, UTC - local == timezone. I.e. positive in the Western Hemisphere.
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 */
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 public HgDate(long millis, int timezone) {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 time = millis;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 // @see http://pydoc.org/2.5.1/time.html time.timezone -- difference in seconds between UTC and local standard time
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 // UTC - local = timezone. local = UTC - timezone
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 // In Java, timezone is positive right of Greenwich, UTC+timezone = local
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 String[] available = TimeZone.getAvailableIDs(-timezone * 1000);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 assert available != null && available.length > 0 : String.valueOf(timezone);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 // this is sort of hach, I don't know another way how to get
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 // abbreviated name from zone offset (other than to have own mapping)
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 // And I can't use any id, because e.g. zone with id "US/Mountain"
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 // gives incorrect (according to hg cmdline) result, unlike MST or US/Arizona (all ids for zone -0700)
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 // use 1125044450000L to see the difference
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 String shortID = TimeZone.getTimeZone(available[0]).getDisplayName(false, TimeZone.SHORT);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 // XXX in fact, might need to handle daylight saving time, but not sure how,
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 // getTimeZone(GMT-timezone*1000).inDaylightTime()?
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 TimeZone tz = TimeZone.getTimeZone(shortID);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 tzone = tz;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 public long getRawTime() {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 return time;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 /**
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 * @return zone object by reference, do not alter it (make own copy by {@link TimeZone#clone()}, to modify).
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 */
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 public TimeZone getTimeZone() {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 return tzone;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 @Override
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 public String toString() {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 // format the same way hg does
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 return toString(Locale.US);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 public String toString(Locale l) {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 Calendar c = Calendar.getInstance(getTimeZone());
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 c.setTimeInMillis(getRawTime());
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 Formatter f = new Formatter(new StringBuilder(), l);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 f.format("%ta %<tb %<td %<tH:%<tM:%<tS %<tY %<tz", c);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 return f.out().toString();
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 public int compareTo(HgDate o) {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 return (int) (time - o.time);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 @Override
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 public boolean equals(Object obj) {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 if (false == obj instanceof HgDate) {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 return false;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 HgDate other = (HgDate) obj;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 return compareTo(other) == 0;
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 @Override
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 public int hashCode() {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 // copied from java.util.Datge
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 return (int) time ^ (int) (time >> 32);
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 @Override
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 protected Object clone() {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 try {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 return super.clone();
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 } catch (CloneNotSupportedException ex) {
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 throw new InternalError(ex.toString());
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 }
644ee58c9f16 Compound HgDate object to provide flexible access to change date/time information
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 }