Annotation of parser3/src/classes/date.C, revision 1.7

1.1       parser      1: /** @file
                      2:        Parser: @b date parser class.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5: 
                      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: */
1.7     ! parser      8: static const char *RCSId="$Id: date.C,v 1.6 2001/09/04 10:50:19 parser Exp $"; 
1.1       parser      9: 
                     10: #include "classes.h"
                     11: #include "pa_request.h"
                     12: #include "pa_vdouble.h"
                     13: #include "pa_vdate.h"
                     14: 
                     15: // defines
                     16: 
                     17: #define DATE_CLASS_NAME "date"
                     18: 
                     19: // class
                     20: 
                     21: class MDate : public Methoded {
                     22: public: // VStateless_class
                     23:        Value *create_new_value(Pool& pool) { return new(pool) VDate(pool, 0); }
                     24: 
                     25: public:
                     26:        MDate(Pool& pool);
                     27: public: // Methoded
                     28:        bool used_directly() { return true; }
                     29: };
                     30: 
                     31: // methods
                     32: 
                     33: static void _now(Request& r, const String& method_name, MethodParams *) {
                     34:        Pool& pool=r.pool();
                     35:        VDate *vdate=static_cast<VDate *>(r.self);
                     36:        vdate->set_time(time(0));
                     37: }
                     38: 
                     39: static void _set(Request& r, const String& method_name, MethodParams *params) {
                     40:        Pool& pool=r.pool();
                     41:        VDate *vdate=static_cast<VDate *>(r.self);
                     42: 
                     43:        time_t time;
                     44:        if(params->size()==1) // ^set(float days)
                     45:                time=(time_t)(params->as_double(0, r)*SECS_PER_DAY);
                     46:        else if(params->size()>=3) { // ^set(y;m;d[;h[;m[;s]]])
                     47:                tm tmIn={0};
                     48:                tmIn.tm_isdst=-1;
                     49:                int year=params->as_int(0, r);
                     50:                if(year<70) // 0..69 -> 100..169 [2000..2069]
                     51:                        year+=100;
1.3       parser     52:                if(year>=1900)
                     53:                        year-=1900;
1.1       parser     54:                tmIn.tm_year=year;
                     55:                tmIn.tm_mon=params->as_int(1, r)-1;
                     56:                tmIn.tm_mday=params->as_int(2, r);
                     57:                if(params->size()>3) tmIn.tm_hour=params->as_int(3, r);
                     58:                if(params->size()>4) tmIn.tm_min=params->as_int(4, r);
                     59:                if(params->size()>5) tmIn.tm_sec=params->as_int(5, r);
                     60:                time=mktime(&tmIn);
                     61:                if(time<0)
                     62:                        PTHROW(0, 0,
                     63:                                &method_name,
                     64:                                "invalid datetime");
                     65:        } else
                     66:                PTHROW(0, 0,
                     67:                        &method_name,
                     68:                        "invalid params count, must be 1 or >=3");
                     69:        vdate->set_time(time);
                     70: }
                     71: 
1.5       parser     72: static void _sql_string(Request& r, const String& method_name, MethodParams *) {
1.1       parser     73:        Pool& pool=r.pool();
                     74:        VDate *vdate=static_cast<VDate *>(r.self);
1.2       parser     75:        int size=1+ 4+1+2+1+2 +1+ 2+1+2+1+2 +1 +1;
1.1       parser     76:        char *buf=(char *)pool.malloc(size);
                     77:        time_t time=vdate->get_time();
1.5       parser     78:        size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", gmtime(&time));
1.1       parser     79:        
1.4       parser     80:        String& string=*new(pool) String(pool);
                     81:        string.APPEND_CLEAN(buf, size, 
                     82:                method_name.origin().file, 
                     83:                method_name.origin().line);
                     84:        Value& result=*new(pool) VString(string);
1.1       parser     85:        r.write_assign_lang(result);
                     86: }
                     87: 
                     88: 
                     89: static void _roll(Request& r, const String& method_name, MethodParams *params) {
                     90:        Pool& pool=r.pool();
                     91:        VDate *vdate=static_cast<VDate *>(r.self);
                     92: 
                     93:        const String& what=params->as_string(0, "'what' must be string");
                     94:     int oyear=0;
                     95:     int omonth=0;
                     96:     int oday=0;
                     97:        int *offset;
                     98:        if(what=="year") offset=&oyear;
                     99:        else if(what=="month") offset=&omonth;
                    100:        else if(what=="day") offset=&oday;
                    101:        else
                    102:                PTHROW(0, 0,
                    103:                        &what,
                    104:                        "must be year|month|day");
                    105:        
                    106:        *offset=params->as_int(1, r);
                    107:        if(!(*offset==1 || *offset==-1))
                    108:                PTHROW(0, 0,
                    109:                        &method_name,
                    110:                        "offset must be +/- 1");
                    111: 
                    112:        time_t tIn=vdate->get_time();
                    113:     tm *tmIn=localtime(&tIn);
                    114:        tmIn->tm_year+=oyear;
                    115:        time_t t=mktime(tmIn);
                    116:        if(t<0)
                    117:                PTHROW(0, 0,
                    118:                        &method_name,
                    119:                        "invalid datetime");
                    120:        t+=omonth*getMonthDays(tmIn->tm_year, (tmIn->tm_mon+(omonth<0?-1:0)+12)%12)*SECS_PER_DAY;
                    121:     t+=oday*SECS_PER_DAY;
                    122:        vdate->set_time(t);
                    123: }
                    124: 
                    125: // constructor
                    126: 
                    127: MDate::MDate(Pool& apool) : Methoded(apool) {
                    128:        set_name(*NEW String(pool(), DATE_CLASS_NAME));
                    129: 
                    130: 
                    131:        // ^now[]
                    132:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 0);
                    133: 
                    134:        // ^set(float days)
                    135:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 6);
                    136: 
1.6       parser    137:        // ^sql-string[]
1.5       parser    138:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1       parser    139: 
                    140:        // ^roll(year|month|day;+/- 1)
                    141:        add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
                    142: 
                    143: }
                    144: // global variable
                    145: 
                    146: Methoded *date_class;
                    147: 
                    148: // creator
                    149: 
                    150: Methoded *MDate_create(Pool& pool) {
                    151:        return date_class=new(pool) MDate(pool);
                    152: }

E-mail: