PageRenderTime 39ms CodeModel.GetById 12ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/core/time.d

http://github.com/wilkie/djehuty
D | 234 lines | 142 code | 53 blank | 39 comment | 10 complexity | 59a34d809069642f3a6d761b73e32b17 MD5 | raw file
  1module core.time;
  2
  3import Scaffold = scaffold.time;
  4
  5import core.string;
  6import core.definitions;
  7import core.timezone;
  8
  9import io.console;
 10
 11// Section: Types
 12// Description: This struct stores a description of time.
 13class Time {
 14
 15	// Description: This will construct a Time object that will represent midnight.
 16	this() {
 17	}
 18
 19	// Description: This will construct a Time object that will represent the
 20	//   amount of time equal to the number of microseconds given by the
 21	//   parameter.
 22	// microseconds: The number of microseconds to be represented.
 23	this(long microseconds) {
 24		_micros = microseconds;
 25	}
 26
 27	// Description: This will construct a Time object that will represent the
 28	//   time given by each parameter such that it will represent
 29	//   hour:minute:second.microsecond
 30	// hour: The hours to be represented by this Time object.
 31	// minute: The minutes to be represented by this Time object.
 32	// second: The seconds to be represented by this Time object.
 33	// microsecond: The microseconds to be represented by this Time object.
 34	this(long hour, long minute, long second, long microsecond = 0) {
 35		_micros = hour;
 36		_micros *= 60;
 37		_micros += minute;
 38		_micros *= 60;
 39		_micros += second;
 40		_micros *= 1000000;
 41		_micros += microsecond;
 42	}
 43
 44	// Description: This will return a Time object that represents the coordinated
 45	//   universal time (UTC) assumed by the system.
 46	// Returns: A Time class that represents the system's idea of the current universal time.
 47	static Time Now() {
 48		return new Time(Scaffold.TimeGet());
 49	}
 50
 51	// Description: This will return a Time object that represents the local time
 52	//   according to the given TimeZone. If a TimeZone is not given, the local
 53	//   time of the current locale is returned.
 54	// localTZ: The TimeZone to use to infer the localized time from the UTC.
 55	// Returns: A Time class that represents the local time.
 56	static Time Local(TimeZone localTZ = null) {
 57		Time ret = new Time(Scaffold.TimeGet());
 58
 59		// Get the local timezone if one was not specified.
 60		if (localTZ is null) {
 61			localTZ = new TimeZone();
 62		}
 63		
 64		ret._micros += localTZ.utcOffset;
 65
 66		// Make sure it is within a day
 67		// I am sure this breaks some leap second business
 68		if (ret._micros < 0) {
 69			ret._micros += (24L * 60L * 60L * 1000000L);
 70		}
 71		return ret;
 72	}
 73
 74	// Description: This will give the floored number of hours this Time
 75	//   object represents.
 76	long hours() {
 77		long h, ms, s, m;
 78		long tmp = _micros;
 79
 80		tmp /= 1000000;
 81		tmp /= 60;
 82		tmp /= 60;
 83
 84		return tmp;
 85	}
 86
 87	void hours(long value) {
 88		_micros = value * 60L * 60L * 1000000L;
 89	}
 90	
 91	// Description: This will give the floored number of seconds this Time
 92	//   object represents.
 93	long seconds() {
 94		long h, ms, s, m;
 95		long tmp = _micros;
 96
 97		tmp /= 1000000;
 98
 99		return tmp;
100	}
101
102	void seconds(long value) {
103		_micros = value * 1000000L;
104	}
105
106	// Description: This will give the floored number of minutes this Time
107	//   object represents.
108	long minutes() {
109		long h, ms, s, m;
110		long tmp = _micros;
111
112		tmp /= 1000000;
113		tmp /= 60;
114
115		return tmp;
116	}
117
118	void minutes(long value) {
119		_micros = value * 60L * 1000000L;
120	}
121
122	// Description: This will give the floored number of milliseconds this Time
123	//   object represents.
124	long milliseconds() {
125		long h, ms, s, m;
126		long tmp = _micros;
127
128		return tmp / 1000;
129	}
130
131	void milliseconds(long value) {
132		_micros = value * 1000L;
133	}
134
135	// Description: This will give the floored number of microseconds this Time
136	//   object represents.
137	long microseconds() {
138		return _micros;
139	}
140
141	void microseconds(long value) {
142		_micros = value;
143	}
144	
145	// comparator functions
146	int opCmp(Time o) {
147		return cast(int)(_micros - o._micros);
148	}
149
150	int opEquals(Time o) {
151		return cast(int)(o._micros == _micros);
152	}
153
154	// string functions
155
156	string toString() {
157		long h, ms, s, m;
158		long tmp = _micros;
159
160		string str = "";
161
162		if (tmp < 0) {
163			tmp *= -1;
164		}
165
166		ms = (tmp % 1000000) / 1000;
167		tmp /= 1000000;
168
169		s = tmp % 60;
170		tmp /= 60;
171
172		m = tmp % 60;
173		tmp /= 60;
174
175		h = tmp;
176
177		if (_micros < 0) {
178			str ~= "-";
179		}
180
181		if (h < 10) {
182			str ~= "0";
183		}
184		str ~= toStr(h, ":");
185
186		if (m < 10) {
187			str ~= "0";
188		}
189		str ~= toStr(m, ":");
190
191		if (s < 10) {
192			str ~= "0";
193		}
194		str ~= toStr(s, ".");
195
196		if (ms < 100) {
197			str ~= "0";
198		}
199		if (ms < 10) {
200			str ~= "0";
201		}
202		str ~= toStr(ms);
203
204		return str;
205	}
206
207	// mathematical functions
208	Time opAdd(Time o) {
209		Time ret = new Time();
210		ret._micros = _micros + o._micros;
211
212		return ret;
213	}
214
215	Time opSub(Time o) {
216		Time ret = new Time();
217		ret._micros = _micros - o._micros;
218
219		return ret;
220	}
221
222	void opAddAssign(Time o) {
223		_micros += o._micros;
224	}
225
226	void opSubAssign(Time o) {
227		_micros -= o._micros;
228	}
229
230protected:
231
232	// Description: The microsecond.
233	long _micros;
234}