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

1.1       parser      1: /** @file
                      2:        Parser: @b date parser class.
                      3: 
1.62      paf         4:        Copyright (c) 2001-2004 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.65.4.1! paf         8: static const char * const IDENT_DATE_C="$Date: 2004/02/19 15:38:00 $";
1.1       parser      9: 
                     10: #include "classes.h"
1.46      paf        11: #include "pa_vmethod_frame.h"
                     12: 
1.1       parser     13: #include "pa_request.h"
                     14: #include "pa_vdouble.h"
                     15: #include "pa_vdate.h"
1.10      parser     16: #include "pa_vtable.h"
1.1       parser     17: 
                     18: // class
                     19: 
1.46      paf        20: class MDate: public Methoded {
1.1       parser     21: public: // VStateless_class
1.53      paf        22:        Value* create_new_value(Pool&) { return new VDate(0); }
1.1       parser     23: 
                     24: public:
1.46      paf        25:        MDate();
1.1       parser     26: public: // Methoded
                     27:        bool used_directly() { return true; }
                     28: };
                     29: 
1.46      paf        30: // global variable
                     31: 
                     32: DECLARE_CLASS_VAR(date, new MDate, 0);
                     33: 
                     34: // helpers
                     35: 
                     36: class Date_calendar_table_template_columns: public ArrayString {
                     37: public:
                     38:        Date_calendar_table_template_columns(): ArrayString(6+2) {
                     39:                for(int i=0; i<=6; i++) {
                     40:                        char *cname=new(PointerFreeGC) char[1/*strlen("6")*/+1/*terminating 0*/];
                     41:                        *this+=new String(cname, sprintf(cname, "%d", i)); // .i column name
                     42:                }
                     43:                *this+=new String("week");
                     44:                *this+=new String("year");
                     45:        }
                     46: };
                     47: 
                     48: 
                     49: Table date_calendar_table_template(new Date_calendar_table_template_columns);
                     50: 
1.1       parser     51: // methods
                     52: 
1.46      paf        53: static void _now(Request& r, MethodParams& params) {
                     54:        VDate& vdate=GET_SELF(r, VDate);
1.23      paf        55: 
                     56:        time_t t=time(0);
1.46      paf        57:        if(params.count()==1) // ^now(offset)
                     58:                t+=(time_t)round(params.as_double(0, "offset must be double", r)*SECS_PER_DAY);
1.23      paf        59:        
1.46      paf        60:        vdate.set_time(t);
1.1       parser     61: }
                     62: 
1.59      paf        63: /// shrinked range: 1970/1/1 to 2038/1/1
1.61      paf        64: static int to_tm_year(int iyear) {
1.59      paf        65:        if(iyear<1970 || iyear>2038)
                     66:                throw Exception(0,
                     67:                        0,
                     68:                        "year '%d' is out of valid range", iyear);
1.61      paf        69:        return iyear-1900;
1.59      paf        70: }
                     71: 
1.41      paf        72: // 2002-04-25 18:14:00
                     73: // 18:14:00
                     74: // 2002:04:25 [+maybe time]
1.65      paf        75: /*not static, used in image.C*/ tm cstr_to_time_t(char *cstr) {
1.64      paf        76:        if( !cstr || !*cstr )
                     77:                throw Exception(0,
                     78:                        0,
                     79:                        "empty string is not valid datetime");
                     80: 
1.41      paf        81:        char *cur=cstr;
                     82:        int date_delim=isdigit(cur[0])&&isdigit(cur[1])&&isdigit(cur[2])&&isdigit(cur[3])&&cur[4]==':'?':'
                     83:                :'-';
1.46      paf        84:        const char* year=lsplit(&cur, date_delim);
                     85:        const char* month=lsplit(&cur, date_delim);
                     86:        const char* mday=lsplit(&cur, ' ');
1.41      paf        87:        if(!month)
                     88:                cur=cstr;
1.46      paf        89:        const char* hour=lsplit(&cur, ':');
                     90:        const char* min=lsplit(&cur, ':');
                     91:        const char* sec=cur;
1.41      paf        92: 
1.56      paf        93:        tm tmIn;  memset(&tmIn, 0, sizeof(tmIn));
1.41      paf        94:        tmIn.tm_isdst=-1;
                     95:        if(!month)
                     96:                if(min) {
                     97:                        year=mday=0; // HH:MM
                     98:                        time_t t=time(0);
                     99:                        tm *tmNow=localtime(&t);
                    100:                        tmIn.tm_year=tmNow->tm_year;
                    101:                        tmIn.tm_mon=tmNow->tm_mon;
                    102:                        tmIn.tm_mday=tmNow->tm_mday;
                    103:                        goto date_part_set;
                    104:                } else
                    105:                        hour=min=sec=0; // not YYYY- & not HH: = just YYYY                                      
1.61      paf       106:        tmIn.tm_year=to_tm_year(pa_atoi(year));
1.60      paf       107:        tmIn.tm_mon=month?pa_atoi(month)-1:0;
                    108:        tmIn.tm_mday=mday?pa_atoi(mday):1;
1.41      paf       109: date_part_set:
1.65      paf       110:        tmIn.tm_hour=hour?pa_atoi(hour):0;
1.60      paf       111:        tmIn.tm_min=min?pa_atoi(min):0;
1.64      paf       112:        tmIn.tm_sec=sec?pa_atoi(sec):0; 
1.65      paf       113:        return tmIn;
1.41      paf       114: }
                    115: 
1.46      paf       116: static void _create(Request& r, MethodParams& params) {
                    117:        VDate& vdate=GET_SELF(r, VDate);
1.1       parser    118: 
1.46      paf       119:        if(params.count()==1) { 
1.31      paf       120:                // ^create[2002-04-25 18:14:00]
                    121:                // ^create[18:14:00]
1.46      paf       122:                if(const String* sdate=params[0].get_string())
1.65      paf       123:                        vdate.set_time(cstr_to_time_t(sdate->cstrm()));
1.41      paf       124:                else { // ^create(float days)
1.65      paf       125:                        time_t t=(time_t)round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY);
1.28      paf       126:                        if(t<0 || !localtime(&t))
                    127:                                throw Exception(0,
1.46      paf       128:                                        0,
1.28      paf       129:                                        "invalid datetime");
1.65      paf       130:                        vdate.set_time(t);
1.28      paf       131:                }
1.46      paf       132:        } else if(params.count()>=2) { // ^create(y;m;d[;h[;m[;s]]])
1.56      paf       133:                tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
1.1       parser    134:                tmIn.tm_isdst=-1;
1.61      paf       135:                tmIn.tm_year=to_tm_year(params.as_int(0, "year must be int", r));
1.46      paf       136:                tmIn.tm_mon=params.as_int(1, "month must be int", r)-1;
                    137:                tmIn.tm_mday=params.count()>2?params.as_int(2, "mday must be int", r):1;
1.64      paf       138:                int savedHour=0;
                    139:                if(params.count()>3) savedHour=tmIn.tm_hour=params.as_int(3, "hour must be int", r);
1.46      paf       140:                if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r);
                    141:                if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r);
1.65      paf       142:                vdate.set_time(tmIn);
1.1       parser    143:        } else
1.22      paf       144:                throw Exception("parser.runtime",
1.46      paf       145:                        0,
1.24      paf       146:                        "invalid params count, must be 1 or >=2");
1.1       parser    147: }
                    148: 
1.46      paf       149: static void _sql_string(Request& r, MethodParams&) {
                    150:        VDate& vdate=GET_SELF(r, VDate);
1.2       parser    151:        int size=1+ 4+1+2+1+2 +1+ 2+1+2+1+2 +1 +1;
1.46      paf       152:        char *buf=new(PointerFreeGC) char[size];
                    153:        time_t time=vdate.get_time();
1.14      paf       154:        size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", localtime(&time));
1.1       parser    155:        
1.46      paf       156:        r.write_assign_lang(String(buf, size));
1.1       parser    157: }
                    158: 
1.46      paf       159: static void _roll(Request& r, MethodParams& params) {
                    160:        VDate& vdate=GET_SELF(r, VDate);
1.1       parser    161: 
1.46      paf       162:        const String& what=params.as_string(0, "'what' must be string");
1.43      paf       163:        int oyear=0;
                    164:        int omonth=0;
                    165:        int oday=0;
1.1       parser    166:        int *offset;
                    167:        if(what=="year") offset=&oyear;
                    168:        else if(what=="month") offset=&omonth;
                    169:        else if(what=="day") offset=&oday;
1.48      paf       170:        else if(what=="TZ") {
                    171:                const String& argument_tz=params.as_string(1, "'TZ' must be string");
                    172:                vdate.set_tz(&argument_tz);
                    173:                return;
                    174:        } else
1.22      paf       175:                throw Exception("parser.runtime",
1.1       parser    176:                        &what,
1.48      paf       177:                        "must be year|month|day|TZ");
1.1       parser    178:        
1.46      paf       179:        *offset=params.as_int(1, "offset must be int", r);
1.1       parser    180: 
1.46      paf       181:        time_t self_time=vdate.get_time();
1.13      paf       182:        tm tmIn=*localtime(&self_time);
                    183:        tm tmSaved=tmIn;
1.43      paf       184:        int adjust_day=0;
                    185:        time_t t_changed_date;
                    186:        while(true) {
                    187:                tmIn.tm_year+=oyear;
                    188:                tmIn.tm_mon+=omonth;
                    189:                tmIn.tm_mday+=oday+adjust_day;
                    190:                tmIn.tm_hour=24/2; 
                    191:                tmIn.tm_min=0;
                    192:                tmIn.tm_sec=0;
1.44      paf       193:                int saved_mon=(tmIn.tm_mon+12*100)%12; // crossing year boundary backwards
1.43      paf       194:                t_changed_date=mktime/*normalizetime*/(&tmIn);
                    195:                if(t_changed_date<0)
                    196:                        throw Exception(0,
1.46      paf       197:                                0,
1.43      paf       198:                                "bad resulting time (rolled out of valid date range)");
1.46      paf       199:                if(oday==0 && tmIn.tm_mon!=saved_mon/*but it changed*/) {
1.43      paf       200:                        if(adjust_day <= -3/*31->28 max, so never, but...*/)
                    201:                                throw Exception(0,
1.46      paf       202:                                        0,
1.43      paf       203:                                        "bad resulting time (day hole still with %d day adjustment)", adjust_day );
                    204:                        
                    205:                        tmIn=tmSaved; // restoring
                    206:                        --adjust_day; //retrying with prev day
                    207:                } else
                    208:                        break;                  
                    209:        }
1.13      paf       210: 
1.43      paf       211:        tm *tmOut=localtime(&t_changed_date);
1.13      paf       212:        if(!tmOut)
1.22      paf       213:                throw Exception(0,
1.46      paf       214:                        0,
1.43      paf       215:                        "bad resulting time (seconds from epoch=%d)", t_changed_date);
1.13      paf       216:     
1.43      paf       217:        tmOut->tm_hour=tmSaved.tm_hour;
                    218:        tmOut->tm_min=tmSaved.tm_min;
                    219:        tmOut->tm_sec=tmSaved.tm_sec;
1.20      paf       220:        tmOut->tm_isdst=-1; 
1.13      paf       221:        {
1.43      paf       222:                time_t t_changed_time=mktime/*normalizetime*/(tmOut);
1.63      paf       223:                /*autofix: in msk timezone last sunday of march hour hole: [2am->3am)
1.21      paf       224:                if(
                    225:                        tmOut->tm_hour!=tmSaved.tm_hour
                    226:                        ||tmOut->tm_min!=tmSaved.tm_min)
1.22      paf       227:                        throw Exception(0,
1.46      paf       228:                                0,
1.24      paf       229:                                "bad resulting time (hour hole)");
1.43      paf       230:                */
1.21      paf       231: 
1.43      paf       232:                if(t_changed_time<0)
1.22      paf       233:                        throw Exception(0,
1.46      paf       234:                                0,
1.21      paf       235:                                "bad resulting time (after reconstruction)");
1.13      paf       236:                
1.46      paf       237:                vdate.set_time(t_changed_time);
1.13      paf       238:        }
1.1       parser    239: }
                    240: 
1.46      paf       241: static Table& fill_month_days(Request& r, MethodParams& params, bool rus){
1.45      paf       242:        Table::Action_options table_options;
1.46      paf       243:        Table& result=*new Table(date_calendar_table_template, table_options);
                    244:        
                    245:        int year=params.as_int(1, "year must be int", r);
                    246:        int month=max(1, min(params.as_int(2, "month must be int", r), 12)) -1;
1.43      paf       247:        
1.55      paf       248:        tm tmIn;  
1.56      paf       249:        memset(&tmIn, 0, sizeof(tmIn)); 
1.55      paf       250:        tmIn.tm_mday=1;
                    251:        tmIn.tm_mon=month; 
1.58      paf       252:        tmIn.tm_year=year-1900;
1.55      paf       253: 
1.43      paf       254:        time_t t=mktime(&tmIn);
1.10      parser    255:        if(t<0)
1.22      paf       256:                throw Exception(0, 
1.46      paf       257:                        0, 
1.10      parser    258:                        "invalid date");
1.43      paf       259:        tm *tmOut=localtime(&t);
                    260:        
                    261:        int weekDay1=tmOut->tm_wday;
1.10      parser    262:        if(rus) 
                    263:                weekDay1=weekDay1?weekDay1-1:6; //sunday last
1.43      paf       264:        int monthDays=getMonthDays(year, month);
                    265:        
                    266:        for(int _day=1-weekDay1; _day<=monthDays;) {
1.46      paf       267:                Table::element_type row(new ArrayString(7));
1.34      paf       268:                // calculating year week no [1..54]
1.53      paf       269:                char *weekno_buf=0; // surely would be assigned to, but to calm down compiler
                    270:                size_t weekno_size=0; // same
                    271:                int weekyear=0; // same
1.34      paf       272:                // 0..6 week days-cells fill with month days
1.43      paf       273:                for(int wday=0; wday<7; wday++, _day++) {
1.46      paf       274:                        String* cell=new String;
1.10      parser    275:                        if(_day>=1 && _day<=monthDays) {
1.46      paf       276:                                char *buf=new(PointerFreeGC) char[2+1]; 
                    277:                                cell->append_know_length(buf, sprintf(buf, "%02d", _day), String::L_CLEAN);
                    278:                        }
                    279:                        *row+=cell;            
1.34      paf       280: 
                    281:                        if(wday==(rus?3:4)/*thursday*/) {
1.57      paf       282:                                tm tms;
                    283:                                memset(&tms, 0, sizeof(tmIn)); 
                    284:                                tms.tm_mday=_day;
                    285:                                tms.tm_mon=month; 
1.58      paf       286:                                tms.tm_year=year-1900;
1.57      paf       287:                                
1.34      paf       288:                                /*normalize*/mktime(&tms);
                    289: 
1.36      paf       290:                                weekyear=tms.tm_year+1900;
                    291: 
1.35      paf       292:                                const int weekno_buf_size=2+1/*for stupid snprintfs*/ +1;
1.34      paf       293: 
                    294:                                // http://www.merlyn.demon.co.uk/weekinfo.htm
                    295:                                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       296:                                int n=1 + (tms.tm_yday-(FirstThurs[weekyear % 28]-3))/7;
1.46      paf       297:                                weekno_buf=new(PointerFreeGC) char[weekno_buf_size];
1.34      paf       298:                                weekno_size=snprintf(weekno_buf, weekno_buf_size, "%02d", n);
                    299:                        }
                    300:                
1.10      parser    301:         }
1.34      paf       302:                // appending year week no
1.46      paf       303:                *row+=new String(weekno_buf, weekno_size);
1.36      paf       304:                // appending year week year
                    305:                {
1.46      paf       306:                        char* buf=new(PointerFreeGC) char[4+1]; 
                    307:                        *row+=new String(buf, sprintf(buf, "%02d", weekyear));
1.34      paf       308:                }
1.46      paf       309:                result+=row;
                    310:        }
                    311:        
                    312:        return result;
1.10      parser    313: }
                    314: 
1.46      paf       315: static Table& fill_week_days(Request& r, MethodParams& params, bool rus){
                    316:        Table::columns_type columns(new ArrayString(4));
                    317:        *columns+=new String("year");
                    318:        *columns+=new String("month");
                    319:        *columns+=new String("day");
                    320:        *columns+=new String("weekday");
                    321:        Table& result=*new Table(columns);
                    322: 
                    323:        int year=params.as_int(1, "year must be int", r);
                    324:        int month=max(1, min(params.as_int(2, "month must be int", r), 12)) -1;
                    325:        int day=params.as_int(3, "day must be int", r);
                    326:        
1.55      paf       327:        tm tmIn;
1.56      paf       328:        memset(&tmIn, 0, sizeof(tmIn)); 
1.55      paf       329:        tmIn.tm_hour=18;
                    330:        tmIn.tm_mday=day;
                    331:        tmIn.tm_mon=month; 
1.58      paf       332:        tmIn.tm_year=year-1900;
1.55      paf       333:                
1.46      paf       334:        time_t t=mktime(&tmIn);
1.10      parser    335:        if(t<0)
1.22      paf       336:                throw Exception(0, 
1.46      paf       337:                        0, 
1.10      parser    338:                        "invalid date");
1.46      paf       339:        tm *tmOut=localtime(&t);
1.10      parser    340:     
1.46      paf       341:        int baseWeekDay=tmOut->tm_wday;
1.10      parser    342:        if(rus) 
                    343:                baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
1.46      paf       344:        
                    345:        t-=baseWeekDay*SECS_PER_DAY;
                    346:                
                    347:        for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
                    348:                tm *tmOut=localtime(&t);
                    349:                Table::element_type row(new ArrayString(4));
1.10      parser    350: #define WDFILL(size, value) { \
1.46      paf       351:                        char *buf=new(PointerFreeGC) char[size+1]; \
                    352:                        *row+=new String(buf, sprintf(buf, "%0"#size"d", value)); \
1.10      parser    353:                }
                    354:                WDFILL(4, 1900+tmOut->tm_year);
                    355:                WDFILL(2, 1+tmOut->tm_mon);
                    356:                WDFILL(2, tmOut->tm_mday);
                    357:                WDFILL(2, tmOut->tm_wday);
1.46      paf       358:                result+=row;
                    359:        }
                    360:        
                    361:        return result;
1.10      parser    362: }
                    363: 
1.46      paf       364: static void _calendar(Request& r, MethodParams& params) {
                    365:        const String& what=params.as_string(0, "format must be strig");
1.10      parser    366:        bool rus=false;
                    367:        if(what=="rus")
                    368:                rus=true;
                    369:        else if(what=="eng")
                    370:                rus=false;
                    371:        else
1.22      paf       372:                throw Exception("parser.runtime", 
1.10      parser    373:                        &what, 
                    374:                        "must be rus|eng");
                    375: 
1.46      paf       376:        Table* table;
                    377:        if(params.count()==1+2) 
                    378:                table=&fill_month_days(r, params, rus);
1.10      parser    379:        else // 1+3
1.46      paf       380:                table=&fill_week_days(r, params, rus);
1.10      parser    381: 
1.46      paf       382:        r.write_no_lang(*new VTable(table));
1.10      parser    383: }
                    384: 
1.49      paf       385: static void _unix_timestamp(Request& r, MethodParams& params) {
                    386:        VDate& vdate=GET_SELF(r, VDate);
                    387: 
                    388:        if(params.count()==0) { 
                    389:                // ^date.unix-timestamp[]
                    390:                r.write_no_lang(*new VInt((int)vdate.get_time()));
                    391:        } else {
1.50      paf       392:                if(vdate.get_time())
                    393:                        throw Exception(0,
                    394:                                0,
                    395:                                "date object already constructed");
1.51      paf       396: 
1.50      paf       397:                // ^unix-timestamp(time_t)
                    398:                time_t t=(time_t)params.as_int(0, "Unix timestamp must be integer", r);
1.49      paf       399: 
1.50      paf       400:                vdate.set_time(t);
1.49      paf       401:        }
                    402: }
                    403: 
                    404: 
1.1       parser    405: // constructor
                    406: 
1.46      paf       407: MDate::MDate(): Methoded("date") {
1.1       parser    408:        // ^now[]
1.23      paf       409:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1       parser    410: 
1.30      paf       411:        // ^create(float days)
1.17      paf       412:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
1.28      paf       413:        // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.17      paf       414:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1       parser    415: 
1.6       parser    416:        // ^sql-string[]
1.5       parser    417:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1       parser    418: 
                    419:        // ^roll(year|month|day;+/- 1)
                    420:        add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10      parser    421: 
                    422:        // ^date:calendar[month|montheng;year;month]  = table
                    423:        // ^date:calendar[week|weekeng;year;month;day] = table
                    424:        add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1       parser    425: 
1.49      paf       426: 
                    427:        // ^unix-timestamp[]
                    428:        add_native_method("unix-timestamp", Method::CT_DYNAMIC, _unix_timestamp, 0, 1);
1.1       parser    429: }

E-mail: