New doc system done for core
[io.git] / libs / basekit / source / Date.c
blob5c740907339e88e7ed30f29030b622bac9aed474
2 //metadoc Date copyright Steve Dekorte 2002
3 //metadoc Date license BSD revised
5 #define DATE_C
6 #include "Date.h"
7 #undef DATE_C
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <time.h>
12 #include <math.h>
13 #include "PortableStrptime.h"
14 #include "UArray.h"
16 Date *Date_new(void)
18 Date *self = (Date *)io_calloc(1, sizeof(Date));
19 Date_now(self);
20 return self;
23 void Date_copy_(Date *self, const Date *other)
25 memcpy(self, other, sizeof(Date));
28 void Date_free(Date *self)
30 io_free(self);
33 int Date_compare(const Date *self, const Date *other)
35 double s1 = Date_asSeconds(self);
36 double s2 = Date_asSeconds(other);
38 if (s1 == s2)
40 return 0;
43 return s1 > s2 ? 1 : -1;
46 double Date_SecondsFrom1970ToNow(void)
48 double s, us;
49 struct timeval timeval;
50 struct timezone timezone;
52 gettimeofday(&timeval, &timezone);
53 s = timeval.tv_sec;
54 //s -= timezone.tz_minuteswest * 60;
55 us = timeval.tv_usec;
57 return s + (us/1000000.0); /* + (60*60);*/
60 void Date_now(Date *self)
62 double s, us;
63 struct timeval timeval;
64 struct timezone timezone;
66 gettimeofday(&timeval, &timezone);
67 s = timeval.tv_sec;
68 s -= timezone.tz_minuteswest * 60;
69 us = timeval.tv_usec;
71 self->tv = timeval;
72 self->tz = timezone;
75 double Date_Clock(void)
77 return ((double)clock())/((double)CLOCKS_PER_SEC);
80 // zone --------------------------------------------------------
82 void Date_setToLocalTimeZone(Date *self)
84 struct timeval timeval;
85 gettimeofday(&timeval, &(self->tz));
88 struct timezone Date_timeZone(const Date *self)
90 return self->tz;
93 void Date_setTimeZone_(Date *self, struct timezone tz)
95 self->tz = tz;
98 void Date_convertToTimeZone_(Date *self, struct timezone tz)
100 double s = Date_asSeconds(self) +
101 ((self->tz.tz_minuteswest - tz.tz_minuteswest) * 60);
102 Date_fromSeconds_(self, s);
103 Date_setTimeZone_(self, tz);
106 // time --------------------------------------------------------
108 void Date_fromLocalTime_(Date *self, struct tm *t)
110 Date_fromTime_(self, mktime(t));
113 void Date_fromTime_(Date *self, time_t t)
115 Date_fromSeconds_(self, (double)t);
118 time_t Date_asTime(const Date *self)
120 return (time_t)self->tv.tv_sec;
123 // sconds --------------------------------------------------------
125 double Date_asSeconds(const Date *self)
127 return ((double)self->tv.tv_sec) + (((double)self->tv.tv_usec) / 1000000.0);
130 void Date_fromSeconds_(Date *self, double s)
132 long secs = s;
133 self->tv.tv_sec = secs;
134 self->tv.tv_usec = (s - secs)*1000000;
137 void Date_addSeconds_(Date *self, double s)
139 long secs = s;
140 self->tv.tv_sec += secs;
141 self->tv.tv_usec += (s - secs)*1000000;
144 double Date_secondsSince_(const Date *self, const Date *other)
146 return Date_asSeconds(self) - Date_asSeconds(other);
149 // components --------------------------------------------------------
151 long Date_year(const Date *self)
153 time_t t = self->tv.tv_sec;
154 struct tm *tm = localtime(&t);
155 return tm->tm_year + 1900;
158 void Date_setYear_(Date *self, long v)
160 time_t t = self->tv.tv_sec;
161 struct tm *tm = localtime(&t);
162 tm->tm_year = v - 1900;
163 self->tv.tv_sec = mktime(tm);
166 int Date_month(const Date *self)
168 time_t t = self->tv.tv_sec;
169 struct tm *tm = localtime(&t);
170 return tm->tm_mon;
173 void Date_setMonth_(Date *self, int v)
175 time_t t = self->tv.tv_sec;
176 struct tm *tm = localtime(&t);
177 tm->tm_mon = v;
178 self->tv.tv_sec = mktime(tm);
181 int Date_day(const Date *self)
183 time_t t = self->tv.tv_sec;
184 struct tm *tm = localtime(&t);
185 return tm->tm_mday;
188 void Date_setDay_(Date *self, int v)
190 time_t t = self->tv.tv_sec;
191 struct tm *tm = localtime(&t);
192 tm->tm_mday = v;
193 self->tv.tv_sec = mktime(tm);
196 int Date_hour(const Date *self)
198 time_t t = self->tv.tv_sec;
199 struct tm *tm = localtime(&t);
200 return tm->tm_hour;
203 void Date_setHour_(Date *self, int v)
205 time_t t = self->tv.tv_sec;
206 struct tm *tm = localtime(&t);
207 tm->tm_hour = v;
208 self->tv.tv_sec = mktime(tm);
211 int Date_minute(const Date *self)
213 time_t t = self->tv.tv_sec;
214 struct tm *tm = localtime(&t);
215 return tm->tm_min;
218 void Date_setMinute_(Date *self, int v)
220 time_t t = self->tv.tv_sec;
221 struct tm *tm = localtime(&t);
222 tm->tm_min = v;
223 self->tv.tv_sec = mktime(tm);
226 double Date_second(const Date *self)
228 time_t t = self->tv.tv_sec;
229 struct tm *tm = localtime(&t);
230 return ((double)tm->tm_sec) + ((double)self->tv.tv_usec)/1000000.0;
233 void Date_setSecond_(Date *self, double v)
235 time_t t = self->tv.tv_sec;
236 struct tm *tm = localtime(&t);
237 tm->tm_sec = v;
238 self->tv.tv_sec = mktime(tm);
239 self->tv.tv_usec = (v - ((long)v))*1000000;
242 unsigned char Date_isDaylightSavingsTime(const Date *self)
244 time_t t = self->tv.tv_sec;
245 struct tm *tm = localtime(&t);
246 return (unsigned char)tm->tm_isdst;
249 int Date_isLeapYear(const Date *self)
251 int year = Date_year(self);
253 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
255 return 1;
257 else
259 return 0;
263 // format --------------------------------------------------------
265 static struct tm EmptyTM(void)
267 time_t tmp = 0;
268 struct tm *tt = localtime(&tmp);
269 struct tm t;
271 memcpy(&t, tt, sizeof(struct tm));
272 t.tm_sec = 0;
273 t.tm_min = 0;
274 t.tm_hour = 0;
275 t.tm_mday = 0;
276 t.tm_mon = 0;
277 t.tm_year = 0;
278 t.tm_wday = 0;
279 t.tm_yday = 0;
280 return t;
283 void Date_fromString_format_(Date *self, const char *s, const char *format)
285 struct tm tm = EmptyTM();
286 io_strptime((char *)s, (char *)format, &tm);
288 printf("year = %i\n", t.tm_year);
289 printf("month = %i\n", t.tm_mon);
290 printf("day = %i\n", t.tm_mday);
291 printf("hour = %i\n", t.tm_hour);
292 printf("min = %i\n", t.tm_min);
293 printf("sec = %i\n", t.tm_sec);
295 Date_fromSeconds_(self, mktime(&tm));
298 // durations --------------------------------------------------------
300 Duration *Date_newDurationBySubtractingDate_(const Date *self, const Date *other)
302 double d = Date_secondsSince_(self, other);
303 return Duration_newWithSeconds_(d);
306 void Date_addDuration_(Date *self, const Duration *d)
308 Date_addSeconds_(self, Duration_asSeconds(d));
311 void Date_subtractDuration_(Date *self, const Duration *d)
313 Date_addSeconds_(self, -Duration_asSeconds(d));
316 // -----------------------------------------------------------
318 double Date_secondsSinceNow(const Date *self)
320 double n = Date_SecondsFrom1970ToNow();
321 double s = Date_asSeconds(self);
322 return n - s;
325 // format --------------------------------------------------------
327 UArray *Date_asString(const Date *self, const char *format)
329 UArray *u = UArray_new();
330 time_t t = self->tv.tv_sec;
331 struct tm *tm = localtime(&t);
333 // what about unicode formats?
334 UArray_setSize_(u, 1024 + strlen(format));
335 strftime((char *)UArray_bytes(u), 1024, format, tm);
336 UArray_setSize_(u, strlen((char *)UArray_bytes(u)));
338 return u;