--- parser3/src/classes/date.C 2020/12/15 17:10:27 1.112 +++ parser3/src/classes/date.C 2024/12/23 16:59:17 1.121 @@ -1,8 +1,8 @@ /** @file Parser: @b date parser class. - Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "classes.h" @@ -12,8 +12,9 @@ #include "pa_vdouble.h" #include "pa_vdate.h" #include "pa_vtable.h" +#include "pa_vbool.h" -volatile const char * IDENT_DATE_C="$Id: date.C,v 1.112 2020/12/15 17:10:27 moko Exp $" IDENT_PA_VDATE_H; +volatile const char * IDENT_DATE_C="$Id: date.C,v 1.121 2024/12/23 16:59:17 moko Exp $" IDENT_PA_VDATE_H; // class @@ -42,8 +43,12 @@ public: } }; - -Table date_calendar_table_template(new Date_calendar_table_template_columns); +static Table &date_calendar_table_template(){ + static Table *singleton=NULL; + if(!singleton) + singleton=new Table(new Date_calendar_table_template_columns); + return *singleton; +} // methods @@ -57,12 +62,16 @@ static void _now(Request& r, MethodParam vdate.set_time(t); } -static void _today(Request& r, MethodParams&) { +static void _today(Request& r, MethodParams& params) { VDate& vdate=GET_SELF(r, VDate); time_t t=time(0); tm today=*localtime(&t); + + if (params.count() == 1) // ^today(offset) + today.tm_mday += params.as_int(0, "offset must be int", r); + today.tm_hour=0; today.tm_min=0; today.tm_sec=0; @@ -188,7 +197,8 @@ tm cstr_to_time_t(char *cstr, const char char *cur=cstr; const char *year, *month, *mday; - const char *hour, *min, *sec, *msec PA_ATTR_UNUSED; + const char *hour, *min, *sec; + PA_UNUSED const char *msec; year=skip_number(&cur, "-:", &delim); if(delim != ':' || delim == ':' && strlen(year) >=4 ){ @@ -254,8 +264,8 @@ static void _create(Request& r, MethodPa vdate.set_tz(tz); vdate.set_tm(tmIn); } else { // ^create(float days) or ^create[date object] - if(Value* adate=params[0].as(VDATE_TYPE)) - vdate.set_tz(static_cast(adate)->get_tz()); + if(VDate* adate=dynamic_cast(¶ms[0])) + vdate.set_tz(adate->get_tz()); vdate.set_time(round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY)); } } else { // ^create(y;m;d[;h[;m[;s[;TZ]]]]) @@ -389,7 +399,7 @@ static void _roll(Request& r, MethodPara static Table& fill_month_days(Request& r, MethodParams& params, bool rus){ Table::Action_options table_options; - Table& result=*new Table(date_calendar_table_template, table_options); + Table& result=*new Table(date_calendar_table_template(), table_options); tm tmIn; memset(&tmIn, 0, sizeof(tmIn)); @@ -525,6 +535,21 @@ static void _last_day(Request& r, Method r.write(*new VInt(VDate::getMonthDays(tmIn.tm_year, tmIn.tm_mon))); } +static void _int(Request& r, MethodParams&) { + VDate& vdate=GET_SELF(r, VDate); + r.write(*new VInt(vdate.as_int())); +} + +static void _double(Request& r, MethodParams&) { + VDate& vdate=GET_SELF(r, VDate); + r.write(*new VDouble(vdate.as_double())); +} + +static void _bool(Request& r, MethodParams&) { + VDate& vdate=GET_SELF(r, VDate); + r.write(VBool::get(vdate.as_bool())); +} + // constructor MDate::MDate(): Methoded("date") { @@ -533,7 +558,8 @@ MDate::MDate(): Methoded("date") { add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1); // ^date::today[] - add_native_method("today", Method::CT_DYNAMIC, _today, 0, 0); + // ^date::today(offset int days) + add_native_method("today", Method::CT_DYNAMIC, _today, 0, 1); // ^date::create(float days) // ^date::create[date] @@ -567,4 +593,13 @@ MDate::MDate(): Methoded("date") { // ^date.unix-timestamp[] // ^date::unix-timestamp(timestamp) add_native_method("unix-timestamp", Method::CT_DYNAMIC, _unix_timestamp, 0, 1); + + // date.int[default for ^string.int compatibility] + add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1); + + // ^date.double[default for ^string.double compatibility] + add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1); + + // ^date.bool[default for ^string.bool compatibility] + add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1); }