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

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.2     ! parser      8:        $Id: date.C,v 1.1 2001/07/07 16:38:01 parser Exp $
1.1       parser      9: */
1.2     ! parser     10: static const char *RCSId="$Id: date.C,v 1.1 2001/07/07 16:38:01 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;
                     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: 
                     72: static void _string(Request& r, const String& method_name, MethodParams *) {
                     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.2     ! parser     78:        size=strftime(buf, size, "'%Y-%m-%d %H:%M:%S'", gmtime(&time));
1.1       parser     79:        
                     80:        Value& result=*new(pool) VString(*new(pool) String(pool, buf, size));
                     81:        r.write_assign_lang(result);
                     82: }
                     83: 
                     84: 
                     85: static void _roll(Request& r, const String& method_name, MethodParams *params) {
                     86:        Pool& pool=r.pool();
                     87:        VDate *vdate=static_cast<VDate *>(r.self);
                     88: 
                     89:        const String& what=params->as_string(0, "'what' must be string");
                     90:     int oyear=0;
                     91:     int omonth=0;
                     92:     int oday=0;
                     93:        int *offset;
                     94:        if(what=="year") offset=&oyear;
                     95:        else if(what=="month") offset=&omonth;
                     96:        else if(what=="day") offset=&oday;
                     97:        else
                     98:                PTHROW(0, 0,
                     99:                        &what,
                    100:                        "must be year|month|day");
                    101:        
                    102:        *offset=params->as_int(1, r);
                    103:        if(!(*offset==1 || *offset==-1))
                    104:                PTHROW(0, 0,
                    105:                        &method_name,
                    106:                        "offset must be +/- 1");
                    107: 
                    108:        time_t tIn=vdate->get_time();
                    109:     tm *tmIn=localtime(&tIn);
                    110:        tmIn->tm_year+=oyear;
                    111:        time_t t=mktime(tmIn);
                    112:        if(t<0)
                    113:                PTHROW(0, 0,
                    114:                        &method_name,
                    115:                        "invalid datetime");
                    116:        t+=omonth*getMonthDays(tmIn->tm_year, (tmIn->tm_mon+(omonth<0?-1:0)+12)%12)*SECS_PER_DAY;
                    117:     t+=oday*SECS_PER_DAY;
                    118:        vdate->set_time(t);
                    119: }
                    120: 
                    121: // constructor
                    122: 
                    123: MDate::MDate(Pool& apool) : Methoded(apool) {
                    124:        set_name(*NEW String(pool(), DATE_CLASS_NAME));
                    125: 
                    126: 
                    127:        // ^now[]
                    128:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 0);
                    129: 
                    130:        // ^set(float days)
                    131:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 6);
                    132: 
                    133:        // ^string[]
                    134:        add_native_method("string", Method::CT_DYNAMIC, _string, 0, 0);
                    135: 
                    136:        // ^roll(year|month|day;+/- 1)
                    137:        add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
                    138: 
                    139: }
                    140: // global variable
                    141: 
                    142: Methoded *date_class;
                    143: 
                    144: // creator
                    145: 
                    146: Methoded *MDate_create(Pool& pool) {
                    147:        return date_class=new(pool) MDate(pool);
                    148: }

E-mail: