--- parser3/src/classes/date.C 2002/04/16 08:59:25 1.26 +++ parser3/src/classes/date.C 2002/08/01 11:26:43 1.32 @@ -3,20 +3,16 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: date.C,v 1.26 2002/04/16 08:59:25 paf Exp $ */ +static const char* IDENT_DATE_C="$Id: date.C,v 1.32 2002/08/01 11:26:43 paf Exp $"; + #include "classes.h" #include "pa_request.h" #include "pa_vdouble.h" #include "pa_vdate.h" #include "pa_vtable.h" -// defines - -#define DATE_CLASS_NAME "date" - // class class MDate : public Methoded { @@ -42,26 +38,71 @@ static void _now(Request& r, const Strin 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) { Pool& pool=r.pool(); VDate *vdate=static_cast(r.self); time_t t; - if(params->size()==1) { // ^set(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"); + if(params->size()==1) { + // ^create[2002-04-25 18:14:00] + // ^create[18:14:00] + if(const String *sdate=params->get(0).get_string()) { + char *cstr=sdate->cstr(); + char *cur=cstr; + const char *year=lsplit(&cur, '-'); + const char *month=lsplit(&cur, '-'); + const char *mday=lsplit(&cur, ' '); + if(!month) + cur=cstr; + const char *hour=lsplit(&cur, ':'); + const char *min=lsplit(&cur, ':'); + const char *sec=cur; + + tm tmIn={0}; + tmIn.tm_isdst=-1; + if(!month) + if(min) { + year=mday=0; // HH:MM + time_t t=time(0); + tm *tmNow=localtime(&t); + tmIn.tm_year=tmNow->tm_year; + tmIn.tm_mon=tmNow->tm_mon; + tmIn.tm_mday=tmNow->tm_mday; + 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; +date_part_set: + 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]]]) tm tmIn={0}; tmIn.tm_isdst=-1; - int year=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_year=NN_year_to_NNNN(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->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); @@ -270,21 +311,18 @@ static void _calendar(Request& r, const table=fill_week_days(r, method_name, params, rus); VTable& result=*new(pool) VTable(pool, table); - result.set_name(method_name); r.write_no_lang(result); } // constructor -MDate::MDate(Pool& apool) : Methoded(apool) { - set_name(*NEW String(pool(), DATE_CLASS_NAME)); - - +MDate::MDate(Pool& apool) : Methoded(apool, "date") { // ^now[] add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1); - // ^set(float days) + // ^create(float days) 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); // ^sql-string[]