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

1.1       parser      1: /** @file
                      2:        Parser: @b date parser class.
                      3: 
1.15    ! paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.12      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: 
1.15    ! paf         7:        $Id: date.C,v 1.14 2001/12/21 14:08:55 paf Exp $
1.1       parser      8: */
                      9: 
                     10: #include "classes.h"
                     11: #include "pa_request.h"
                     12: #include "pa_vdouble.h"
                     13: #include "pa_vdate.h"
1.10      parser     14: #include "pa_vtable.h"
1.1       parser     15: 
                     16: // defines
                     17: 
                     18: #define DATE_CLASS_NAME "date"
                     19: 
                     20: // class
                     21: 
                     22: class MDate : public Methoded {
                     23: public: // VStateless_class
                     24:        Value *create_new_value(Pool& pool) { return new(pool) VDate(pool, 0); }
                     25: 
                     26: public:
                     27:        MDate(Pool& pool);
                     28: public: // Methoded
                     29:        bool used_directly() { return true; }
                     30: };
                     31: 
                     32: // methods
                     33: 
                     34: static void _now(Request& r, const String& method_name, MethodParams *) {
                     35:        Pool& pool=r.pool();
                     36:        VDate *vdate=static_cast<VDate *>(r.self);
                     37:        vdate->set_time(time(0));
                     38: }
                     39: 
                     40: static void _set(Request& r, const String& method_name, MethodParams *params) {
                     41:        Pool& pool=r.pool();
                     42:        VDate *vdate=static_cast<VDate *>(r.self);
                     43: 
                     44:        time_t time;
                     45:        if(params->size()==1) // ^set(float days)
1.9       parser     46:                time=(time_t)(params->as_double(0, "float days must be double", r)*SECS_PER_DAY);
1.1       parser     47:        else if(params->size()>=3) { // ^set(y;m;d[;h[;m[;s]]])
                     48:                tm tmIn={0};
                     49:                tmIn.tm_isdst=-1;
1.9       parser     50:                int year=params->as_int(0, "year must be int", r);
1.1       parser     51:                if(year<70) // 0..69 -> 100..169 [2000..2069]
                     52:                        year+=100;
1.3       parser     53:                if(year>=1900)
                     54:                        year-=1900;
1.1       parser     55:                tmIn.tm_year=year;
1.9       parser     56:                tmIn.tm_mon=params->as_int(1, "month must be int", r)-1;
                     57:                tmIn.tm_mday=params->as_int(2, "mday must be int", r);
                     58:                if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r);
                     59:                if(params->size()>4) tmIn.tm_min=params->as_int(4, "minutes must be int", r);
                     60:                if(params->size()>5) tmIn.tm_sec=params->as_int(5, "seconds must be int", r);
1.1       parser     61:                time=mktime(&tmIn);
                     62:                if(time<0)
1.11      parser     63:                        throw Exception(0, 0,
1.1       parser     64:                                &method_name,
                     65:                                "invalid datetime");
                     66:        } else
1.11      parser     67:                throw Exception(0, 0,
1.1       parser     68:                        &method_name,
                     69:                        "invalid params count, must be 1 or >=3");
                     70:        vdate->set_time(time);
                     71: }
                     72: 
