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