PageRenderTime 19ms CodeModel.GetById 7ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/platform/unix/scaffold/time.d

http://github.com/wilkie/djehuty
D | 160 lines | 119 code | 27 blank | 14 comment | 9 complexity | 02aa7ffdc7b16df8e008ec2ed94558df MD5 | raw file
  1/*
  2 * time.d
  3 *
  4 * This Scaffold holds the Time implementations for the Linux platform
  5 *
  6 * Author: Dave Wilkinson
  7 * Originated: May 19th, 2009
  8 *
  9 */
 10
 11module scaffold.time;
 12
 13import platform.unix.common;
 14
 15import core.definitions;
 16import core.string;
 17import core.date;
 18import core.time;
 19
 20// Timing
 21private {
 22	struct tm {
 23		int tm_sec;
 24		int tm_min;
 25		int tm_hour;
 26		int tm_mday;
 27		int tm_mon;
 28		int tm_year;
 29		int tm_wday;
 30		int tm_yday;
 31		int tm_isdst;
 32		char* tm_zone;
 33		int tm_gmtoff;
 34	}
 35
 36	extern(C) tm* gmtime(time_t* tim);
 37	extern(C) tm* gmtime_r(time_t* tim, tm* output);
 38	extern(C) tm* localtime(time_t* tim);
 39	extern(C) tm* localtime_r(time_t* tim, tm* output);
 40	extern(C) size_t strftime(char*, size_t, char*, tm*);
 41	extern(C) void* memcpy(void*, void*, size_t);
 42
 43	extern(C) time_t mktime(tm*);
 44
 45	extern(C) void tzset();
 46
 47	extern(C) {
 48		extern char* tzname[2];
 49	}
 50}
 51
 52string TimeZoneGet() {
 53	int foo;
 54	timeval val;
 55	gettimeofday(&val, null);
 56
 57	tm LOCAL;
 58	tm GMT;
 59	gmtime_r(cast(time_t*)&val.tv_sec, &GMT);
 60	localtime_r(cast(time_t*)&val.tv_sec, &LOCAL);
 61
 62	// Get GMT difference
 63	// Which is earliest?
 64	time_t gmt_time = mktime(&GMT);
 65	time_t local_time = mktime(&LOCAL);
 66	
 67	time_t diff = local_time - gmt_time;
 68	int diff_min = diff % (60*60);
 69	int diff_hr = diff / 60 / 60;
 70
 71	char[] strret = new char[128];
 72	strret[0..3] = "UTC";
 73	size_t len = sprintf(&strret[3], "%d", diff_hr);
 74	strret[3+len] = ':';
 75	len+=4;
 76	if (diff_min < 10) {
 77		strret[len] = '0';
 78		len++;
 79	}
 80	
 81	if (diff_min > 0) {
 82		len = sprintf(&strret[len], "%d", diff_min);
 83	}
 84	else {
 85		strret[len] = '0';
 86		len++;
 87	}
 88	strret = strret[0..len];
 89
 90	if (tzname[0] !is null) {
 91		len = strlen(tzname[0]);
 92		char[] tzstr = new char[len];
 93		tzstr[0..tzstr.length] = tzname[0][0..tzstr.length];
 94
 95		switch(tzstr[0..len]) {
 96			case "EST":
 97				if (strret == "UTC-5:00") {
 98					return "Eastern Standard Time";
 99				}
100				break;
101			case "PST":
102				if (strret == "UTC-8:00") {
103					return "Pacific Standard Time";
104				}
105				break;
106			default:
107				return strret;
108		}
109	}
110	return strret;
111}
112
113// getting microseconds
114long TimeGet() {
115	timeval val;
116	gettimeofday(&val, null);
117
118	tm time_struct;
119	gmtime_r(cast(time_t*)&val.tv_sec, &time_struct);
120
121	// get microseconds
122	long micros;
123	micros = time_struct.tm_sec + (time_struct.tm_min * 60);
124	micros += time_struct.tm_hour * 60 * 60;
125	micros *= 1000000;
126	micros += val.tv_usec;
127	return micros;
128}
129
130ulong SystemTimeGet() {
131	timeval val;
132	gettimeofday(&val, null);
133	return (val.tv_sec * 1000000) + val.tv_usec;
134}
135
136float gettimeofday_difference(timeval *b, timeval *a) {
137	timeval diff_tv;
138
139	diff_tv.tv_usec = b.tv_usec - a.tv_usec;
140	diff_tv.tv_sec = b.tv_sec - a.tv_sec;
141
142	if (a.tv_usec > b.tv_usec) {
143		diff_tv.tv_usec += 1000000;
144		diff_tv.tv_sec--;
145	}
146
147	return cast(float)diff_tv.tv_sec + (cast(float)diff_tv.tv_usec / 1000000.0);
148}
149
150void DateGet(out Month month, out uint day, out uint year) {
151	timeval val;
152	gettimeofday(&val, null);
153
154	tm time_struct;
155	gmtime_r(cast(time_t*)&val.tv_sec, &time_struct);
156
157	month = cast(Month)time_struct.tm_mon;
158	day = time_struct.tm_mday;
159	year = time_struct.tm_year + 1900;
160}