Sample: cleaning up Inheritance
[io/quag.git] / libs / basekit / source / Date.c
blobc3dbe8ea16510688157a0bc259e1fc1045eb8954
1 /*#io
2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
4 */
6 #define DATE_C
7 #include "Date.h"
8 #undef DATE_C
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <time.h>
13 #include <math.h>
14 #include "PortableStrptime.h"
15 #include "UArray.h"
17 Date *Date_new(void)
19 Date *self = (Date *)io_calloc(1, sizeof(Date));
20 Date_now(self);
21 return self;
24 void Date_copy_(Date *self, const Date *other)
26 memcpy(self, other, sizeof(Date));
29 void Date_free(Date *self)
31 io_free(self);
34 int Date_compare(const Date *self, const Date *other)
36 double s1 = Date_asSeconds(self);
37 double s2 = Date_asSeconds(other);
39 if (s1 == s2)
41 return 0;
44 return s1 > s2 ? 1 : -1;
47 double Date_SecondsFrom1970ToNow(void)
49 double s, us;
50 struct timeval timeval;
51 struct timezone timezone;
53 gettimeofday(&timeval, &timezone);
54 s = timeval.tv_sec;
55 //s -= timezone.tz_minuteswest * 60;
56 us = timeval.tv_usec;
58 return s + (us/1000000.0); /* + (60*60);*/
61 void Date_now(Date *self)
63 double s, us;
64 struct timeval timeval;
65 struct timezone timezone;
67 gettimeofday(&timeval, &timezone);
68 s = timeval.tv_sec;
69 s -= timezone.tz_minuteswest * 60;
70 us = timeval.tv_usec;
72 self->tv = timeval;
73 self->tz = timezone;
76 double Date_Clock(void)
78 return ((double)clock())/((double)CLOCKS_PER_SEC);
81 // zone --------------------------------------------------------
83 void Date_setToLocalTimeZone(Date *self)
85 struct timeval timeval;
86 gettimeofday(&timeval, &(self->tz));
89 struct timezone Date_timeZone(const Date *self)
91 return self->tz;
94 void Date_setTimeZone_(Date *self, struct timezone tz)
96 self->tz = tz;
99 void Date_convertToTimeZone_(Date *self, struct timezone tz)
101 double s = Date_asSeconds(self) +
102 ((self->tz.tz_minuteswest - tz.tz_minuteswest) * 60);
103 Date_fromSeconds_(self, s);
104 Date_setTimeZone_(self, tz);
107 // time --------------------------------------------------------
109 void Date_fromLocalTime_(Date *self, struct tm *t)
111 Date_fromTime_(self, mktime(t));
114 void Date_fromTime_(Date *self, time_t t)
116 Date_fromSeconds_(self, (double)t);
119 time_t Date_asTime(const Date *self)
121 return (time_t)self->tv.tv_sec;
124 // sconds --------------------------------------------------------
126 double Date_asSeconds(const Date *self)
128 return ((double)self->tv.tv_sec) + (((double)self->tv.tv_usec) / 1000000.0);
131 void Date_fromSeconds_(Date *self, double s)
133 long secs = s;
134 self->tv.tv_sec = secs;
135 self->tv.tv_usec = (s - secs)*1000000;
138 void Date_addSeconds_(Date *self, double s)
140 long secs = s;
141 self->tv.tv_sec += secs;
142 self->tv.tv_usec += (s - secs)*1000000;
145 double Date_secondsSince_(const Date *self, const Date *other)
147 return Date_asSeconds(self) - Date_asSeconds(other);
150 // components --------------------------------------------------------
152 long Date_year(const Date *self)
154 time_t t = self->tv.tv_sec;
155 struct tm *tm = localtime(&t);
156 return tm->tm_year + 1900;
159 void Date_setYear_(Date *self, long v)
161 time_t t = self->tv.tv_sec;
162 struct tm *tm = localtime(&t);
163 tm->tm_year = v - 1900;
164 self->tv.tv_sec = mktime(tm);
167 int Date_month(const Date *self)
169 time_t t = self->tv.tv_sec;
170 struct tm *tm = localtime(&t);
171 return tm->tm_mon;
174 void Date_setMonth_(Date *self, int v)
176 time_t t = self->tv.tv_sec;
177 struct tm *tm = localtime(&t);
178 tm->tm_mon = v;
179 self->tv.tv_sec = mktime(tm);
182 int Date_day(const Date *self)
184 time_t t = self->tv.tv_sec;
185 struct tm *tm = localtime(&t);
186 return tm->tm_mday;
189 void Date_setDay_(Date *self, int v)
191 time_t t = self->tv.tv_sec;
192 struct tm *tm = localtime(&t);
193 tm->tm_mday = v;
194 self->tv.tv_sec = mktime(tm);
197 int Date_hour(const Date *self)
199 time_t t = self->tv.tv_sec;
200 struct tm *tm = localtime(&t);
201 return tm->tm_hour;
204 void Date_setHour_(Date *self, int v)
206 time_t t = self->tv.tv_sec;
207 struct tm *tm = localtime(&t);
208 tm->tm_hour = v;
209 self->tv.tv_sec = mktime(tm);
212 int Date_minute(const Date *self)
214 time_t t = self->tv.tv_sec;
215 struct tm *tm = localtime(&t);
216 return tm->tm_min;
219 void Date_setMinute_(Date *self, int v)
221 time_t t = self->tv.tv_sec;
222 struct tm *tm = localtime(&t);
223 tm->tm_min = v;
224 self->tv.tv_sec = mktime(tm);
227 double Date_second(const Date *self)
229 time_t t = self->tv.tv_sec;
230 struct tm *tm = localtime(&t);
231 return ((double)tm->tm_sec) + ((double)self->tv.tv_usec)/1000000.0;
234 void Date_setSecond_(Date *self, double v)
236 time_t t = self->tv.tv_sec;
237 struct tm *tm = localtime(&t);
238 tm->tm_sec = v;
239 self->tv.tv_sec = mktime(tm);
240 self->tv.tv_usec = (v - ((long)v))*1000000;
243 unsigned char Date_isDaylightSavingsTime(const Date *self)
245 time_t t = self->tv.tv_sec;
246 struct tm *tm = localtime(&t);
247 return (unsigned char)tm->tm_isdst;
250 int Date_isLeapYear(const Date *self)
252 int year = Date_year(self);
254 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
256 return 1;
258 else
260 return 0;
264 // format --------------------------------------------------------
266 static struct tm EmptyTM(void)
268 time_t tmp = 0;
269 struct tm *tt = localtime(&tmp);
270 struct tm t;
272 memcpy(&t, tt, sizeof(struct tm));
273 t.tm_sec = 0;
274 t.tm_min = 0;
275 t.tm_hour = 0;
276 t.tm_mday = 0;
277 t.tm_mon = 0;
278 t.tm_year = 0;
279 t.tm_wday = 0;
280 t.tm_yday = 0;
281 return t;
284 void Date_fromString_format_(Date *self, const char *s, const char *format)
286 struct tm tm = EmptyTM();
287 io_strptime((char *)s, (char *)format, &tm);
289 printf("year = %i\n", t.tm_year);
290 printf("month = %i\n", t.tm_mon);
291 printf("day = %i\n", t.tm_mday);
292 printf("hour = %i\n", t.tm_hour);
293 printf("min = %i\n", t.tm_min);
294 printf("sec = %i\n", t.tm_sec);
296 Date_fromSeconds_(self, mktime(&tm));
299 // durations --------------------------------------------------------
301 Duration *Date_newDurationBySubtractingDate_(const Date *self, const Date *other)
303 double d = Date_secondsSince_(self, other);
304 return Duration_newWithSeconds_(d);
307 void Date_addDuration_(Date *self, const Duration *d)
309 Date_addSeconds_(self, Duration_asSeconds(d));
312 void Date_subtractDuration_(Date *self, const Duration *d)
314 Date_addSeconds_(self, -Duration_asSeconds(d));
317 // -----------------------------------------------------------
319 double Date_secondsSinceNow(const Date *self)
321 double n = Date_SecondsFrom1970ToNow();
322 double s = Date_asSeconds(self);
323 return n - s;
326 // format --------------------------------------------------------
328 UArray *Date_asString(const Date *self, const char *format)
330 UArray *u = UArray_new();
331 time_t t = self->tv.tv_sec;
332 struct tm *tm = localtime(&t);
334 // what about unicode formats?
335 UArray_setSize_(u, 1024 + strlen(format));
336 strftime((char *)UArray_bytes(u), 1024, format, tm);
337 UArray_setSize_(u, strlen((char *)UArray_bytes(u)));
339 return u;