|
|
| version 1.26, 2002/04/16 08:59:25 | version 1.28, 2002/04/25 14:35:20 |
|---|---|
| Line 13 | Line 13 |
| #include "pa_vdate.h" | #include "pa_vdate.h" |
| #include "pa_vtable.h" | #include "pa_vtable.h" |
| // defines | |
| #define DATE_CLASS_NAME "date" | |
| // class | // class |
| class MDate : public Methoded { | class MDate : public Methoded { |
| Line 42 static void _now(Request& r, const Strin | Line 38 static void _now(Request& r, const Strin |
| vdate->set_time(t); | 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; | |
| } | |
| /// @test 09 ok? [octal maybe] | |
| static void _create(Request& r, const String& method_name, MethodParams *params) { | static void _create(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VDate *vdate=static_cast<VDate *>(r.self); | VDate *vdate=static_cast<VDate *>(r.self); |
| time_t t; | time_t t; |
| if(params->size()==1) { // ^set(float days) | if(params->size()==1) { |
| t=(time_t)(params->as_double(0, "float days must be double", r)*SECS_PER_DAY); | if(const String *sdate=params->get(0).get_string()) { // ^create[2002-04-25 18:14:00] |
| if(t<0 || !localtime(&t)) | char *cstr=sdate->cstr(); |
| throw Exception(0, | const char *year=lsplit(&cstr, '-'); |
| &method_name, | const char *month=lsplit(&cstr, '-'); |
| "invalid datetime"); | const char *mday=lsplit(&cstr, ' '); |
| const char *hour=lsplit(&cstr, ':'); | |
| const char *min=lsplit(&cstr, ':'); | |
| const char *sec=cstr; | |
| if(!year) | |
| throw Exception(0, | |
| sdate, | |
| "no part of date specified, must be in YYYY-NN-DD HH-MM-SS format"); | |
| tm tmIn={0}; | |
| tmIn.tm_isdst=-1; | |
| 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_hour=hour?atoi(hour):0; | |
| tmIn.tm_min=min?atoi(min):0; | |
| tmIn.tm_sec=sec?atoi(sec):0; | |
| t=mktime(&tmIn); | |
| if(t<0) | |
| throw Exception(0, | |
| sdate, | |
| "invalid datetime"); | |
| } else { // ^create(float days) | |
| t=(time_t)(params->as_double(0, "float days must be double", r)*SECS_PER_DAY); | |
| if(t<0 || !localtime(&t)) | |
| throw Exception(0, | |
| &method_name, | |
| "invalid datetime"); | |
| } | |
| } else if(params->size()>=2) { // ^set(y;m;d[;h[;m[;s]]]) | } else if(params->size()>=2) { // ^set(y;m;d[;h[;m[;s]]]) |
| tm tmIn={0}; | tm tmIn={0}; |
| tmIn.tm_isdst=-1; | tmIn.tm_isdst=-1; |
| int year=params->as_int(0, "year must be int", r); | tmIn.tm_year=NN_year_to_NNNN(params->as_int(0, "year must be int", r)); |
| if(year<70) // 0..69 -> 100..169 [2000..2069] | |
| year+=100; | |
| if(year>=1900) | |
| year-=1900; | |
| tmIn.tm_year=year; | |
| tmIn.tm_mon=params->as_int(1, "month must be int", r)-1; | tmIn.tm_mon=params->as_int(1, "month must be int", r)-1; |
| tmIn.tm_mday=params->size()>2?params->as_int(2, "mday must be int", r):1; | tmIn.tm_mday=params->size()>2?params->as_int(2, "mday must be int", r):1; |
| if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r); | if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r); |
| Line 270 static void _calendar(Request& r, const | Line 297 static void _calendar(Request& r, const |
| table=fill_week_days(r, method_name, params, rus); | table=fill_week_days(r, method_name, params, rus); |
| VTable& result=*new(pool) VTable(pool, table); | VTable& result=*new(pool) VTable(pool, table); |
| result.set_name(method_name); | |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| // constructor | // constructor |
| MDate::MDate(Pool& apool) : Methoded(apool) { | MDate::MDate(Pool& apool) : Methoded(apool, "date") { |
| set_name(*NEW String(pool(), DATE_CLASS_NAME)); | |
| // ^now[] | // ^now[] |
| add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1); | add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1); |
| // ^set(float days) | // ^set(float days) |
| add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6); | add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6); |
| // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf | |
| add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6); | add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6); |
| // ^sql-string[] | // ^sql-string[] |