1.5       parser     73: static void _sql_string(Request& r, const String& method_name, MethodParams *) {
1.1       parser     74:        Pool& pool=r.pool();
                     75:        VDate *vdate=static_cast<VDate *>(r.self);
1.2       parser     76:        int size=1+ 4+1+2+1+2 +1+ 2+1+2+1+2 +1 +1;
1.1       parser     77:        char *buf=(char *)pool.malloc(size);
                     78:        time_t time=vdate->get_time();
1.14      paf        79:        size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", localtime(&time));
1.1       parser     80:        
1.4       parser     81:        String& string=*new(pool) String(pool);
                     82:        string.APPEND_CLEAN(buf, size, 
                     83:                method_name.origin().file, 
                     84:                method_name.origin().line);
                     85:        Value& result=*new(pool) VString(string);
1.1       parser     86:        r.write_assign_lang(result);
                     87: }
                     88: 
                     89: 
                     90: static void _roll(Request& r, const String& method_name, MethodParams *params) {
                     91:        Pool& pool=r.pool();
                     92:        VDate *vdate=static_cast<VDate *>(r.self);
                     93: 
                     94:        const String& what=params->as_string(0, "'what' must be string");
                     95:     int oyear=0;
                     96:     int omonth=0;
                     97:     int oday=0;
                     98:        int *offset;
                     99:        if(what=="year") offset=&oyear;
                    100:        else if(what=="month") offset=&omonth;
                    101:        else if(what=="day") offset=&oday;
                    102:        else
1.11      parser    103:                throw Exception(0, 0,
1.1       parser    104:                        &what,
                    105:                        "must be year|month|day");
                    106:        
1.9       parser    107:        *offset=params->as_int(1, "offset must be int", r);
1.1       parser    108:        if(!(*offset==1 || *offset==-1))
1.11      parser    109:                throw Exception(0, 0,
1.1       parser    110:                        &method_name,
                    111:                        "offset must be +/- 1");
                    112: 
1.13      paf       113:        time_t self_time=vdate->get_time();
                    114:        tm tmIn=*localtime(&self_time);
                    115:        // we will preserve daytime from day-light-saving shifts across roll
                    116:        tm tmSaved=tmIn;
                    117:        tmIn.tm_hour=24/2; tmIn.tm_min=tmIn.tm_sec=0;
                    118: 
                    119:     tmIn.tm_year+=oyear;
                    120:     time_t t=mktime(&tmIn);
                    121:     t+=omonth*getMonthDays(tmIn.tm_year, (tmIn.tm_mon+(omonth<0?-1:0)+12)%12)*SECS_PER_DAY;
                    122:     t+=oday*SECS_PER_DAY;
                    123:     
                    124:     tm *tmOut=localtime(&t);
                    125:        if(!tmOut)
1.11      parser    126:                throw Exception(0, 0,
1.1       parser    127:                        &method_name,
1.13      paf       128:                        "bad resulting time (seconds from epoch=%ld)", t);
                    129:     
                    130:     tmOut->tm_hour=tmSaved.tm_hour;
                    131:     tmOut->tm_min=tmSaved.tm_min;
                    132:     tmOut->tm_sec=tmSaved.tm_sec;
                    133:        {
                    134:                time_t t=mktime(tmOut);
                    135:                if(t<0)
                    136:                        throw Exception(0, 0,
                    137:                        &method_name,
                    138:                        "bad resulting time (after reconstruction)");
                    139:                
                    140:                vdate->set_time(t);
                    141:        }
1.1       parser    142: }
                    143: 
1.10      parser    144: static Table *fill_month_days(Request& r, 
                    145:                                                          const String& method_name, MethodParams *params, bool rus){
                    146:        Pool& pool=r.pool();
                    147:        Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/);
                    148: 
                    149:     int year=params->as_int(1, "year must be int", r);
                    150:     int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
                    151: 
                    152:     tm tmIn={0, 0, 0, 1, month, year-1900};
                    153:     time_t t=mktime(&tmIn);
                    154:        if(t<0)
