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

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

E-mail: