|
|
| version 1.95, 2015/09/18 00:08:12 | version 1.99, 2015/10/26 01:21:53 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b date parser class. | Parser: @b date parser class. |
| Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 50 Table date_calendar_table_template(new D | Line 50 Table date_calendar_table_template(new D |
| static void _now(Request& r, MethodParams& params) { | static void _now(Request& r, MethodParams& params) { |
| VDate& vdate=GET_SELF(r, VDate); | VDate& vdate=GET_SELF(r, VDate); |
| time_t t=time(0); | pa_time_t t=(pa_time_t)time(0); |
| if(params.count()==1) // ^now(offset) | if(params.count()==1) // ^now(offset) |
| t+=(time_t)round(params.as_double(0, "offset must be double", r)*SECS_PER_DAY); | t+=(pa_time_t)round(params.as_double(0, "offset must be double", r)*SECS_PER_DAY); |
| vdate.set_time(t); | vdate.set_time(t); |
| } | } |
| Line 80 static int to_month(int imonth) { | Line 80 static int to_month(int imonth) { |
| return max(1, min(imonth, 12)) -1; | return max(1, min(imonth, 12)) -1; |
| } | } |
| static const char *skip_number_throw(char *string, char c, const char *valid){ | |
| if(!valid[0]) | |
| throw Exception("date.format", 0, "invalid character '%c' after number in '%s'", c, string); | |
| if(!strcmp(valid, "+-Z")) | |
| throw Exception("date.format", 0, "invalid timezone character '%c' after number in '%s'", c, string); | |
| throw Exception("date.format", 0, "number delimiter '%c'%s expected, but found '%c' in date '%s'", | |
| valid[0], valid[strlen(valid)-1] == 'Z' ? " or timezone":"", c, string); | |
| } | |
| static char *skip_number(char* string, const char *valid_delim, char *delim) { | static char *skip_number(char* string, const char *valid_delim, char *delim) { |
| if(string) { | if(string) { |
| char *str=string; | char *str=string; |
| Line 88 static char *skip_number(char* string, c | Line 98 static char *skip_number(char* string, c |
| // skipping +- | // skipping +- |
| if(str[0]=='-' || str[0]=='+') str++; | if(str[0]=='-' || str[0]=='+') str++; |
| // at least one digit should be present | // at least one digit should be present |
| if(!isdigit(*(str++))) | if(!str[0]) |
| throw Exception("date.format", 0, "invalid number in date specification '%s'", string); | throw Exception("date.format", 0, "number expected in date '%s'", string); |
| if(!isdigit(str[0])) | |
| throw Exception("date.format", 0, "'%c' must be number in date '%s'", str[0], string); | |
| str++; | |
| // skipping digits | // skipping digits |
| while(isdigit(str[0])) str++; | while(isdigit(str[0])) str++; |
| // skipping trailing whitespace | // skipping trailing whitespace |
| Line 98 static char *skip_number(char* string, c | Line 111 static char *skip_number(char* string, c |
| // delimiter check | // delimiter check |
| if(char c=str[0]){ | if(char c=str[0]){ |
| if(!strchr(valid_delim, c)) | if(!strchr(valid_delim, c)) |
| throw Exception("date.format", 0, "invalid character '%c' in date specification '%s'", c, string); | skip_number_throw(string, c, valid_delim); |
| if(delim) | if(delim) |
| *delim=c; | *delim=c; |
| str[0]=0; | str[0]=0; |
| Line 147 static char *numeric_tz(char prefix, cha | Line 160 static char *numeric_tz(char prefix, cha |
| char *buf=new(PointerFreeGC) char[size]; | char *buf=new(PointerFreeGC) char[size]; |
| strcpy(buf, prefix=='+' ? "SUB-":"SUB+"); | strcpy(buf, prefix=='+' ? "SUB-":"SUB+"); |
| strncpy(buf+4, tz, cur-tz); | strncpy(buf+4, tz, cur-tz); |
| buf[size]=0; | buf[size-1]=0; |
| return buf; | return buf; |
| } | } |
| Line 172 tm cstr_to_time_t(char *cstr, const char | Line 185 tm cstr_to_time_t(char *cstr, const char |
| const char *year, *month, *mday; | const char *year, *month, *mday; |
| const char *hour, *min, *sec, *msec; | const char *hour, *min, *sec, *msec; |
| year=skip_number(&cur, ":-", &delim); | year=skip_number(&cur, "-:", &delim); |
| if(delim != ':' || delim == ':' && strlen(year) >=4 ){ | if(delim != ':' || delim == ':' && strlen(year) >=4 ){ |
| // year present | // year present |
| month=skip_number(&cur, delim == ':' ? ":" : "-"); | month=skip_number(&cur, delim == ':' ? ":" : "-"); |
| Line 194 tm cstr_to_time_t(char *cstr, const char | Line 207 tm cstr_to_time_t(char *cstr, const char |
| const char *tz = delim == 'Z' ? (skip_writespace(cur) ? 0 : "UTC") : (cur ? numeric_tz(delim, cur) : 0); | const char *tz = delim == 'Z' ? (skip_writespace(cur) ? 0 : "UTC") : (cur ? numeric_tz(delim, cur) : 0); |
| if(!tz){ | if(!tz){ |
| if(!delim) | if(!delim) |
| throw Exception("date.format", 0, "empty timezone specification"); | throw Exception("date.format", 0, "empty timezone"); |
| throw Exception("date.format", 0, "invalid timezone specification '%c%s'", cur ? cur : ""); | throw Exception("date.format", 0, "invalid timezone '%c%s'", delim, cur ? cur : ""); |
| } | } |
| *tzOut=tz; | *tzOut=tz; |
| } | } |