1.11      parser    155:                throw Exception(0, 0, 
1.10      parser    156:                        &method_name, 
                    157:                        "invalid date");
                    158:     tm *tmOut=localtime(&t);
                    159: 
                    160:     int weekDay1=tmOut->tm_wday;
                    161:        if(rus) 
                    162:                weekDay1=weekDay1?weekDay1-1:6; //sunday last
                    163:     int monthDays=getMonthDays(year, month);
                    164:     
                    165:     for(int _day=1-weekDay1; _day<=monthDays;) {
                    166:                Array& row=*new(pool) Array(pool, 7);
                    167:        for(int wday=0; wday<7; wday++, _day++) {
                    168:                String *cell=new(pool) String(pool);
                    169:                        if(_day>=1 && _day<=monthDays) {
                    170:                                char *buf=(char *)pool.malloc(2+1); 
                    171:                                cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day), 
                    172:                                        method_name.origin().file, method_name.origin().line);
                    173:             }
                    174:                        row+=cell;            
                    175:         }
                    176:        *result+=&row;
                    177:     }
                    178:     
                    179:     return result;
                    180: }
                    181: 
                    182: static Table *fill_week_days(Request& r, 
                    183:                                                         const String& method_name, MethodParams *params, bool rus){
                    184:        Pool& pool=r.pool();
                    185:        Array& columns=*new(pool) Array(pool, 4);
                    186:        Table *result=new(pool) Table(pool, &method_name, &columns);
                    187: 
                    188:     int year=params->as_int(1, "year must be int", r);
                    189:     int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
                    190:     int day=params->as_int(3, "day must be int", r);
                    191:     
                    192:     tm tmIn={0, 0, 18, day, month, year-1900};
                    193:     time_t t=mktime(&tmIn);
                    194:        if(t<0)
1.11      parser    195:                throw Exception(0, 0, 
1.10      parser    196:                        &method_name, 
                    197:                        "invalid date");
                    198:     tm *tmOut=localtime(&t);
                    199:     
                    200:     int baseWeekDay=tmOut->tm_wday;
                    201:        if(rus) 
                    202:                baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
                    203: 
                    204:     t-=baseWeekDay*SECS_PER_DAY;
                    205: 
                    206:     for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
                    207:         tm *tmOut=localtime(&t);
                    208:                Array& row=*new(pool) Array(pool, 4);
                    209: #define WDFILL(size, value) { \
                    210:                char *buf=(char *)pool.malloc(size+1); \
                    211:                String *cell=new(pool) String(pool); \
                    212:                cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
                    213:                        method_name.origin().file, \
                    214:                        method_name.origin().line); \
                    215:                row+=cell; \
                    216:                }
                    217:                WDFILL(4, 1900+tmOut->tm_year);
                    218:                WDFILL(2, 1+tmOut->tm_mon);
                    219:                WDFILL(2, tmOut->tm_mday);
                    220:                WDFILL(2, tmOut->tm_wday);
                    221:         *result+=&row;
                    222:     }
                    223:     
                    224:     return result;
                    225: }
                    226: 
                    227: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
                    228:        Pool& pool=r.pool();
                    229: 
                    230:        const String& what=params->as_string(0, "format must be strig");
                    231:        bool rus=false;
                    232:        if(what=="rus")
                    233:                rus=true;
                    234:        else if(what=="eng")
                    235:                rus=false;
                    236:        else
1.11      parser    237:                throw Exception(0, 0, 
1.10      parser    238:                        &what, 
                    239:                        "must be rus|eng");
                    240: 
                    241:        Table *table;
                    242:        if(params->size()==1+2) 
                    243:                table=fill_month_days(r, method_name, params, rus);
                    244:        else // 1+3
                    245:                table=fill_week_days(r, method_name, params, rus);
                    246: 
                    247:        VTable& result=*new(pool) VTable(pool, table);
                    248:        result.set_name(method_name);
                    249:        r.write_no_lang(result);
                    250: }
                    251: 
1.1       parser    252: // constructor
                    253: 
                    254: MDate::MDate(Pool& apool) : Methoded(apool) {
                    255:        set_name(*NEW String(pool(), DATE_CLASS_NAME));
                    256: 
                    257: 
                    258:        // ^now[]
                    259:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 0);
                    260: 
                    261:        // ^set(float days)
                    262:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 6);
                    263: 
1.6       parser    264:        // ^sql-string[]
1.5       parser    265:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1       parser    266: 
                    267:        // ^roll(year|month|day;+/- 1)
                    268:        add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10      parser    269: 
                    270:        // ^date:calendar[month|montheng;year;month]  = table
                    271:        // ^date:calendar[week|weekeng;year;month;day] = table
                    272:        add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1       parser    273: 
                    274: }
                    275: // global variable
                    276: 
                    277: Methoded *date_class;
                    278: 
                    279: // creator
                    280: 
                    281: Methoded *MDate_create(Pool& pool) {
                    282:        return date_class=new(pool) MDate(pool);
                    283: }

E-mail: