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

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.16      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: 
1.31    ! paf         7:        $Id: date.C,v 1.30 2002/05/15 08:58:00 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: // class
                     17: 
                     18: class MDate : public Methoded {
                     19: public: // VStateless_class
                     20:        Value *create_new_value(Pool& pool) { return new(pool) VDate(pool, 0); }
                     21: 
                     22: public:
                     23:        MDate(Pool& pool);
                     24: public: // Methoded
                     25:        bool used_directly() { return true; }
                     26: };
                     27: 
                     28: // methods
                     29: 
1.23      paf        30: static void _now(Request& r, const String& method_name, MethodParams *params) {
1.1       parser     31:        Pool& pool=r.pool();
                     32:        VDate *vdate=static_cast<VDate *>(r.self);
1.23      paf        33: 
                     34:        time_t t=time(0);
                     35:        if(params->size()==1) // ^now(offset)
                     36:                t+=(time_t)(params->as_double(0, "offset must be double", r)*SECS_PER_DAY);
                     37:        
                     38:        vdate->set_time(t);
1.1       parser     39: }
                     40: 
1.28      paf        41: static int NN_year_to_NNNN(int year) {
                     42:        if(year<70) // 0..69 -> 100..169 [2000..2069]
                     43:                year+=100;
                     44:        if(year>=1900)
                     45:                year-=1900;
                     46:        return year;
                     47: }
                     48: 
                     49: /// @test 09 ok? [octal maybe]
1.17      paf        50: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.1       parser     51:        Pool& pool=r.pool();
                     52:        VDate *vdate=static_cast<VDate *>(r.self);
                     53: 
1.23      paf        54:        time_t t;
1.28      paf        55:        if(params->size()==1) { 
1.31    ! paf        56:                // ^create[2002-04-25 18:14:00]
        !            57:                // ^create[18:14:00]
        !            58:                if(const String *sdate=params->get(0).get_string()) {
1.28      paf        59:                        char *cstr=sdate->cstr();
1.31    ! paf        60:                        char *cur=cstr;
        !            61:                        const char *year=lsplit(&cur, '-');
        !            62:                        const char *month=lsplit(&cur, '-');
        !            63:                        const char *mday=lsplit(&cur, ' ');
        !            64:                        if(!month)
        !            65:                                cur=cstr;
        !            66:                        const char *hour=lsplit(&cur, ':');
        !            67:                        const char *min=lsplit(&cur, ':');
        !            68:                        const char *sec=cur;
        !            69: 
1.28      paf        70:                        tm tmIn={0};
                     71:                        tmIn.tm_isdst=-1;
1.31    ! paf        72:                        if(!month)
        !            73:                                if(min) {
        !            74:                                        year=mday=0; // HH:MM
        !            75:                                        time_t t=time(0);
        !            76:                                        tm *tmNow=localtime(&t);
        !            77:                                        tmIn.tm_year=tmNow->tm_year;
        !            78:                                        tmIn.tm_mon=tmNow->tm_mon;
        !            79:                                        tmIn.tm_mday=tmNow->tm_mday;
        !            80:                                        goto date_part_set;
        !            81:                                } else
        !            82:                                        hour=min=sec=0; // not YYYY- & not HH: = just YYYY                                      
1.28      paf        83:                        tmIn.tm_year=NN_year_to_NNNN(atoi(year));
                     84:                        tmIn.tm_mon=month?atoi(month)-1:0;
                     85:                        tmIn.tm_mday=mday?atoi(mday):1;
1.31    ! paf        86: date_part_set:
1.28      paf        87:                        tmIn.tm_hour=hour?atoi(hour):0;
                     88:                        tmIn.tm_min=min?atoi(min):0;
                     89:                        tmIn.tm_sec=sec?atoi(sec):0;
                     90:                        t=mktime(&tmIn);
                     91:                        if(t<0)
                     92:                                throw Exception(0,
                     93:                                        sdate,
                     94:                                        "invalid datetime");
                     95:                } else { // ^create(float days)
                     96:                        t=(time_t)(params->as_double(0, "float days must be double", r)*SECS_PER_DAY);
                     97:                        if(t<0 || !localtime(&t))
                     98:                                throw Exception(0,
                     99:                                        &method_name,
                    100:                                        "invalid datetime");
                    101:                }
1.26      paf       102:        } else if(params->size()>=2) { // ^set(y;m;d[;h[;m[;s]]])
1.1       parser    103:                tm tmIn={0};
                    104:                tmIn.tm_isdst=-1;
1.28      paf       105:                tmIn.tm_year=NN_year_to_NNNN(params->as_int(0, "year must be int", r));
1.9       parser    106:                tmIn.tm_mon=params->as_int(1, "month must be int", r)-1;
1.24      paf       107:                tmIn.tm_mday=params->size()>2?params->as_int(2, "mday must be int", r):1;
1.9       parser    108:                if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r);
                    109:                if(params->size()>4) tmIn.tm_min=params->as_int(4, "minutes must be int", r);
                    110:                if(params->size()>5) tmIn.tm_sec=params->as_int(5, "seconds must be int", r);
1.23      paf       111:                t=mktime(&tmIn);
                    112:                if(t<0)
1.22      paf       113:                        throw Exception(0,
1.1       parser    114:                                &method_name,
                    115:                                "invalid datetime");
                    116:        } else
1.22      paf       117:                throw Exception("parser.runtime",
1.1       parser    118:                        &method_name,
1.24      paf       119:                        "invalid params count, must be 1 or >=2");
1.23      paf       120:        vdate->set_time(t);
1.1       parser    121: }
                    122: 
1.5       parser    123: static void _sql_string(Request& r, const String& method_name, MethodParams *) {
1.1       parser    124:        Pool& pool=r.pool();
                    125:        VDate *vdate=static_cast<VDate *>(r.self);
1.2       parser    126:        int size=1+ 4+1+2+1+2 +1+ 2+1+2+1+2 +1 +1;
1.1       parser    127:        char *buf=(char *)pool.malloc(size);
                    128:        time_t time=vdate->get_time();
1.14      paf       129:        size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", localtime(&time));
1.1       parser    130:        
1.4       parser    131:        String& string=*new(pool) String(pool);
                    132:        string.APPEND_CLEAN(buf, size, 
                    133:                method_name.origin().file, 
                    134:                method_name.origin().line);
1.25      paf       135:        r.write_assign_lang(string);
1.1       parser    136: }
                    137: 
                    138: 
                    139: static void _roll(Request& r, const String& method_name, MethodParams *params) {
                    140:        Pool& pool=r.pool();
                    141:        VDate *vdate=static_cast<VDate *>(r.self);
                    142: 
                    143:        const String& what=params->as_string(0, "'what' must be string");
                    144:     int oyear=0;
                    145:     int omonth=0;
                    146:     int oday=0;
                    147:        int *offset;
                    148:        if(what=="year") offset=&oyear;
                    149:        else if(what=="month") offset=&omonth;
                    150:        else if(what=="day") offset=&oday;
                    151:        else
1.22      paf       152:                throw Exception("parser.runtime",
1.1       parser    153:                        &what,
                    154:                        "must be year|month|day");
                    155:        
1.9       parser    156:        *offset=params->as_int(1, "offset must be int", r);
1.1       parser    157: 
1.13      paf       158:        time_t self_time=vdate->get_time();
                    159:        tm tmIn=*localtime(&self_time);
                    160:        tm tmSaved=tmIn;
                    161: 
1.21      paf       162:        tmIn.tm_year+=oyear;
                    163:        tmIn.tm_mon+=omonth;
                    164:        tmIn.tm_mday+=oday;
                    165:        tmIn.tm_hour=24/2; 
                    166:        tmIn.tm_min=0;
                    167:        tmIn.tm_sec=0;
                    168:        time_t t=mktime/*normalizetime*/(&tmIn);
                    169:        if(t<0)
1.22      paf       170:                throw Exception(0,
1.21      paf       171:                &method_name,
                    172:                "bad resulting time (after roll)");
1.24      paf       173:        if(oday==0 && tmIn.tm_mday!=tmSaved.tm_mday)
                    174:                throw Exception(0,
                    175:                        &method_name,
                    176:                        "bad resulting time (day hole)", t);
1.21      paf       177: 
1.13      paf       178:     tm *tmOut=localtime(&t);
                    179:        if(!tmOut)
1.22      paf       180:                throw Exception(0,
1.1       parser    181:                        &method_name,
1.13      paf       182:                        "bad resulting time (seconds from epoch=%ld)", t);
                    183:     
                    184:     tmOut->tm_hour=tmSaved.tm_hour;
                    185:     tmOut->tm_min=tmSaved.tm_min;
                    186:     tmOut->tm_sec=tmSaved.tm_sec;
1.20      paf       187:        tmOut->tm_isdst=-1; 
1.13      paf       188:        {
1.21      paf       189:                time_t t=mktime/*normalizetime*/(tmOut);
                    190:                if(
                    191:                        tmOut->tm_hour!=tmSaved.tm_hour
                    192:                        ||tmOut->tm_min!=tmSaved.tm_min)
1.22      paf       193:                        throw Exception(0,
1.21      paf       194:                                &method_name,
1.24      paf       195:                                "bad resulting time (hour hole)");
1.21      paf       196: 
1.13      paf       197:                if(t<0)
1.22      paf       198:                        throw Exception(0,
1.21      paf       199:                                &method_name,
                    200:                                "bad resulting time (after reconstruction)");
1.13      paf       201:                
                    202:                vdate->set_time(t);
                    203:        }
1.1       parser    204: }
                    205: 
1.10      parser    206: static Table *fill_month_days(Request& r, 
                    207:                                                          const String& method_name, MethodParams *params, bool rus){
                    208:        Pool& pool=r.pool();
                    209:        Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/);
                    210: 
                    211:     int year=params->as_int(1, "year must be int", r);
                    212:     int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
                    213: 
                    214:     tm tmIn={0, 0, 0, 1, month, year-1900};
                    215:     time_t t=mktime(&tmIn);
                    216:        if(t<0)
