001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/trunk/sakai/admin-tools/su/src/java/org/sakaiproject/tool/su/SuTool.java $
003: * $Id: SuTool.java 5970 2006-02-15 03:07:19Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.time.impl;
021:
022: import java.text.ParsePosition;
023: import java.text.DateFormat;
024: import java.util.Date;
025: import java.text.SimpleDateFormat;
026: import java.util.GregorianCalendar;
027: import java.util.TimeZone;
028:
029: import org.sakaiproject.time.api.Time;
030: import org.sakaiproject.time.api.TimeBreakdown;
031: import org.sakaiproject.time.cover.TimeService;
032:
033: /**
034: * <p>
035: * MyTime is an implementation of the Time API Time.
036: * </p>
037: */
038: public class MyTime implements Time {
039: /** A fixed class serian number. */
040: private static final long serialVersionUID = 1L;
041:
042: /** The milliseconds since... same as Date */
043: protected long m_millisecondsSince = 0;
044:
045: /**
046: * construct from a string, in our format, GMT values
047: *
048: * @param str
049: * time format string
050: */
051: public MyTime(String str) {
052: // use formatter A: yyyyMMddHHmmssSSS
053: Date d = null;
054: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtA) {
055: ParsePosition pos = new ParsePosition(0);
056: d = ((BasicTimeService) TimeService.getInstance()).M_fmtA
057: .parse(str, pos);
058: }
059: m_millisecondsSince = d.getTime();
060: }
061:
062: /**
063: * construct as now
064: */
065: public MyTime() {
066: m_millisecondsSince = System.currentTimeMillis();
067: }
068:
069: /**
070: * construct from a Long
071: *
072: * @param l
073: * time value in ms since...
074: */
075: public MyTime(long l) {
076: m_millisecondsSince = l;
077: }
078:
079: /**
080: * construct from individual ints, and the zone.
081: *
082: * @param zone
083: * The time zone.
084: * @param year
085: * full year (i.e. 1999, 2000)
086: * @param month
087: * month in year (1..12)
088: * @param day
089: * day in month (1..31)
090: * @param hour
091: * hour in day (0..23)
092: * @param minuet
093: * minute in hour (0..59)
094: * @param second
095: * second in minute (0..59)
096: * @param millisecond
097: * millisecond in second (0..999)
098: */
099: public MyTime(TimeZone zone, int year, int month, int day,
100: int hour, int minute, int second, int millisecond) {
101: GregorianCalendar cal = ((BasicTimeService) TimeService
102: .getInstance()).getCalendar(zone, year, month - 1, day,
103: hour, minute, second, millisecond);
104: m_millisecondsSince = cal.getTimeInMillis();
105: }
106:
107: /**
108: * construct from time breakdown, and the zone.
109: *
110: * @param zone
111: * The time zone.
112: * @param tb
113: * The TimeBreakdown with the values.
114: */
115: public MyTime(TimeZone zone, TimeBreakdown tb) {
116: GregorianCalendar cal = ((BasicTimeService) TimeService
117: .getInstance()).getCalendar(zone, tb.getYear(), tb
118: .getMonth() - 1, tb.getDay(), tb.getHour(),
119: tb.getMin(), tb.getSec(), tb.getMs());
120: m_millisecondsSince = cal.getTimeInMillis();
121: }
122:
123: /**
124: * {@inheritDoc}
125: */
126: public Object clone() {
127: return new MyTime(m_millisecondsSince);
128: }
129:
130: /**
131: * {@inheritDoc}
132: */
133: public String toString() {
134: String s = null;
135: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtA) {
136: // format
137: s = ((BasicTimeService) TimeService.getInstance()).M_fmtA
138: .format(new Date(getTime()));
139: }
140:
141: return s;
142: }
143:
144: /**
145: * {@inheritDoc}
146: */
147: public String toStringSql() {
148: String s = null;
149: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtE) {
150: // format
151: s = ((BasicTimeService) TimeService.getInstance()).M_fmtE
152: .format(new Date(getTime()));
153: }
154:
155: return s;
156: }
157:
158: /**
159: * {@inheritDoc}
160: */
161: public String toStringLocal() {
162: String s = null;
163: DateFormat fmtAl = ((BasicTimeService) TimeService
164: .getInstance())
165: .getLocalTzFormat(((BasicTimeService) TimeService
166: .getInstance()).getUserTimezoneLocale()).M_fmtAl;
167: synchronized (fmtAl) {
168: // format
169: s = fmtAl.format(new Date(getTime()));
170: }
171:
172: return s;
173: }
174:
175: /**
176: * {@inheritDoc}
177: */
178: public String toStringGmtFull() {
179: String s = null;
180: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtB) {
181: // format
182: s = ((BasicTimeService) TimeService.getInstance()).M_fmtB
183: .format(new Date(getTime()));
184: }
185:
186: // lower the case of AM/PM
187: s = fix(s);
188:
189: return s;
190: }
191:
192: /**
193: * {@inheritDoc}
194: */
195: public String toStringLocalFull() {
196: String s = null;
197: DateFormat fmtBl = ((BasicTimeService) TimeService
198: .getInstance())
199: .getLocalTzFormat(((BasicTimeService) TimeService
200: .getInstance()).getUserTimezoneLocale()).M_fmtBl;
201: synchronized (fmtBl) {
202: // format
203: s = fmtBl.format(new Date(getTime()));
204: }
205:
206: // lower the case of AM/PM
207: s = fix(s);
208:
209: return s;
210: }
211:
212: /**
213: * {@inheritDoc}
214: */
215: public String toStringLocalFullZ() {
216: String s = null;
217: DateFormat fmtBlz = ((BasicTimeService) TimeService
218: .getInstance())
219: .getLocalTzFormat(((BasicTimeService) TimeService
220: .getInstance()).getUserTimezoneLocale()).M_fmtBlz;
221: synchronized (fmtBlz) {
222: // format
223: s = fmtBlz.format(new Date(getTime()));
224: }
225:
226: return s;
227: }
228:
229: /**
230: * {@inheritDoc}
231: */
232: public String toStringGmtShort() {
233: String s = null;
234: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtC) {
235: // format
236: s = ((BasicTimeService) TimeService.getInstance()).M_fmtC
237: .format(new Date(getTime()));
238: }
239:
240: // lower the case of AM/PM
241: s = fix(s);
242:
243: return s;
244: }
245:
246: /**
247: * {@inheritDoc}
248: */
249: public String toStringLocalShort() {
250: String s = null;
251: DateFormat fmtCl = ((BasicTimeService) TimeService
252: .getInstance())
253: .getLocalTzFormat(((BasicTimeService) TimeService
254: .getInstance()).getUserTimezoneLocale()).M_fmtCl;
255: synchronized (fmtCl) {
256: // format
257: s = fmtCl.format(new Date(getTime()));
258: }
259:
260: // lower the case of AM/PM
261: s = fix(s);
262:
263: return s;
264: }
265:
266: /**
267: * {@inheritDoc}
268: */
269: public String toStringGmtTime() {
270: String s = null;
271: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtC) {
272: // format
273: s = ((BasicTimeService) TimeService.getInstance()).M_fmtC
274: .format(new Date(getTime()));
275: }
276:
277: // lower the case of AM/PM
278: s = fix(s);
279:
280: return s;
281: }
282:
283: /**
284: * {@inheritDoc}
285: */
286: public String toStringLocalTime() {
287: String s = null;
288: DateFormat fmtCl = ((BasicTimeService) TimeService
289: .getInstance())
290: .getLocalTzFormat(((BasicTimeService) TimeService
291: .getInstance()).getUserTimezoneLocale()).M_fmtCl;
292: synchronized (fmtCl) {
293: // format
294: s = fmtCl.format(new Date(getTime()));
295: }
296:
297: // lower the case of AM/PM
298: s = fix(s);
299:
300: return s;
301: }
302:
303: /**
304: * {@inheritDoc}
305: */
306: public String toStringLocalTimeZ() {
307:
308: String s = null;
309: DateFormat fmtClz = ((BasicTimeService) TimeService
310: .getInstance())
311: .getLocalTzFormat(((BasicTimeService) TimeService
312: .getInstance()).getUserTimezoneLocale()).M_fmtClz;
313:
314: synchronized (fmtClz) {
315: // format
316: s = fmtClz.format(new Date(getTime()));
317: }
318: return s;
319: }
320:
321: /**
322: * {@inheritDoc}
323: */
324: public String toStringLocalTime24() {
325: String s = null;
326: DateFormat fmtFl = ((BasicTimeService) TimeService
327: .getInstance())
328: .getLocalTzFormat(((BasicTimeService) TimeService
329: .getInstance()).getUserTimezoneLocale()).M_fmtFl;
330: synchronized (fmtFl) {
331: // format
332: s = fmtFl.format(new Date(getTime()));
333: }
334:
335: return s;
336: }
337:
338: /**
339: * {@inheritDoc}
340: */
341: public String toStringGmtDate() {
342: String s = null;
343: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtD) {
344: // format
345: s = ((BasicTimeService) TimeService.getInstance()).M_fmtD
346: .format(new Date(getTime()));
347: }
348:
349: return s;
350: }
351:
352: /**
353: * {@inheritDoc}
354: */
355: public String toStringLocalDate() {
356: String s = null;
357: DateFormat fmtDl = ((BasicTimeService) TimeService
358: .getInstance())
359: .getLocalTzFormat(((BasicTimeService) TimeService
360: .getInstance()).getUserTimezoneLocale()).M_fmtDl;
361: synchronized (fmtDl) {
362: // format
363: s = fmtDl.format(new Date(getTime()));
364: }
365:
366: return s;
367: }
368:
369: /**
370: * {@inheritDoc}
371: */
372: public String toStringLocalShortDate() {
373: String s = null;
374: DateFormat fmtD2 = ((BasicTimeService) TimeService
375: .getInstance())
376: .getLocalTzFormat(((BasicTimeService) TimeService
377: .getInstance()).getUserTimezoneLocale()).M_fmtD2;
378: synchronized (fmtD2) {
379: // format
380: s = fmtD2.format(new Date(getTime()));
381: }
382:
383: return s;
384: }
385:
386: public static SimpleDateFormat RFC822DATEFORMAT
387: // = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
388: = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z");
389:
390: /**
391: * {@inheritDoc}
392: */
393: public String toStringRFC822Local() {
394: return RFC822DATEFORMAT.format(new Date(getTime()));
395: }
396:
397: /**
398: * {@inheritDoc}
399: */
400: public String toStringFilePath() {
401: String s = null;
402: synchronized (((BasicTimeService) TimeService.getInstance()).M_fmtG) {
403: // format
404: s = ((BasicTimeService) TimeService.getInstance()).M_fmtG
405: .format(new Date(getTime()));
406: }
407:
408: return s;
409: }
410:
411: /**
412: * {@inheritDoc}
413: */
414: public boolean equals(Object obj) {
415: boolean equals = false;
416:
417: if (obj instanceof MyTime) {
418: equals = (((MyTime) obj).m_millisecondsSince == m_millisecondsSince);
419: }
420:
421: return equals;
422: }
423:
424: /**
425: * {@inheritDoc}
426: */
427: public int compareTo(Object o) {
428: return (m_millisecondsSince < ((MyTime) o).m_millisecondsSince ? -1
429: : (m_millisecondsSince > ((MyTime) o).m_millisecondsSince ? 1
430: : 0));
431: }
432:
433: /**
434: * {@inheritDoc}
435: */
436: public void setTime(long l) {
437: m_millisecondsSince = l;
438: }
439:
440: /**
441: * {@inheritDoc}
442: */
443: public long getTime() {
444: return m_millisecondsSince;
445: }
446:
447: /**
448: * {@inheritDoc}
449: */
450: public boolean before(Time other) {
451: return (m_millisecondsSince < ((MyTime) other).m_millisecondsSince);
452: }
453:
454: /**
455: * {@inheritDoc}
456: */
457: public boolean after(Time other) {
458: return (m_millisecondsSince > ((MyTime) other).m_millisecondsSince);
459: }
460:
461: /**
462: * Fix the AM/PM format of the time string - lower the case.
463: *
464: * @param s
465: * The time string.
466: * @return The time string fixed.
467: */
468: protected String fix(String s) {
469: // if the last two chars are either AM or PM, change to "am" or "pm"
470: int len = s.length();
471: if (s.endsWith("PM"))
472: return s.substring(0, len - 2) + "pm";
473: else if (s.endsWith("AM"))
474: return s.substring(0, len - 2) + "am";
475: else
476: return s;
477: }
478:
479: /**
480: * {@inheritDoc}
481: */
482: public TimeBreakdown breakdownGmt() {
483: String s = toString();
484: TimeBreakdown b = ((BasicTimeService) TimeService.getInstance())
485: .newTimeBreakdown(Integer.parseInt(s.substring(0, 4)),
486: Integer.parseInt(s.substring(4, 6)), Integer
487: .parseInt(s.substring(6, 8)), Integer
488: .parseInt(s.substring(8, 10)), Integer
489: .parseInt(s.substring(10, 12)), Integer
490: .parseInt(s.substring(12, 14)), Integer
491: .parseInt(s.substring(14)));
492:
493: return b;
494: }
495:
496: /**
497: * {@inheritDoc}
498: */
499: public TimeBreakdown breakdownLocal() {
500: String s = toStringLocal();
501: TimeBreakdown b = ((BasicTimeService) TimeService.getInstance())
502: .newTimeBreakdown(Integer.parseInt(s.substring(0, 4)),
503: Integer.parseInt(s.substring(4, 6)), Integer
504: .parseInt(s.substring(6, 8)), Integer
505: .parseInt(s.substring(8, 10)), Integer
506: .parseInt(s.substring(10, 12)), Integer
507: .parseInt(s.substring(12, 14)), Integer
508: .parseInt(s.substring(14)));
509:
510: return b;
511: }
512:
513: /**
514: * {@inheritDoc}
515: */
516: public String getDisplay() {
517: return this.toStringLocalFull();
518: }
519: }
|