--- parser3/src/classes/date.C 2015/09/18 00:08:12 1.95 +++ parser3/src/classes/date.C 2016/03/27 20:53:43 1.100 @@ -1,7 +1,7 @@ /** @file 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 (http://paf.design.ru) */ @@ -13,7 +13,7 @@ #include "pa_vdate.h" #include "pa_vtable.h" -volatile const char * IDENT_DATE_C="$Id: date.C,v 1.95 2015/09/18 00:08:12 moko Exp $" IDENT_PA_VDATE_H; +volatile const char * IDENT_DATE_C="$Id: date.C,v 1.100 2016/03/27 20:53:43 moko Exp $" IDENT_PA_VDATE_H; // class @@ -50,9 +50,9 @@ Table date_calendar_table_template(new D static void _now(Request& r, MethodParams& params) { 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) - 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); } @@ -80,6 +80,16 @@ static int to_month(int imonth) { 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) { if(string) { char *str=string; @@ -88,8 +98,11 @@ static char *skip_number(char* string, c // skipping +- if(str[0]=='-' || str[0]=='+') str++; // at least one digit should be present - if(!isdigit(*(str++))) - throw Exception("date.format", 0, "invalid number in date specification '%s'", string); + if(!str[0]) + 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 while(isdigit(str[0])) str++; // skipping trailing whitespace @@ -98,7 +111,7 @@ static char *skip_number(char* string, c // delimiter check if(char c=str[0]){ 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) *delim=c; str[0]=0; @@ -147,7 +160,7 @@ static char *numeric_tz(char prefix, cha char *buf=new(PointerFreeGC) char[size]; strcpy(buf, prefix=='+' ? "SUB-":"SUB+"); strncpy(buf+4, tz, cur-tz); - buf[size]=0; + buf[size-1]=0; return buf; } @@ -172,7 +185,7 @@ tm cstr_to_time_t(char *cstr, const char const char *year, *month, *mday; const char *hour, *min, *sec, *msec; - year=skip_number(&cur, ":-", &delim); + year=skip_number(&cur, "-:", &delim); if(delim != ':' || delim == ':' && strlen(year) >=4 ){ // year present month=skip_number(&cur, delim == ':' ? ":" : "-"); @@ -194,8 +207,8 @@ 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); if(!tz){ if(!delim) - throw Exception("date.format", 0, "empty timezone specification"); - throw Exception("date.format", 0, "invalid timezone specification '%c%s'", cur ? cur : ""); + throw Exception("date.format", 0, "empty timezone"); + throw Exception("date.format", 0, "invalid timezone '%c%s'", delim, cur ? cur : ""); } *tzOut=tz; } @@ -277,10 +290,34 @@ static void _gmt_string(Request& r, Meth r.write_assign_lang(*vdate.get_gmt_string()); } -static void _iso_string(Request& r, MethodParams&) { +static void _iso_string(Request& r, MethodParams& params) { VDate& vdate=GET_SELF(r, VDate); - r.write_assign_lang(*vdate.get_iso_string()); + VDate::iso_string_type format=VDate::iso_string_default; + + if(params.count()>0) + if(HashStringValue* options=params.as_hash(0)){ + int valid_options=0; + if(Value* vshow_ms=options->get("ms")){ + if(r.process_to_value(*vshow_ms).as_bool()) + format=VDate::iso_string_type(format|VDate::iso_string_ms); + valid_options++; + } + if(Value* vshow_colon=options->get("colon")){ + if(!r.process_to_value(*vshow_colon).as_bool()) + format=VDate::iso_string_type(format|VDate::iso_string_no_colon); + valid_options++; + } + if(Value* vshow_z=options->get("z")){ + if(!r.process_to_value(*vshow_z).as_bool()) + format=VDate::iso_string_type(format|VDate::iso_string_no_z); + valid_options++; + } + if(valid_options != options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + + r.write_assign_lang(*vdate.get_iso_string(format)); } static void _roll(Request& r, MethodParams& params) { @@ -506,8 +543,8 @@ MDate::MDate(): Methoded("date") { // ^date.gmt-string[] add_native_method("gmt-string", Method::CT_DYNAMIC, _gmt_string, 0, 0); - // ^date.iso-string[] - add_native_method("iso-string", Method::CT_DYNAMIC, _iso_string, 0, 0); + // ^date.iso-string[$.colon(true) $.z(true) $.ms(false)] + add_native_method("iso-string", Method::CT_DYNAMIC, _iso_string, 0, 1); // ^date:lastday(year;month) // ^date.lastday[]