PageRenderTime 41ms CodeModel.GetById 16ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/core/timezone.d

http://github.com/wilkie/djehuty
D | 93 lines | 62 code | 15 blank | 16 comment | 4 complexity | 7c7ffdd1fcda8cbf1ff6634a23bc6f03 MD5 | raw file
 1/*
 2 * timezone.d
 3 *
 4 * This module implements the TimeZone class. This class can depict difference
 5 * timezones and can be used to translate Time into a localized value.
 6 *
 7 * Author: Dave Wilkinson
 8 * Originated: January 1st, 2010
 9 *
10 */
11
12module core.timezone;
13
14import Scaffold = scaffold.time;
15
16import core.definitions;
17import core.string;
18
19import io.console;
20
21class TimeZone {
22
23	// Description: This will construct a TimeZone for the current location.
24	this() {
25		this(Scaffold.TimeZoneGet());
26	}
27
28	// Description: This will construct a specific TimeZone for a specific
29	//   timezone name or a string containing a custom timezone.
30	// name: A full name, or "GMT+h:mm" or "GMT-h:mm" where h and mm
31	//   represent the hour and minutes from GMT respectively.
32	this(string name) {
33		_name = name;
34		if (name[0..3] == "UTC") {
35			_utc = name;
36			string foo = name[3..$];
37			int pos = foo.find(":");
38			if (pos < 0) { pos = foo.length; }
39			foo = foo[0..pos];
40			int diff_hr;
41			int diff_min;
42			foo.nextInt(diff_hr);
43			foo = name[3+pos+1..$];
44			foo.nextInt(diff_min);
45			_offset_in_minutes = (diff_hr * 60) + diff_min;
46		}
47		else {
48			switch(name) {
49				case "Eastern Standard Time":
50					_offset_in_minutes = -(5 * 60);
51					break;
52				case "Pacific Standard Time":
53					_offset_in_minutes = -(8 * 60);
54					break;
55				case "Coordinated Universal Time":
56				case "Temps Universel Coordonné":
57				case "Greenwich Mean Time":
58					_offset_in_minutes = 0;
59					break;
60				default:
61					break;
62			}
63			int diff_hr = _offset_in_minutes / 60;
64			int diff_min = _offset_in_minutes % 60;
65			_utc = "UTC" ~ toStr(diff_hr) ~ ":" ~ (diff_min < 10 ? "0" : "") ~ toStr(diff_min);
66		}
67	}
68
69	string name() {
70		return _name;
71	}
72
73	string utcName() {
74		return _utc;
75	}
76
77	string toString() {
78		return utcName;
79	}
80
81	// Description: This will return the offset from UTC in microseconds.
82	long utcOffset() {
83		return cast(long)_offset_in_minutes * 60L * 1000000L;
84	}
85
86protected:
87
88	string _name = "";
89	string _utc = "";
90	int _offset_in_minutes;
91}
92
93