--- parser3/src/classes/date.C 2003/11/27 10:20:32 1.58 +++ parser3/src/classes/date.C 2004/02/19 15:25:10 1.64 @@ -1,11 +1,11 @@ /** @file Parser: @b date parser class. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_DATE_C="$Date: 2003/11/27 10:20:32 $"; +static const char * const IDENT_DATE_C="$Date: 2004/02/19 15:25:10 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -60,25 +60,24 @@ static void _now(Request& r, MethodParam vdate.set_time(t); } -static int NN_year_to_NNNN(int year) { - if(year<70) // 0..69 -> 100..169 [2000..2069] - year+=100; - if(year>=1900) - year-=1900; - return year; +/// shrinked range: 1970/1/1 to 2038/1/1 +static int to_tm_year(int iyear) { + if(iyear<1970 || iyear>2038) + throw Exception(0, + 0, + "year '%d' is out of valid range", iyear); + return iyear-1900; } // 2002-04-25 18:14:00 // 18:14:00 // 2002:04:25 [+maybe time] -/*not static, used in image.C*/ time_t cstr_to_time_t(char *cstr, bool fail_on_error) { - if( !cstr || !*cstr ) { - if(fail_on_error) - throw Exception(0, - 0, - "empty string is not valid datetime"); - return 0; - } +/*not static, used in image.C*/ time_t cstr_to_time_t(char *cstr) { + if( !cstr || !*cstr ) + throw Exception(0, + 0, + "empty string is not valid datetime"); + char *cur=cstr; int date_delim=isdigit(cur[0])&&isdigit(cur[1])&&isdigit(cur[2])&&isdigit(cur[3])&&cur[4]==':'?':' :'-'; @@ -104,19 +103,22 @@ static int NN_year_to_NNNN(int year) { goto date_part_set; } else hour=min=sec=0; // not YYYY- & not HH: = just YYYY - tmIn.tm_year=NN_year_to_NNNN(atoi(year)); - tmIn.tm_mon=month?atoi(month)-1:0; - tmIn.tm_mday=mday?atoi(mday):1; + tmIn.tm_year=to_tm_year(pa_atoi(year)); + tmIn.tm_mon=month?pa_atoi(month)-1:0; + tmIn.tm_mday=mday?pa_atoi(mday):1; date_part_set: - tmIn.tm_hour=hour?atoi(hour):0; - tmIn.tm_min=min?atoi(min):0; - tmIn.tm_sec=sec?atoi(sec):0; + int savedHour=tmIn.tm_hour=hour?pa_atoi(hour):0; + tmIn.tm_min=min?pa_atoi(min):0; + tmIn.tm_sec=sec?pa_atoi(sec):0; time_t result=mktime(&tmIn); + if(tmIn.tm_hour!=savedHour) + throw Exception(0, + 0, //report_error_origin, + "invalid datetime: spring daylightsaving hour hole"); if(result<0) - if(fail_on_error) - throw Exception(0, - 0, //report_error_origin, - "invalid datetime"); + throw Exception(0, + 0, //report_error_origin, + "invalid datetime"); return result; } @@ -129,7 +131,7 @@ static void _create(Request& r, MethodPa // ^create[2002-04-25 18:14:00] // ^create[18:14:00] if(const String* sdate=params[0].get_string()) - t=cstr_to_time_t(sdate->cstrm(), true); + t=cstr_to_time_t(sdate->cstrm()); else { // ^create(float days) t=(time_t)round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY); if(t<0 || !localtime(&t)) @@ -140,13 +142,18 @@ static void _create(Request& r, MethodPa } else if(params.count()>=2) { // ^create(y;m;d[;h[;m[;s]]]) tm tmIn; memset(&tmIn, 0, sizeof(tmIn)); tmIn.tm_isdst=-1; - tmIn.tm_year=NN_year_to_NNNN(params.as_int(0, "year must be int", r)); + tmIn.tm_year=to_tm_year(params.as_int(0, "year must be int", r)); tmIn.tm_mon=params.as_int(1, "month must be int", r)-1; tmIn.tm_mday=params.count()>2?params.as_int(2, "mday must be int", r):1; - if(params.count()>3) tmIn.tm_hour=params.as_int(3, "hour must be int", r); + int savedHour=0; + if(params.count()>3) savedHour=tmIn.tm_hour=params.as_int(3, "hour must be int", r); if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r); if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r); t=mktime(&tmIn); + if(tmIn.tm_hour!=savedHour) + throw Exception(0, + 0, //report_error_origin, + "invalid datetime: spring daylightsaving hour hole"); if(t<0) throw Exception(0, 0, @@ -232,7 +239,7 @@ static void _roll(Request& r, MethodPara tmOut->tm_isdst=-1; { time_t t_changed_time=mktime/*normalizetime*/(tmOut); - /*autofix: in msk timezone last sunday of april hour hole: [2am->3am) + /*autofix: in msk timezone last sunday of march hour hole: [2am->3am) if( tmOut->tm_hour!=tmSaved.tm_hour ||tmOut->tm_min!=tmSaved.tm_min)