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

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

E-mail: