Annotation of parser3/src/types/pa_vdate.C, revision 1.25
1.1 moko 1: /** @file
2: Parser: @b date parser class.
3:
1.24 moko 4: Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com)
1.1 moko 5: */
6:
7: #include "pa_vstateless_object.h"
8: #include "pa_vdate.h"
9: #include "pa_vint.h"
10: #include "pa_vstring.h"
11:
1.25 ! moko 12: volatile const char * IDENT_PA_PA_VDATE_C="$Id: pa_vdate.C,v 1.24 2020/12/15 17:10:39 moko Exp $" IDENT_PA_VDATE_H;
1.1 moko 13:
1.4 moko 14: #define ZERO_DATE (-62169984000ll-SECS_PER_DAY) // '0000-00-00 00:00:00' - 1 day
15: #define MAX_DATE (253402300799ll+SECS_PER_DAY) // '9999-12-31 23:59:59' + 1 day
1.3 moko 16:
17: static const int DAYS_IN_MONTH[12] =
18: {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
19:
20: static const int DAYS_BEFORE_MONTH[12] =
21: {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
22:
23: #define IS_LEAP(y) (((y) % 4) == 0 && (((y) % 100) != 0 || (((y)+1900) % 400) == 0))
24:
25: int VDate::getMonthDays(int year, int month) {
26: return (month == 1 /* january -- 0 */ && IS_LEAP(year)) ? 29 : DAYS_IN_MONTH[month];
27: }
28:
1.20 moko 29: void pa_gmtime(pa_time_t lcltime, struct tm *res);
30: pa_time_t pa_mktime(struct tm *tim_p);
1.3 moko 31:
32: static int gmt_offset() {
33: #if defined(HAVE_TIMEZONE)
34: tzset();
1.18 moko 35: #if _MSC_VER >= 1900
36: long timezone = 0;
37: _get_timezone(&timezone);
38: #endif
1.6 moko 39: return -timezone;
1.3 moko 40: #else
41: time_t t=time(0);
42: tm *tm=localtime(&t);
43: #if defined(HAVE_TM_GMTOFF)
1.6 moko 44: return tm->tm_gmtoff;
1.3 moko 45: #elif defined(HAVE_TM_TZADJ)
1.6 moko 46: return -tm->tm_tzadj;
1.3 moko 47: #else
48: #error neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF nor HAVE_TM_TZADJ defined
49: #endif
50: #endif
51: }
52:
53: static void pa_set_tz(const char* ntz) {
54: if(ntz && *ntz){
55: static char temp_tz_pair[MAX_STRING];
56: snprintf(temp_tz_pair, sizeof(temp_tz_pair), "TZ=%s", ntz);
57: putenv(temp_tz_pair);
58: } else {
1.1 moko 59: #ifdef HAVE_UNSETENV
1.3 moko 60: unsetenv("TZ");
1.1 moko 61: #else
1.3 moko 62: putenv("TZ=");
1.1 moko 63: #endif
1.3 moko 64: }
1.9 moko 65: tzset(); // required in Windows
1.1 moko 66: }
67:
1.3 moko 68: /// Auto-object used for temporarily substituting/removing timezone variable
69: class Temp_tz {
70: const char* ntz;
71: char saved_tz[MAX_STRING];
72: public:
73: static const char *default_tz;
74: public:
75: Temp_tz(const char *atz) : ntz(atz) {
76: if(!ntz)
77: ntz=default_tz;
78: if(!ntz)
79: return;
80: if(const char* ctz=getenv("TZ")){
1.25 ! moko 81: pa_strncpy(saved_tz, ctz, MAX_STRING);
1.3 moko 82: } else
83: saved_tz[0]=0;
84: pa_set_tz(ntz);
85: }
86: ~Temp_tz() {
87: if(ntz)
88: pa_set_tz(saved_tz);
89: }
90: };
91:
92: const char *Temp_tz::default_tz=0;
1.1 moko 93:
1.8 moko 94: static void pa_localtime(const char *tz, pa_time_t atime, struct tm &tmIn) {
1.3 moko 95: Temp_tz temp_tz(tz);
96: #ifdef PA_DATE64
97: tmIn=*localtime(&atime);
98: #else
1.11 moko 99: if(atime >= 0 && atime <= INT_MAX){
1.5 moko 100: time_t itime=(time_t)atime;
1.3 moko 101: tmIn=*localtime(&itime);
102: } else {
1.6 moko 103: pa_gmtime(atime+gmt_offset(), &tmIn);
1.3 moko 104: }
105: #endif
1.1 moko 106: }
107:
1.3 moko 108: static pa_time_t pa_mktime(const char *tz, struct tm &tmIn) {
109: Temp_tz temp_tz(tz);
110: #ifdef PA_DATE64
111: return mktime(&tmIn);
112: #else
113: time_t result=mktime(&tmIn);
114: if(result != -1)
1.10 moko 115: return (pa_time_t)result;
1.6 moko 116: return pa_mktime(&tmIn)-gmt_offset();
1.3 moko 117: #endif
1.1 moko 118: }
119:
1.19 moko 120: const String* VDate::get_sql_string(sql_string_type aformat) {
121: switch(aformat){
1.3 moko 122: case sql_string_datetime:{
123: static const char *format="%.4d-%.2d-%.2d %.2d:%.2d:%.2d";
124: static int size=4+1+2+1+2 +1+ 2+1+2+1+2 +1/*zero-teminator*/+1/*for faulty snprintfs*/;
125: char *buf=new(PointerFreeGC) char[size];
126: snprintf(buf, size, format, ftm.tm_year+1900, ftm.tm_mon+1, ftm.tm_mday, ftm.tm_hour, ftm.tm_min, ftm.tm_sec);
127: return new String(buf);
128: }
129: case sql_string_date:{
130: static const char *format="%.4d-%.2d-%.2d";
131: static int size=4+1+2+1+2 +1/*zero-teminator*/+1/*for faulty snprintfs*/;
132: char *buf=new(PointerFreeGC) char[size];
133: snprintf(buf, size, format, ftm.tm_year+1900, ftm.tm_mon+1, ftm.tm_mday);
134: return new String(buf);
135: }
136: case sql_string_time:{
137: static const char *format="%.2d:%.2d:%.2d";
138: static int size=2+1+2+1+2 +1/*zero-teminator*/+1/*for faulty snprintfs*/;
139: char *buf=new(PointerFreeGC) char[size];
140: snprintf(buf, size, format, ftm.tm_hour, ftm.tm_min, ftm.tm_sec);
141: return new String(buf);
142: }
1.1 moko 143: }
1.12 moko 144: return &String::Empty;
1.3 moko 145: }
146:
147:
148: const String* VDate::get_gmt_string() {
1.6 moko 149: struct tm gtm;
1.3 moko 150: #ifdef PA_DATE64
1.6 moko 151: gtm=*gmtime(&ftime);
1.3 moko 152: #else
1.6 moko 153: pa_gmtime(ftime, >m);
1.3 moko 154: #endif
155: /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
156: static const char month_names[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
157: static const char days[7][4]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
1.1 moko 158:
1.6 moko 159: static const char *format="%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT";
1.3 moko 160: static int size=3+1+1+2+1+3+1+4+1+2+1+2+1+2+4 +1/*zero-teminator*/+1/*for faulty snprintfs*/;
161: char *buf=new(PointerFreeGC) char[size];
1.6 moko 162: snprintf(buf, size, format, days[gtm.tm_wday], gtm.tm_mday, month_names[gtm.tm_mon], gtm.tm_year+1900, gtm.tm_hour, gtm.tm_min, gtm.tm_sec);
1.3 moko 163: return new String(buf);
1.1 moko 164: }
165:
1.15 moko 166: const String* VDate::get_iso_string(iso_string_type format) {
1.6 moko 167: Temp_tz temp_tz(ftz_cstr);
1.15 moko 168: int offset=gmt_offset();
1.6 moko 169: /// http://www.w3.org/TR/NOTE-datetime
1.15 moko 170: if(offset || (format & iso_string_no_z)){
1.16 moko 171: char sign=offset<0 ? '-':'+';
1.6 moko 172: offset=abs(offset);
1.15 moko 173: static const char *sformats[]={
174: "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d%c%.2d:%.2d",
175: "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.000%c%.2d:%.2d",
176: "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d%c%.2d%.2d",
177: "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.000%c%.2d%.2d",
178: };
179: static int size=4+1+2+1+2 +1 +2+1+2+1+2 +4 +1 +2+1+2 +1/*zero-teminator*/+1/*for faulty snprintfs*/;
180: const char *sformat=sformats[format & (iso_string_ms | iso_string_no_colon)];
1.6 moko 181: char *buf=new(PointerFreeGC) char[size];
1.15 moko 182: snprintf(buf, size, sformat, ftm.tm_year+1900, ftm.tm_mon+1, ftm.tm_mday, ftm.tm_hour, ftm.tm_min, ftm.tm_sec,
1.6 moko 183: sign, offset/3600, (offset/60)%60);
184: return new String(buf);
185: } else {
1.15 moko 186: static const char *sformats[]={
187: "%.4d-%.2d-%.2dT%.2d:%.2d:%.2dZ",
188: "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.000Z"
189: };
190: static int size=4+1+2+1+2 +1 +2+1+2+1+2 +4 +1 +1/*zero-teminator*/+1/*for faulty snprintfs*/;
191: const char *sformat=sformats[format & (iso_string_ms)];
1.6 moko 192: char *buf=new(PointerFreeGC) char[size];
1.15 moko 193: snprintf(buf, size, sformat, ftm.tm_year+1900, ftm.tm_mon+1, ftm.tm_mday, ftm.tm_hour, ftm.tm_min, ftm.tm_sec);
1.6 moko 194: return new String(buf);
195: }
196: }
197:
1.17 moko 198: Value* VDate::get_element(const String& aname) {
1.1 moko 199: // $method
200: if(Value* result=VStateless_object::get_element(aname))
201: return result;
202:
203: // $TZ
1.3 moko 204: if(aname=="TZ")
205: return ftz_cstr ? new VString(*new String(ftz_cstr)): new VString();
1.1 moko 206:
207: int result;
1.3 moko 208: if(aname=="year") result=1900+ftm.tm_year;
209: else if(aname=="month") result=1+ftm.tm_mon;
210: else if(aname=="day") result=ftm.tm_mday;
211: else if(aname=="hour") result=ftm.tm_hour;
212: else if(aname=="minute") result=ftm.tm_min;
213: else if(aname=="second") result=ftm.tm_sec;
214: else if(aname=="weekday") result=ftm.tm_wday;
215: else if(aname=="yearday") result=ftm.tm_yday;
216: else if(aname=="daylightsaving") result=ftm.tm_isdst;
1.1 moko 217: else if(aname=="week") {
1.3 moko 218: yw week = CalcWeek(ftm);
1.1 moko 219: result=week.week;
220: }
221: else if(aname=="weekyear") {
1.3 moko 222: yw week = CalcWeek(ftm);
1.1 moko 223: result=1900+week.year;
224: } else { return bark("%s field not found", &aname); }
225: return new VInt(result);
226: }
227:
1.17 moko 228: extern int to_year(int iyear);
229:
230: const VJunction* VDate::put_element(const String& aname, Value* avalue) {
231: tm tmIn=get_tm();
232:
233: if(aname=="year") tmIn.tm_year=to_year(avalue->as_int());
234: else if(aname=="month") tmIn.tm_mon=avalue->as_int()-1;
235: else if(aname=="day") tmIn.tm_mday=avalue->as_int();
236: else if(aname=="hour") tmIn.tm_hour=avalue->as_int();
237: else if(aname=="minute") tmIn.tm_min=avalue->as_int();
238: else if(aname=="second") tmIn.tm_sec=avalue->as_int();
239: else bark("%s field not found", &aname);
240:
241: set_tm(tmIn);
242:
1.23 moko 243: return 0;
1.17 moko 244: }
245:
246:
1.1 moko 247: const String* VDate::get_json_string(Json_options& options) {
248: String* result=new String();
249: switch(options.date){
250: case Json_options::D_SQL:
251: result->append_quoted(get_sql_string());
252: break;
253: case Json_options::D_GMT:
254: result->append_quoted(get_gmt_string());
255: break;
1.6 moko 256: case Json_options::D_ISO:
257: result->append_quoted(get_iso_string());
258: break;
1.1 moko 259: case Json_options::D_TIMESTAMP:
260: *result << format((int)ftime, 0);
261: break;
262: }
263: return result;
264: }
1.2 moko 265:
1.3 moko 266: void VDate::validate() {
267: if((ftm.tm_year==-1901) && (ftm.tm_mon==10) && (ftm.tm_mday==30)){
268: ftm.tm_year=-1900;
269: ftm.tm_mon=-1;
270: ftm.tm_mday=0;
271: }
272: if((ftm.tm_year+1900)<0 || (ftm.tm_year+1900)>9999){
273: throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "year '%d' is out of range 0..9999", ftm.tm_year+1900);
274: }
275: }
276:
277: void VDate::set_time(pa_time_t atime) {
1.2 moko 278: if(atime==-1)
279: throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "invalid datetime");
1.3 moko 280: if(atime<ZERO_DATE || atime>MAX_DATE)
281: throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "unix time %.15g is out of range 0..9999 year", (double)atime);
1.2 moko 282: ftime=atime;
1.3 moko 283: pa_localtime(ftz_cstr, ftime, ftm);
284: validate();
285: }
286:
287: void VDate::set_tm(tm &tmIn) {
1.4 moko 288: pa_time_t atime=pa_mktime(ftz_cstr, tmIn);
289: if(atime==-1)
290: throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "invalid datetime '%04d-%02d-%02d'", tmIn.tm_year+1900, tmIn.tm_mon+1, tmIn.tm_mday);
291: ftime=atime;
1.3 moko 292: ftm=tmIn;
293: validate();
1.2 moko 294: }
295:
1.6 moko 296: void VDate::set_tz(const char* atz) {
297: ftz_cstr=atz && atz[0] ? atz : 0; // ftm should be updated afterwards
1.3 moko 298: }
299:
1.6 moko 300: void VDate::set_default_tz(const char* atz) {
301: Temp_tz::default_tz=atz && atz[0] ? atz : 0;
1.2 moko 302: }
303:
304: static int ISOWeekCount (int year) {
305: static const unsigned int YearWeeks[] = {
306: 52,52,52,52,53, 52,52,52,52,52,
307: 53,52,52,52,52, 52,53,52,52,52,
308: 52,53,52,52,52, 52,52,53
309: };
310: return YearWeeks[(year+1900) % 28];
311: }
312:
1.14 moko 313: VDate::yw VDate::CalcWeek(tm tms) {
1.2 moko 314: yw week = {tms.tm_year, 0};
315:
316: // http://www.merlyn.demon.co.uk/weekinfo.htm
317: static const unsigned int FirstThurs[] = {7,5,4,3,2,7,6,5,4,2,1,7,6,4,3,2,1,6,5,4,3,1,7,6,5,3,2,1};
318: int diff = tms.tm_yday-(FirstThurs[(tms.tm_year+1900) % 28]-4);
319: if (diff < 0){
320: tms.tm_mday = diff;
1.3 moko 321: pa_mktime(0, tms); // normalize
1.2 moko 322: week = CalcWeek(tms);
323: } else {
324: week.week = 1 + diff/7;
325: if ( week.week > 52 && ISOWeekCount(week.year) < week.week ){
326: week.year++;
327: week.week = 1;
328: }
329: }
330: return week;
331: }
1.3 moko 332:
333: #ifndef PA_DATE64
334:
335: /*
336: * gmtime_r.c
337: * Original Author: Adapted from tzcode maintained by Arthur David Olson.
338: * Modifications:
339: * - Changed to mktm_r and added __tzcalc_limits - 04/10/02, Jeff Johnston
340: * - Fixed bug in mday computations - 08/12/04, Alex Mogilnikov <alx@intellectronika.ru>
341: * - Fixed bug in __tzcalc_limits - 08/12/04, Alex Mogilnikov <alx@intellectronika.ru>
342: * - Move code from _mktm_r() to gmtime_r() - 05/09/14, Freddie Chopin <freddie_chopin@op.pl>
343: * - Fixed bug in calculations for dates after year 2069 or before year 1901. Ideas for
344: * solution taken from musl's __secs_to_tm() - 07/12/2014, Freddie Chopin
345: * <freddie_chopin@op.pl>
346: * - Use faster algorithm from civil_from_days() by Howard Hinnant - 12/06/2014,
347: * Freddie Chopin <freddie_chopin@op.pl>
348: *
349: * Converts the calendar time pointed to by tim_p into a broken-down time
350: * expressed as local time. Returns a pointer to a structure containing the
351: * broken-down time.
352: */
353:
354: /* Move epoch from 01.01.1970 to 01.03.0000 (yes, Year 0) - this is the first
355: * day of a 400-year long "era", right after additional day of leap year.
356: * This adjustment is required only for date calculation, so instead of
357: * modifying time_t value (which would require 64-bit operations to work
358: * correctly) it's enough to adjust the calculated number of days since epoch.
359: */
360:
361: #define SECS_PER_HOUR 3600
362: #define SECS_PER_MIN 60
363: #define DAYS_PER_WEEK 7
364: #define YEAR_BASE 1900
365:
366: #define EPOCH_ADJUSTMENT_DAYS 719468L
367: /* year to which the adjustment was made */
368: #define ADJUSTED_EPOCH_YEAR 0
369: /* 1st March of year 0 is Wednesday */
370: #define ADJUSTED_EPOCH_WDAY 3
371: /* there are 97 leap years in 400-year periods. ((400 - 97) * 365 + 97 * 366) */
372: #define DAYS_PER_ERA 146097L
373: /* there are 24 leap years in 100-year periods. ((100 - 24) * 365 + 24 * 366) */
374: #define DAYS_PER_CENTURY 36524L
375: /* there is one leap year every 4 years */
376: #define DAYS_PER_4_YEARS (3 * 365 + 366)
377: /* number of days in a non-leap year */
378: #define DAYS_PER_YEAR 365
379: /* number of days in January */
380: #define DAYS_IN_JANUARY 31
381: /* number of days in non-leap February */
382: #define DAYS_IN_FEBRUARY 28
383: /* number of years per era */
384: #define YEARS_PER_ERA 400
385:
1.21 moko 386: void pa_gmtime(pa_time_t lcltime, struct tm *res) {
1.3 moko 387: long days, rem;
388: int era, weekday, year;
389: unsigned erayear, yearday, month, day;
390: unsigned long eraday;
391:
1.5 moko 392: days = (long)(lcltime / SECS_PER_DAY);
393: rem = (long)(lcltime - (pa_time_t)days * SECS_PER_DAY);
1.3 moko 394: days += EPOCH_ADJUSTMENT_DAYS;
395: if (rem < 0)
396: {
397: rem += SECS_PER_DAY;
398: --days;
399: }
400:
401: /* compute hour, min, and sec */
402: res->tm_hour = (int) (rem / SECS_PER_HOUR);
403: rem %= SECS_PER_HOUR;
404: res->tm_min = (int) (rem / SECS_PER_MIN);
405: res->tm_sec = (int) (rem % SECS_PER_MIN);
406:
407: /* compute day of week */
408: if ((weekday = ((ADJUSTED_EPOCH_WDAY + days) % DAYS_PER_WEEK)) < 0)
409: weekday += DAYS_PER_WEEK;
410: res->tm_wday = weekday;
411:
412: /* compute year, month, day & day of year */
413: /* for description of this algorithm see
414: * http://howardhinnant.github.io/date_algorithms.html#civil_from_days */
415: era = (days >= 0 ? days : days - (DAYS_PER_ERA - 1)) / DAYS_PER_ERA;
416: eraday = days - era * DAYS_PER_ERA; /* [0, 146096] */
417: erayear = (eraday - eraday / (DAYS_PER_4_YEARS - 1) + eraday / DAYS_PER_CENTURY -
418: eraday / (DAYS_PER_ERA - 1)) / 365; /* [0, 399] */
419: yearday = eraday - (DAYS_PER_YEAR * erayear + erayear / 4 - erayear / 100); /* [0, 365] */
420: month = (5 * yearday + 2) / 153; /* [0, 11] */
421: day = yearday - (153 * month + 2) / 5 + 1; /* [1, 31] */
422: month += month < 10 ? 2 : -10;
423: year = ADJUSTED_EPOCH_YEAR + erayear + era * YEARS_PER_ERA + (month <= 1);
424:
425: res->tm_yday = yearday >= DAYS_PER_YEAR - DAYS_IN_JANUARY - DAYS_IN_FEBRUARY ?
426: yearday - (DAYS_PER_YEAR - DAYS_IN_JANUARY - DAYS_IN_FEBRUARY) :
427: yearday + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + IS_LEAP(erayear);
428: res->tm_year = year - YEAR_BASE;
429: res->tm_mon = month;
430: res->tm_mday = day;
431:
432: res->tm_isdst = 0;
433: }
434:
435:
436: /*
437: * mktime.c
438: * Original Author: G. Haley
439: *
440: * Converts the broken-down time, expressed as local time, in the structure
441: * pointed to by tim_p into a calendar time value. The original values of the
442: * tm_wday and tm_yday fields of the structure are ignored, and the original
443: * values of the other fields have no restrictions. On successful completion
444: * the fields of the structure are set to represent the specified calendar
445: * time. Returns the specified calendar time. If the calendar time can not be
446: * represented, returns the value (time_t) -1.
447: */
448:
449: #define _DAYS_IN_MONTH(x) ((x == 1) ? days_in_feb : DAYS_IN_MONTH[x])
450: #define _DAYS_IN_YEAR(year) (IS_LEAP(year) ? 366 : 365)
451:
452: static void validate_structure(struct tm *tim_p) {
453: div_t res;
454: int days_in_feb = 28;
455:
456: /* calculate time & date to account for out of range values */
457: if (tim_p->tm_sec < 0 || tim_p->tm_sec > 59)
458: {
459: res = div (tim_p->tm_sec, 60);
460: tim_p->tm_min += res.quot;
461: if ((tim_p->tm_sec = res.rem) < 0)
462: {
463: tim_p->tm_sec += 60;
464: --tim_p->tm_min;
465: }
466: }
467:
468: if (tim_p->tm_min < 0 || tim_p->tm_min > 59)
469: {
470: res = div (tim_p->tm_min, 60);
471: tim_p->tm_hour += res.quot;
472: if ((tim_p->tm_min = res.rem) < 0)
473: {
474: tim_p->tm_min += 60;
475: --tim_p->tm_hour;
476: }
477: }
478:
479: if (tim_p->tm_hour < 0 || tim_p->tm_hour > 23)
480: {
481: res = div (tim_p->tm_hour, 24);
482: tim_p->tm_mday += res.quot;
483: if ((tim_p->tm_hour = res.rem) < 0)
484: {
485: tim_p->tm_hour += 24;
486: --tim_p->tm_mday;
487: }
488: }
489:
490: if (tim_p->tm_mon < 0 || tim_p->tm_mon > 11)
491: {
492: res = div (tim_p->tm_mon, 12);
493: tim_p->tm_year += res.quot;
494: if ((tim_p->tm_mon = res.rem) < 0)
495: {
496: tim_p->tm_mon += 12;
497: --tim_p->tm_year;
498: }
499: }
500:
501: if (_DAYS_IN_YEAR (tim_p->tm_year) == 366)
502: days_in_feb = 29;
503:
504: if (tim_p->tm_mday <= 0)
505: {
506: while (tim_p->tm_mday <= 0)
507: {
508: if (--tim_p->tm_mon == -1)
509: {
510: tim_p->tm_year--;
511: tim_p->tm_mon = 11;
512: days_in_feb =
513: ((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ?
514: 29 : 28);
515: }
516: tim_p->tm_mday += _DAYS_IN_MONTH (tim_p->tm_mon);
517: }
518: }
519: else
520: {
521: while (tim_p->tm_mday > _DAYS_IN_MONTH (tim_p->tm_mon))
522: {
523: tim_p->tm_mday -= _DAYS_IN_MONTH (tim_p->tm_mon);
524: if (++tim_p->tm_mon == 12)
525: {
526: tim_p->tm_year++;
527: tim_p->tm_mon = 0;
528: days_in_feb =
529: ((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ?
530: 29 : 28);
531: }
532: }
533: }
534: }
535:
1.21 moko 536: pa_time_t pa_mktime(struct tm *tim_p) {
1.3 moko 537: pa_time_t tim = 0;
538: long days = 0;
539: int year;
540:
541: /* validate structure */
542: validate_structure (tim_p);
543:
544: /* compute hours, minutes, seconds */
545: tim += tim_p->tm_sec + (tim_p->tm_min * SECS_PER_MIN) +
546: (tim_p->tm_hour * SECS_PER_HOUR);
547:
548: /* compute days in year */
549: days += tim_p->tm_mday - 1;
550: days += DAYS_BEFORE_MONTH[tim_p->tm_mon];
551: if (tim_p->tm_mon > 1 && _DAYS_IN_YEAR (tim_p->tm_year) == 366)
552: days++;
553:
554: /* compute day of the year */
555: tim_p->tm_yday = days;
556:
557: if (tim_p->tm_year > 10000 || tim_p->tm_year < -10000)
558: return (time_t) -1;
559:
560: /* compute days in other years */
561: if ((year = tim_p->tm_year) > 70)
562: {
563: for (year = 70; year < tim_p->tm_year; year++)
564: days += _DAYS_IN_YEAR (year);
565: }
566: else if (year < 70)
567: {
568: for (year = 69; year > tim_p->tm_year; year--)
569: days -= _DAYS_IN_YEAR (year);
570: days -= _DAYS_IN_YEAR (year);
571: }
572:
573: /* compute total seconds */
574: tim += (pa_time_t)days * SECS_PER_DAY;
575:
576: /* compute day of the week */
577: if ((tim_p->tm_wday = (days + 4) % 7) < 0)
578: tim_p->tm_wday += 7;
579:
580: return tim;
581: }
582:
583: #endif
E-mail: