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

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.32      paf         6: */
1.1       parser      7: 
1.41    ! paf         8: static const char* IDENT_DATE_C="$Date: 2002/10/08 06:57:04 $";
1.1       parser      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();
1.37      paf        32:        VDate *vdate=static_cast<VDate *>(r.get_self());
1.23      paf        33: 
                     34:        time_t t=time(0);
                     35:        if(params->size()==1) // ^now(offset)
1.39      paf        36:                t+=(time_t)round(params->as_double(0, "offset must be double", r)*SECS_PER_DAY);
1.23      paf        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: 
1.41    ! paf        49: // 2002-04-25 18:14:00
        !            50: // 18:14:00
        !            51: // 2002:04:25 [+maybe time]
        !            52: time_t cstr_to_time_t(char *cstr, const String *report_error_origin) { // used in image.C
        !            53:        char *cur=cstr;
        !            54:        int date_delim=isdigit(cur[0])&&isdigit(cur[1])&&isdigit(cur[2])&&isdigit(cur[3])&&cur[4]==':'?':'
        !            55:                :'-';
        !            56:        const char *year=lsplit(&cur, date_delim);
        !            57:        const char *month=lsplit(&cur, date_delim);
        !            58:        const char *mday=lsplit(&cur, ' ');
        !            59:        if(!month)
        !            60:                cur=cstr;
        !            61:        const char *hour=lsplit(&cur, ':');
        !            62:        const char *min=lsplit(&cur, ':');
        !            63:        const char *sec=cur;
        !            64: 
        !            65:        tm tmIn={0};
        !            66:        tmIn.tm_isdst=-1;
        !            67:        if(!month)
        !            68:                if(min) {
        !            69:                        year=mday=0; // HH:MM
        !            70:                        time_t t=time(0);
        !            71:                        tm *tmNow=localtime(&t);
        !            72:                        tmIn.tm_year=tmNow->tm_year;
        !            73:                        tmIn.tm_mon=tmNow->tm_mon;
        !            74:                        tmIn.tm_mday=tmNow->tm_mday;
        !            75:                        goto date_part_set;
        !            76:                } else
        !            77:                        hour=min=sec=0; // not YYYY- & not HH: = just YYYY                                      
        !            78:        tmIn.tm_year=NN_year_to_NNNN(atoi(year));
        !            79:        tmIn.tm_mon=month?atoi(month)-1:0;
        !            80:        tmIn.tm_mday=mday?atoi(mday):1;
        !            81: date_part_set:
        !            82:        tmIn.tm_hour=hour?atoi(hour):0;
        !            83:        tmIn.tm_min=min?atoi(min):0;
        !            84:        tmIn.tm_sec=sec?atoi(sec):0;
        !            85:        time_t result=mktime(&tmIn);
        !            86:        if(result<0)
        !            87:                if(report_error_origin)
        !            88:                        throw Exception(0,
        !            89:                                report_error_origin,
        !            90:                                "invalid datetime");
        !            91: 
        !            92:        return result;
        !            93: }
        !            94: 
1.17      paf        95: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.1       parser     96:        Pool& pool=r.pool();
1.37      paf        97:        VDate *vdate=static_cast<VDate *>(r.get_self());
1.1       parser     98: 
1.23      paf        99:        time_t t;
1.28      paf       100:        if(params->size()==1) { 
1.31      paf       101:                // ^create[2002-04-25 18:14:00]
                    102:                // ^create[18:14:00]
1.41    ! paf       103:                if(const String *sdate=params->get(0).get_string())
        !           104:                        t=cstr_to_time_t(sdate->cstr(), sdate);
        !           105:                else { // ^create(float days)
1.38      paf       106:                        t=(time_t)round(params->as_double(0, "float days must be double", r)*SECS_PER_DAY);
1.28      paf       107:                        if(t<0 || !localtime(&t))
                    108:                                throw Exception(0,
                    109:                                        &method_name,
                    110:                                        "invalid datetime");
                    111:                }
1.40      paf       112:        } else if(params->size()>=2) { // ^create(y;m;d[;h[;m[;s]]])
1.1       parser    113:                tm tmIn={0};
                    114:                tmIn.tm_isdst=-1;
1.28      paf       115:                tmIn.tm_year=NN_year_to_NNNN(params->as_int(0, "year must be int", r));
1.9       parser    116:                tmIn.tm_mon=params->as_int(1, "month must be int", r)-1;
1.24      paf       117:                tmIn.tm_mday=params->size()>2?params->as_int(2, "mday must be int", r):1;
1.9       parser    118:                if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r);
                    119:                if(params->size()>4) tmIn.tm_min=params->as_int(4, "minutes must be int", r);
                    120:                if(params->size()>5) tmIn.tm_sec=params->as_int(5, "seconds must be int", r);
1.23      paf       121:                t=mktime(&tmIn);
                    122:                if(t<0)
1.22      paf       123:                        throw Exception(0,
1.1       parser    124:                                &method_name,
                    125:                                "invalid datetime");
                    126:        } else
1.22      paf       127:                throw Exception("parser.runtime",
1.1       parser    128:                        &method_name,
1.24      paf       129:                        "invalid params count, must be 1 or >=2");
1.23      paf       130:        vdate->set_time(t);
1.1       parser    131: }
                    132: 
1.5       parser    133: static void _sql_string(Request& r, const String& method_name, MethodParams *) {
1.1       parser    134:        Pool& pool=r.pool();
1.37      paf       135:        VDate *vdate=static_cast<VDate *>(r.get_self());
1.2       parser    136:        int size=1+ 4+1+2+1+2 +1+ 2+1+2+1+2 +1 +1;
1.1       parser    137:        char *buf=(char *)pool.malloc(size);
                    138:        time_t time=vdate->get_time();
1.14      paf       139:        size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", localtime(&time));
1.1       parser    140:        
1.4       parser    141:        String& string=*new(pool) String(pool);
                    142:        string.APPEND_CLEAN(buf, size, 
                    143:                method_name.origin().file, 
                    144:                method_name.origin().line);
1.25      paf       145:        r.write_assign_lang(string);
1.1       parser    146: }
                    147: 
                    148: 
                    149: static void _roll(Request& r, const String& method_name, MethodParams *params) {
                    150:        Pool& pool=r.pool();
1.37      paf       151:        VDate *vdate=static_cast<VDate *>(r.get_self());
1.1       parser    152: 
                    153:        const String& what=params->as_string(0, "'what' must be string");
                    154:     int oyear=0;
                    155:     int omonth=0;
                    156:     int oday=0;
                    157:        int *offset;
                    158:        if(what=="year") offset=&oyear;
                    159:        else if(what=="month") offset=&omonth;
                    160:        else if(what=="day") offset=&oday;
                    161:        else
1.22      paf       162:                throw Exception("parser.runtime",
1.1       parser    163:                        &what,
                    164:                        "must be year|month|day");
                    165:        
1.9       parser    166:        *offset=params->as_int(1, "offset must be int", r);
1.1       parser    167: 
1.13      paf       168:        time_t self_time=vdate->get_time();
                    169:        tm tmIn=*localtime(&self_time);
                    170:        tm tmSaved=tmIn;
                    171: 
1.21      paf       172:        tmIn.tm_year+=oyear;
                    173:        tmIn.tm_mon+=omonth;
                    174:        tmIn.tm_mday+=oday;
                    175:        tmIn.tm_hour=24/2; 
                    176:        tmIn.tm_min=0;
                    177:        tmIn.tm_sec=0;
                    178:        time_t t=mktime/*normalizetime*/(&tmIn);
                    179:        if(t<0)
1.22      paf       180:                throw Exception(0,
1.21      paf       181:                &method_name,
                    182:                "bad resulting time (after roll)");
1.24      paf       183:        if(oday==0 && tmIn.tm_mday!=tmSaved.tm_mday)
                    184:                throw Exception(0,
                    185:                        &method_name,
                    186:                        "bad resulting time (day hole)", t);
1.21      paf       187: 
1.13      paf       188:     tm *tmOut=localtime(&t);
                    189:        if(!tmOut)
1.22      paf       190:                throw Exception(0,
1.1       parser    191:                        &method_name,
1.13      paf       192:                        "bad resulting time (seconds from epoch=%ld)", t);
                    193:     
                    194:     tmOut->tm_hour=tmSaved.tm_hour;
                    195:     tmOut->tm_min=tmSaved.tm_min;
                    196:     tmOut->tm_sec=tmSaved.tm_sec;
1.20      paf       197:        tmOut->tm_isdst=-1; 
1.13      paf       198:        {
1.21      paf       199:                time_t t=mktime/*normalizetime*/(tmOut);
                    200:                if(
                    201:                        tmOut->tm_hour!=tmSaved.tm_hour
                    202:                        ||tmOut->tm_min!=tmSaved.tm_min)
1.22      paf       203:                        throw Exception(0,
1.21      paf       204:                                &method_name,
1.24      paf       205:                                "bad resulting time (hour hole)");
1.21      paf       206: 
1.13      paf       207:                if(t<0)
1.22      paf       208:                        throw Exception(0,
1.21      paf       209:                                &method_name,
                    210:                                "bad resulting time (after reconstruction)");
1.13      paf       211:                
                    212:                vdate->set_time(t);
                    213:        }
1.1       parser    214: }
                    215: 
1.10      parser    216: static Table *fill_month_days(Request& r, 
                    217:                                                          const String& method_name, MethodParams *params, bool rus){
                    218:        Pool& pool=r.pool();
1.34      paf       219:        Table *result=new(pool) Table(pool, *date_calendar_table_template);
1.10      parser    220: 
                    221:     int year=params->as_int(1, "year must be int", r);
                    222:     int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
                    223: 
                    224:     tm tmIn={0, 0, 0, 1, month, year-1900};
                    225:     time_t t=mktime(&tmIn);
                    226:        if(t<0)
1.22      paf       227:                throw Exception(0, 
1.10      parser    228:                        &method_name, 
                    229:                        "invalid date");
                    230:     tm *tmOut=localtime(&t);
                    231: 
                    232:     int weekDay1=tmOut->tm_wday;
                    233:        if(rus) 
                    234:                weekDay1=weekDay1?weekDay1-1:6; //sunday last
                    235:     int monthDays=getMonthDays(year, month);
                    236:     
                    237:     for(int _day=1-weekDay1; _day<=monthDays;) {
                    238:                Array& row=*new(pool) Array(pool, 7);
1.34      paf       239:                // calculating year week no [1..54]
                    240:                char *weekno_buf;
                    241:                size_t weekno_size;
1.36      paf       242:                int weekyear;
1.34      paf       243:                // 0..6 week days-cells fill with month days
1.10      parser    244:        for(int wday=0; wday<7; wday++, _day++) {
1.34      paf       245:                        String *cell=new(pool) String(pool);
1.10      parser    246:                        if(_day>=1 && _day<=monthDays) {
                    247:                                char *buf=(char *)pool.malloc(2+1); 
                    248:                                cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day), 
                    249:                                        method_name.origin().file, method_name.origin().line);
                    250:             }
                    251:                        row+=cell;            
1.34      paf       252: 
                    253:                        if(wday==(rus?3:4)/*thursday*/) {
                    254:                                tm tms={0,0,0,  _day, month, year-1900};
                    255:                                /*normalize*/mktime(&tms);
                    256: 
1.36      paf       257:                                weekyear=tms.tm_year+1900;
                    258: 
1.35      paf       259:                                const int weekno_buf_size=2+1/*for stupid snprintfs*/ +1;
1.34      paf       260: 
                    261:                                // http://www.merlyn.demon.co.uk/weekinfo.htm
                    262:                                const int FirstThurs[] = {7,5,4,3,2,7,6,5,4,2,1,7,6,4,3,2,1,6,5,4,3,1,7,6,5,3,2,1};
1.36      paf       263:                                int n=1 + (tms.tm_yday-(FirstThurs[weekyear % 28]-3))/7;
1.34      paf       264:                                weekno_buf=(char *)pool.malloc(weekno_buf_size);
                    265:                                weekno_size=snprintf(weekno_buf, weekno_buf_size, "%02d", n);
                    266:                        }
                    267:                
1.10      parser    268:         }
1.34      paf       269:                // appending year week no
                    270:                {
                    271:                String *cell=new(pool) String(pool);
                    272:                        cell->APPEND_CLEAN(weekno_buf, weekno_size, 
1.36      paf       273:                                method_name.origin().file, method_name.origin().line);
                    274:                        row+=cell;
                    275:                }
                    276:                // appending year week year
                    277:                {
                    278:                String *cell=new(pool) String(pool);
                    279:                        char *buf=(char *)pool.malloc(4+1); 
                    280:                        cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", weekyear), 
1.34      paf       281:                                method_name.origin().file, method_name.origin().line);
                    282:                        row+=cell;
                    283:                }
1.10      parser    284:        *result+=&row;
                    285:     }
                    286:     
                    287:     return result;
                    288: }
                    289: 
                    290: static Table *fill_week_days(Request& r, 
                    291:                                                         const String& method_name, MethodParams *params, bool rus){
                    292:        Pool& pool=r.pool();
                    293:        Array& columns=*new(pool) Array(pool, 4);
1.18      paf       294:        columns+=new(pool) String(pool, "year");
                    295:        columns+=new(pool) String(pool, "month");
                    296:        columns+=new(pool) String(pool, "day");
1.19      paf       297:        columns+=new(pool) String(pool, "weekday");
1.10      parser    298:        Table *result=new(pool) Table(pool, &method_name, &columns);
                    299: 
                    300:     int year=params->as_int(1, "year must be int", r);
                    301:     int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
                    302:     int day=params->as_int(3, "day must be int", r);
                    303:     
                    304:     tm tmIn={0, 0, 18, day, month, year-1900};
                    305:     time_t t=mktime(&tmIn);
                    306:        if(t<0)
1.22      paf       307:                throw Exception(0, 
1.10      parser    308:                        &method_name, 
                    309:                        "invalid date");
                    310:     tm *tmOut=localtime(&t);
                    311:     
                    312:     int baseWeekDay=tmOut->tm_wday;
                    313:        if(rus) 
                    314:                baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
                    315: 
                    316:     t-=baseWeekDay*SECS_PER_DAY;
                    317: 
                    318:     for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
                    319:         tm *tmOut=localtime(&t);
                    320:                Array& row=*new(pool) Array(pool, 4);
                    321: #define WDFILL(size, value) { \
                    322:                char *buf=(char *)pool.malloc(size+1); \
                    323:                String *cell=new(pool) String(pool); \
                    324:                cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
                    325:                        method_name.origin().file, \
                    326:                        method_name.origin().line); \
                    327:                row+=cell; \
                    328:                }
                    329:                WDFILL(4, 1900+tmOut->tm_year);
                    330:                WDFILL(2, 1+tmOut->tm_mon);
                    331:                WDFILL(2, tmOut->tm_mday);
                    332:                WDFILL(2, tmOut->tm_wday);
                    333:         *result+=&row;
                    334:     }
                    335:     
                    336:     return result;
                    337: }
                    338: 
                    339: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
                    340:        Pool& pool=r.pool();
                    341: 
                    342:        const String& what=params->as_string(0, "format must be strig");
                    343:        bool rus=false;
                    344:        if(what=="rus")
                    345:                rus=true;
                    346:        else if(what=="eng")
                    347:                rus=false;
                    348:        else
1.22      paf       349:                throw Exception("parser.runtime", 
1.10      parser    350:                        &what, 
                    351:                        "must be rus|eng");
                    352: 
                    353:        Table *table;
                    354:        if(params->size()==1+2) 
                    355:                table=fill_month_days(r, method_name, params, rus);
                    356:        else // 1+3
                    357:                table=fill_week_days(r, method_name, params, rus);
                    358: 
                    359:        VTable& result=*new(pool) VTable(pool, table);
                    360:        r.write_no_lang(result);
                    361: }
                    362: 
1.1       parser    363: // constructor
                    364: 
1.27      paf       365: MDate::MDate(Pool& apool) : Methoded(apool, "date") {
1.1       parser    366:        // ^now[]
1.23      paf       367:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1       parser    368: 
1.30      paf       369:        // ^create(float days)
1.17      paf       370:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
1.28      paf       371:        // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.17      paf       372:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1       parser    373: 
1.6       parser    374:        // ^sql-string[]
1.5       parser    375:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1       parser    376: 
                    377:        // ^roll(year|month|day;+/- 1)
                    378:        add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10      parser    379: 
                    380:        // ^date:calendar[month|montheng;year;month]  = table
                    381:        // ^date:calendar[week|weekeng;year;month;day] = table
                    382:        add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1       parser    383: 
                    384: }
                    385: // global variable
                    386: 
                    387: Methoded *date_class;
                    388: 
                    389: // creator
                    390: 
                    391: Methoded *MDate_create(Pool& pool) {
                    392:        return date_class=new(pool) MDate(pool);
                    393: }

E-mail: