Debian Codegraph @

src/dr/evolution/util/Date.java

raw symbols SCIP
1 /*
2 * Date.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.util;
27
28 import dr.util.Attribute;
29 import dr.util.NumberFormatter;
30
31 import java.util.Calendar;
32 import java.util.TimeZone;
33
34 /**
35 * A data class.
36 *
37 * @version $Id: Date.java,v 1.26 2005/05/24 20:25:57 rambaut Exp $
38 *
39 * @author Alexei Drummond
40 * @author Andrew Rambaut
41 */
42 public class Date extends TimeScale implements Attribute {
43
44 public static final String DATE = "date";
45 private double uncertainty = 0.0;
46
47 /**
48 * Constructor for relative to origin
49 * @param time the time in units relative to origin
50 * @param units the units of the given time
51 * @param backwards true if the time is earlier than the origin
52 * @param origin the absolute origin at a Date.
53 */
54 public Date(double time, Type units, boolean backwards, java.util.Date origin) {
55 super(units, backwards, origin);
56 this.time = time;
57 }
58
59 /**
60 * Constructor for an absolute date.
61 * @param date the date
62 */
63 public Date(java.util.Date date) {
64 super(Units.Type.YEARS, false);
65 origin = -1970.0;
66 initUsingDate(date);
67 }
68
69 /**
70 * Constructor for an absolute date.
71 * @param date the date
72 * @param units the units
73 */
74 public Date(java.util.Date date, Type units) {
75 super(units, false);
76 initUsingDate(date);
77 }
78
79 /**
80 * Constructor for an absolute date with origin specified
81 * @param date the date
82 * @param units the units
83 * @param origin the origin as a date
84 */
85 public Date(java.util.Date date, Type units, java.util.Date origin) {
86 super(units, false, origin);
87 initUsingDate(date);
88 }
89
90 /**
91 * Constructor of a relative age
92 * @param time the time relative to arbitrary zero point.
93 * @param units the units the time is measured in
94 * @param backwards true of the time is earlier than the zero point.
95 */
96 public Date(double time, Type units, boolean backwards) {
97 super(units, backwards);
98 this.time = time;
99 }
100
101 /**
102 * Constructor for time a relative to origin
103 * @param origin the origin in given units from Jan 1st 1970
104 */
105 private Date(double time, Type units, boolean backwards, double origin) {
106 super(units, backwards, origin);
107 this.time = time;
108 }
109
110 //************************************************************************
111 // Factory methods
112 //************************************************************************
113
114 /**
115 * Create an age representing the given age (time ago) in the given units
116 */
117 public static Date createRelativeAge(double age, Type units) {
118 return new Date(age, units, true);
119 }
120
121 /**
122 * Create an age representing the given age (time ago) in the given units
123 * with an origin of the given date.
124 * The age represents the number units back in time from the origin.
125 */
126 public static Date createTimeAgoFromOrigin(double age, Type units, java.util.Date origin) {
127 return new Date(age, units, true, origin);
128 }
129
130 /**
131 * Create an age representing the given age (time ago) in the given units
132 * with an origin as the given number of units since 1970.
133 * The age represents the number units back in time from the origin.
134 */
135 public static Date createTimeAgoFromOrigin(double age, Type units, double origin) {
136 return new Date(age, units, true, origin);
137 }
138
139 /**
140 * Create an age representing the given age (time since) in the given units
141 * with an origin of the given date.
142 * The age represents the number units back in time from the origin.
143 */
144 public static Date createTimeSinceOrigin(double age, Type units, java.util.Date origin) {
145 return new Date(age, units, false, origin);
146 }
147
148 /**
149 * Create an age representing the given age (time since) in the given units
150 * with an origin as the given number of units since 1970.
151 * The age represents the number units forwards in time from the origin.
152 */
153 public static Date createTimeSinceOrigin(double age, Type units, double origin) {
154 return new Date(age, units, false, origin);
155 }
156
157 /**
158 * Create a date an sets units to Units.YEARS
159 */
160 public static Date createDate(java.util.Date date) {
161 return new Date(date, Units.Type.YEARS);
162 }
163
164 //************************************************************************
165 // Private methods
166 //************************************************************************
167
168 private void initUsingDate(java.util.Date date) {
169
170 // get the number of milliseconds this date is after the 1st January 1970
171 long millisAhead = date.getTime();
172
173
174 double daysAhead = ((double)millisAhead)/MILLIS_PER_DAY;
175
176 switch (units) {
177 case DAYS: time = daysAhead;
178 break;
179 case MONTHS: time = daysAhead / DAYS_PER_MONTH;
180 break;
181 case YEARS:
182 //time = daysAhead / DAYS_PER_YEAR;
183 // more precise (so 1st Jan 2013 is 2013.0)
184
185 // to avoid timezone specific differences in date calculations, all dates and calendars are
186 // set to GMT.
187 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
188 cal.setTime(date);
189
190 int year = cal.get(Calendar.YEAR);
191 long millis1 = cal.getTimeInMillis();
192
193 cal.set(year, Calendar.JANUARY, 1, 0, 0);
194 long millis2 = cal.getTimeInMillis();
195
196 cal.set(year + 1, Calendar.JANUARY, 1, 0, 0);
197 long millis3 = cal.getTimeInMillis();
198 double fractionalYear = ((double)(millis1 - millis2)) / (millis3 - millis2);
199
200 time = fractionalYear + year - 1970;
201 break;
202 default: throw new IllegalArgumentException();
203 }
204
205
206 if (time < getOrigin()) {
207 time = getOrigin() - time;
208 backwards = true;
209 } else {
210 time = time - getOrigin();
211 backwards = false;
212 }
213
214
215 }
216
217 /**
218 * Returns the time value that is relative to the origin
219 */
220 public double getTimeValue() {
221 return time;
222 }
223
224 /**
225 * Returns the absolute time value (i.e., relative to zero).
226 */
227 public double getAbsoluteTimeValue() {
228 if (isBackwards()) {
229 return getOrigin() - getTimeValue();
230 }
231 return getOrigin() + getTimeValue();
232 }
233
234 public boolean before(Date date) {
235 double newTime = convertTime(date.getTimeValue(), date);
236 if (isBackwards()) {
237 return getTimeValue() > newTime;
238 }
239 return getTimeValue() < newTime;
240 }
241
242 public boolean after(Date date) {
243 double newTime = convertTime(date.getTimeValue(), date);
244 if (isBackwards()) {
245 return getTimeValue() < newTime;
246 }
247 return getTimeValue() > newTime;
248 }
249
250 public boolean equals(Date date) {
251 double newTime = convertTime(date.getTimeValue(), date);
252 return getTimeValue() == newTime;
253 }
254
255 public String getAttributeName() { return DATE; }
256
257 public Object getAttributeValue() { return this; }
258
259 public String toString() {
260 if (isBackwards()) {
261 return formatter.format(time).trim() + " " + unitString(time) + " ago";
262 } else {
263 return formatter.format(time).trim() + " " + unitString(time);
264 }
265 }
266
267 private double time;
268
269 private NumberFormatter formatter = new NumberFormatter(5);
270
271 public void setUncertainty(double uncertainty) {
272 this.uncertainty = uncertainty;
273 }
274
275 public double getUncertainty() {
276 return uncertainty;
277 }
278 }