1.22      paf       217:                throw Exception(0, 
1.10      parser    218:                        &method_name, 
                    219:                        "invalid date");
                    220:     tm *tmOut=localtime(&t);
                    221: 
                    222:     int weekDay1=tmOut->tm_wday;
                    223:        if(rus) 
                    224:                weekDay1=weekDay1?weekDay1-1:6; //sunday last
                    225:     int monthDays=getMonthDays(year, month);
                    226:     
                    227:     for(int _day=1-weekDay1; _day<=monthDays;) {
                    228:                Array& row=*new(pool) Array(pool, 7);
                    229:        for(int wday=0; wday<7; wday++, _day++) {
                    230:                String *cell=new(pool) String(pool);
                    231:                        if(_day>=1 && _day<=monthDays) {
                    232:                                char *buf=(char *)pool.malloc(2+1); 
                    233:                                cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day), 
                    234:                                        method_name.origin().file, method_name.origin().line);
                    235:             }
                    236:                        row+=cell;            
                    237:         }
                    238:        *result+=&row;
                    239:     }
                    240:     
                    241:     return result;
                    242: }
                    243: 
                    244: static Table *fill_week_days(Request& r, 
                    245:                                                         const String& method_name, MethodParams *params, bool rus){
                    246:        Pool& pool=r.pool();
                    247:        Array& columns=*new(pool) Array(pool, 4);
1.18      paf       248:        columns+=new(pool) String(pool, "year");
                    249:        columns+=new(pool) String(pool, "month");
                    250:        columns+=new(pool) String(pool, "day");
1.19      paf       251:        columns+=new(pool) String(pool, "weekday");
1.10      parser    252:        Table *result=new(pool) Table(pool, &method_name, &columns);
                    253: 
                    254:     int year=params->as_int(1, "year must be int", r);
                    255:     int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
                    256:     int day=params->as_int(3, "day must be int", r);
                    257:     
                    258:     tm tmIn={0, 0, 18, day, month, year-1900};
                    259:     time_t t=mktime(&tmIn);
                    260:        if(t<0)
1.22      paf       261:                throw Exception(0, 
1.10      parser    262:                        &method_name, 
                    263:                        "invalid date");
                    264:     tm *tmOut=localtime(&t);
                    265:     
                    266:     int baseWeekDay=tmOut->tm_wday;
                    267:        if(rus) 
                    268:                baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
                    269: 
                    270:     t-=baseWeekDay*SECS_PER_DAY;
                    271: 
                    272:     for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
                    273:         tm *tmOut=localtime(&t);
                    274:                Array& row=*new(pool) Array(pool, 4);
                    275: #define WDFILL(size, value) { \
                    276:                char *buf=(char *)pool.malloc(size+1); \
                    277:                String *cell=new(pool) String(pool); \
                    278:                cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
                    279:                        method_name.origin().file, \
                    280:                        method_name.origin().line); \
                    281:                row+=cell; \
                    282:                }
                    283:                WDFILL(4, 1900+tmOut->tm_year);
                    284:                WDFILL(2, 1+tmOut->tm_mon);
                    285:                WDFILL(2, tmOut->tm_mday);
                    286:                WDFILL(2, tmOut->tm_wday);
                    287:         *result+=&row;
                    288:     }
                    289:     
                    290:     return result;
                    291: }
                    292: 
                    293: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
                    294:        Pool& pool=r.pool();
                    295: 
                    296:        const String& what=params->as_string(0, "format must be strig");
                    297:        bool rus=false;
                    298:        if(what=="rus")
                    299:                rus=true;
                    300:        else if(what=="eng")
                    301:                rus=false;
                    302:        else
1.22      paf       303:                throw Exception("parser.runtime", 
1.10      parser    304:                        &what, 
                    305:                        "must be rus|eng");
                    306: 
                    307:        Table *table;
                    308:        if(params->size()==1+2) 
                    309:                table=fill_month_days(r, method_name, params, rus);
                    310:        else // 1+3
                    311:                table=fill_week_days(r, method_name, params, rus);
                    312: 
                    313:        VTable& result=*new(pool) VTable(pool, table);
                    314:        r.write_no_lang(result);
                    315: }
                    316: 
1.1       parser    317: // constructor
                    318: 
1.27      paf       319: MDate::MDate(Pool& apool) : Methoded(apool, "date") {
1.1       parser    320:        // ^now[]
1.23      paf       321:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1       parser    322: 
1.30      paf       323:        // ^create(float days)
1.17      paf       324:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
1.28      paf       325:        // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.17      paf       326:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1       parser    327: 
1.6       parser    328:        // ^sql-string[]
1.5       parser    329:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1       parser    330: 
                    331:        // ^roll(year|month|day;+/- 1)
                    332:        add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10      parser    333: 
                    334:        // ^date:calendar[month|montheng;year;month]  = table
                    335:        // ^date:calendar[week|weekeng;year;month;day] = table
                    336:        add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1       parser    337: 
                    338: }
                    339: // global variable
                    340: 
                    341: Methoded *date_class;
                    342: 
                    343: // creator
                    344: 
                    345: Methoded *MDate_create(Pool& pool) {
                    346:        return date_class=new(pool) MDate(pool);
                    347: }

E-mail: