001: /* TimeZones.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Jun 12 12:17:03 2006, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: }}IS_RIGHT
016: */
017: package org.zkoss.util;
018:
019: import java.util.TimeZone;
020:
021: /**
022: * Utilities to access time-zone.
023: *
024: * @author tomyeh
025: */
026: public class TimeZones {
027: private static final InheritableThreadLocal _thdTZone = new InheritableThreadLocal();
028:
029: /** Returns the current time zone; never null.
030: * This is the time zone that every other objects shall use,
031: * unless they have special consideration.
032: *
033: * <p>Default: If {@link #setThreadLocal} was called with non-null,
034: * the value is returned. Otherwise, TimeZone.getDefault() is returned,
035: */
036: public static final TimeZone getCurrent() {
037: final TimeZone l = (TimeZone) _thdTZone.get();
038: return l != null ? l : TimeZone.getDefault();
039: }
040:
041: /**
042: * Sets the time-zone for the current thread only.
043: *
044: * <p>Each thread could have an independent time zone, called
045: * the thread time zone.
046: *
047: * <p>When Invoking this method under a thread that serves requests,
048: * remember to clean up the setting upon completing each request.
049: *
050: * <pre><code>TimeZone old = TimeZones.setThreadLocal(newValue);
051: *try {
052: * ...
053: *} finally {
054: * TimeZones.setThreadLocal(old);
055: *}</code></pre>
056: *
057: * @param timezone the thread time zone; null to denote no thread time zone
058: * (and the system's timezone will be used instead)
059: * @return the previous thread time zone, or null if no previous time zone
060: */
061: public static final TimeZone setThreadLocal(TimeZone timezone) {
062: final TimeZone old = (TimeZone) _thdTZone.get();
063: _thdTZone.set(timezone);
064: return old;
065: }
066:
067: /**
068: * Returns the time zone defined by {@link #setThreadLocal}.
069: *
070: * @since 3.0.0
071: * @see #getCurrent
072: */
073: public static final TimeZone getThreadLocal() {
074: return (TimeZone) _thdTZone.get();
075: }
076:
077: /** Returns the time by specifying the offset in minutes.
078: *
079: * <p>For example, the following are equivalent.
080: *<pre><code>
081: *TimeZone.getTimeZone("GMT+8");
082: *TimeZones.getTimeZone(480);
083: *</code></pre>
084: *
085: * @param ofsmins the offset in minutes.
086: */
087: public static final TimeZone getTimeZone(int ofsmins) {
088: final StringBuffer sb = new StringBuffer(8).append("GMT");
089: if (ofsmins >= 0) {
090: sb.append('+');
091: } else {
092: sb.append('-');
093: ofsmins = -ofsmins;
094: }
095: final int hr = ofsmins / 60, min = ofsmins % 60;
096: if (min == 0) {
097: sb.append(hr);
098: } else {
099: if (hr < 10)
100: sb.append('0');
101: sb.append(hr).append(':');
102: if (min < 10)
103: sb.append('0');
104: sb.append(min);
105: }
106: return TimeZone.getTimeZone(sb.toString());
107: }
108: }
|