2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
14 #include "PortableStrptime.h"
19 Date
*self
= (Date
*)io_calloc(1, sizeof(Date
));
24 void Date_copy_(Date
*self
, const Date
*other
)
26 memcpy(self
, other
, sizeof(Date
));
29 void Date_free(Date
*self
)
34 int Date_compare(const Date
*self
, const Date
*other
)
36 double s1
= Date_asSeconds(self
);
37 double s2
= Date_asSeconds(other
);
44 return s1
> s2
? 1 : -1;
47 double Date_SecondsFrom1970ToNow(void)
50 struct timeval timeval
;
51 struct timezone timezone
;
53 gettimeofday(&timeval
, &timezone
);
55 //s -= timezone.tz_minuteswest * 60;
58 return s
+ (us
/1000000.0); /* + (60*60);*/
61 void Date_now(Date
*self
)
64 struct timeval timeval
;
65 struct timezone timezone
;
67 gettimeofday(&timeval
, &timezone
);
69 s
-= timezone
.tz_minuteswest
* 60;
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
)
94 void Date_setTimeZone_(Date
*self
, struct timezone 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
)
134 self
->tv
.tv_sec
= secs
;
135 self
->tv
.tv_usec
= (s
- secs
)*1000000;
138 void Date_addSeconds_(Date
*self
, double 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
);
174 void Date_setMonth_(Date
*self
, int v
)
176 time_t t
= self
->tv
.tv_sec
;
177 struct tm
*tm
= localtime(&t
);
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
);
189 void Date_setDay_(Date
*self
, int v
)
191 time_t t
= self
->tv
.tv_sec
;
192 struct tm
*tm
= localtime(&t
);
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
);
204 void Date_setHour_(Date
*self
, int v
)
206 time_t t
= self
->tv
.tv_sec
;
207 struct tm
*tm
= localtime(&t
);
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
);
219 void Date_setMinute_(Date
*self
, int v
)
221 time_t t
= self
->tv
.tv_sec
;
222 struct tm
*tm
= localtime(&t
);
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
);
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))
264 // format --------------------------------------------------------
266 static struct tm
EmptyTM(void)
269 struct tm
*tt
= localtime(&tmp
);
272 memcpy(&t
, tt
, sizeof(struct tm
));
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
);
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
)));