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

1.1       parser      1: /** @file
                      2:        Parser: @b date parser class.
                      3: 
1.99      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (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: 
                      8: #include "classes.h"
1.46      paf         9: #include "pa_vmethod_frame.h"
                     10: 
1.1       parser     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: 
1.101   ! moko       16: volatile const char * IDENT_DATE_C="$Id: date.C,v 1.100 2016/03/27 20:53:43 moko Exp $" IDENT_PA_VDATE_H;
1.91      moko       17: 
1.1       parser     18: // class
                     19: 
1.46      paf        20: class MDate: public Methoded {
1.1       parser     21: public: // VStateless_class
1.87      misha      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: };
                     27: 
1.46      paf        28: // global variable
                     29: 
                     30: DECLARE_CLASS_VAR(date, new MDate, 0);
                     31: 
                     32: // helpers
                     33: 
                     34: class Date_calendar_table_template_columns: public ArrayString {
                     35: public:
                     36:        Date_calendar_table_template_columns(): ArrayString(6+2) {
1.85      misha      37:                for(int i=0; i<=6; i++)
                     38:                        *this+=new String(i, "%d"); // .i column name
                     39: 
1.46      paf        40:                *this+=new String("week");
                     41:                *this+=new String("year");
                     42:        }
                     43: };
                     44: 
                     45: 
                     46: Table date_calendar_table_template(new Date_calendar_table_template_columns);
                     47: 
1.1       parser     48: // methods
                     49: 
1.46      paf        50: static void _now(Request& r, MethodParams& params) {
                     51:        VDate& vdate=GET_SELF(r, VDate);
1.23      paf        52: 
1.98      moko       53:        pa_time_t t=(pa_time_t)time(0);
1.46      paf        54:        if(params.count()==1) // ^now(offset)
1.98      moko       55:                t+=(pa_time_t)round(params.as_double(0, "offset must be double", r)*SECS_PER_DAY);
1.23      paf        56:        
1.46      paf        57:        vdate.set_time(t);
1.1       parser     58: }
                     59: 
1.90      misha      60: static void _today(Request& r, MethodParams&) {
                     61:        VDate& vdate=GET_SELF(r, VDate);
                     62: 
                     63:        time_t t=time(0);
                     64: 
                     65:        tm today=*localtime(&t);
                     66:        today.tm_hour=0;
                     67:        today.tm_min=0;
                     68:        today.tm_sec=0;
                     69: 
1.94      moko       70:        vdate.set_tm(today);
1.90      misha      71: }
                     72: 
1.78      misha      73: static int to_year(int iyear) {
1.94      moko       74:        if(iyear<0 || iyear>9999)
                     75:                throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "year '%d' is out of range 0..9999", iyear);
                     76:        return iyear-1900;
1.78      misha      77: }
                     78: 
                     79: static int to_month(int imonth) {
                     80:        return max(1, min(imonth, 12)) -1;
1.59      paf        81: }
                     82: 
1.96      moko       83: 
                     84: static const char *skip_number_throw(char *string, char c, const char *valid){
                     85:        if(!valid[0])
                     86:                throw Exception("date.format", 0, "invalid character '%c' after number in '%s'", c, string);
                     87:        if(!strcmp(valid, "+-Z"))
                     88:                throw Exception("date.format", 0, "invalid timezone character '%c' after number in '%s'", c, string);
                     89:        throw Exception("date.format", 0, "number delimiter '%c'%s expected, but found '%c' in date '%s'",
                     90:                valid[0], valid[strlen(valid)-1] == 'Z' ? " or timezone":"", c, string);
                     91: }
                     92: 
1.95      moko       93: static char *skip_number(char* string, const char *valid_delim, char *delim) {
                     94:        if(string) {
                     95:                char *str=string;
                     96:                // skipping whitespace
                     97:                while(isspace(str[0])) str++;
                     98:                // skipping +-
                     99:                if(str[0]=='-' || str[0]=='+') str++;
                    100:                // at least one digit should be present
1.96      moko      101:                if(!str[0])
                    102:                        throw Exception("date.format", 0, "number expected in date '%s'", string);
                    103:                if(!isdigit(str[0]))
                    104:                        throw Exception("date.format", 0, "'%c' must be number in date '%s'", str[0], string);
                    105:                str++;
1.95      moko      106:                // skipping digits
                    107:                while(isdigit(str[0])) str++;
                    108:                // skipping trailing whitespace
                    109:                if(!strchr(valid_delim, ' '))
                    110:                        while(isspace(str[0])) str++;
                    111:                // delimiter check
                    112:                if(char c=str[0]){
                    113:                        if(!strchr(valid_delim, c))
1.96      moko      114:                                skip_number_throw(string, c, valid_delim);
1.95      moko      115:                        if(delim)
                    116:                                *delim=c;
                    117:                        str[0]=0;
                    118:                        return str+1;
                    119:                }
                    120:        }
                    121:        if(delim)
                    122:                *delim=0;
                    123:        return 0;
                    124: }
                    125: 
                    126: static char *skip_number(char** string_ref, const char *valid_delim, char *delim=0) {
                    127:        char *result=*string_ref;
                    128:        *string_ref=skip_number(*string_ref, valid_delim, delim);
                    129:        return result;
                    130: }
                    131: 
                    132: static char *skip_writespace(char* str) {
                    133:        if(str){
                    134:                while(isspace(str[0])) str++;
                    135:                return str[0] ? str : 0;
                    136:        }
                    137:        return 0;
                    138: }
                    139: 
                    140: static char *numeric_tz(char prefix, char* tz) {
1.101   ! moko      141:        // preparing POSIX TZ format
        !           142:        char *buf=new(PointerFreeGC) char[4+5+1/*zero-teminator*/];
        !           143:        strcpy(buf, prefix=='+' ? "SUB-":"SUB+");
        !           144:        char *cur=buf+4;
        !           145: 
1.95      moko      146:        // hours
1.101   ! moko      147:        if(!isdigit(*(cur++)=*(tz++)))
1.95      moko      148:                return 0;
1.101   ! moko      149:        if(isdigit(tz[0]))
        !           150:                *(cur++)=*(tz++);
        !           151: 
        !           152:        if(tz[0] == ':'){
        !           153:                // HH:mm format
        !           154:                *(cur++)=*(tz++);
        !           155:                if(!isdigit(*(cur++)=*(tz++)))
        !           156:                        return 0;
        !           157:                if(isdigit(tz[0]))
        !           158:                        *(cur++)=*(tz++);
        !           159:        } else if(isdigit(tz[0])){
        !           160:                // HHmm format
        !           161:                *(cur++)=':';
        !           162:                if(!isdigit(*(cur++)=*(tz++)) || !isdigit(*(cur++)=*(tz++)))
1.95      moko      163:                        return 0;
                    164:        }
                    165:        // nothing more
1.101   ! moko      166:        if(skip_writespace(tz))
1.95      moko      167:                return 0;
1.101   ! moko      168:        *cur=0;
1.95      moko      169:        return buf;
                    170: }
                    171: 
                    172: // SQL 2002-04-25 18:14:00
                    173: // ISO 2002-04-25T18:14:00.45+01:00
                    174: // TIME 18:14:00
                    175: // ':' DELIMITED 2002:04:25 [+maybe time]
                    176: // not static, used in image.C
                    177: tm cstr_to_time_t(char *cstr, const char **tzOut) {
1.64      paf       178:        if( !cstr || !*cstr )
1.94      moko      179:                throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "empty string is not valid datetime");
1.95      moko      180:        if(tzOut)
                    181:                *tzOut=0;
1.41      paf       182: 
1.94      moko      183:        tm tmIn;
                    184:        memset(&tmIn, 0, sizeof(tmIn));
1.41      paf       185:        tmIn.tm_isdst=-1;
1.95      moko      186: 
                    187:        char delim;
                    188:        char *cur=cstr;
                    189: 
                    190:        const char *year, *month, *mday;
                    191:        const char *hour, *min, *sec, *msec;
                    192: 
1.96      moko      193:        year=skip_number(&cur, "-:", &delim);
1.95      moko      194:        if(delim != ':' || delim == ':' && strlen(year) >=4 ){
                    195:                // year present
                    196:                month=skip_number(&cur, delim == ':' ? ":" : "-");
                    197:                mday=skip_number(&cur, tzOut ? " \tT":" \t", &delim);
                    198:                if(delim != 'T'){
                    199:                        // SQL date format
                    200:                        cur=skip_writespace(cur);
                    201:                        hour=skip_number(&cur, ":");
                    202:                        min=skip_number(&cur, ":");
                    203:                        sec=skip_number(&cur, ".");
                    204:                        msec=skip_number(&cur, "");
                    205:                } else {
                    206:                        // ISO date format
                    207:                        hour=skip_number(&cur, ":");
                    208:                        min=skip_number(&cur, ":+-Z", &delim);
                    209:                        sec=delim==':' ? skip_number(&cur, ".+-Z", &delim) : 0;
                    210:                        msec=delim=='.' ? skip_number(&cur, "+-Z", &delim) : 0;
                    211:                        // timezone specification check
                    212:                        const char *tz = delim == 'Z' ? (skip_writespace(cur) ? 0 : "UTC") : (cur ? numeric_tz(delim, cur) : 0);
                    213:                        if(!tz){
                    214:                                if(!delim)
1.96      moko      215:                                        throw Exception("date.format", 0, "empty timezone");
                    216:                                throw Exception("date.format", 0, "invalid timezone '%c%s'", delim, cur ? cur : "");
1.95      moko      217:                        }
                    218:                        *tzOut=tz;
                    219:                }
                    220: 
                    221:                tmIn.tm_year=to_year(pa_atoi(year));
                    222:                tmIn.tm_mon=month?pa_atoi(month)-1:0;
                    223:                tmIn.tm_mday=mday?pa_atoi(mday):1;
                    224:        } else {
                    225:                // time only
                    226:                hour=year;
                    227:                min=skip_number(&cur, ":");
                    228:                sec=skip_number(&cur, ".");
                    229:                msec=skip_number(&cur, "");
                    230: 
                    231:                time_t t=time(0);
                    232:                tm *tmNow=localtime(&t);
                    233:                tmIn.tm_year=tmNow->tm_year;
                    234:                tmIn.tm_mon=tmNow->tm_mon;
                    235:                tmIn.tm_mday=tmNow->tm_mday;
1.93      moko      236:        }
1.95      moko      237: 
1.65      paf       238:        tmIn.tm_hour=hour?pa_atoi(hour):0;
1.60      paf       239:        tmIn.tm_min=min?pa_atoi(min):0;
1.64      paf       240:        tmIn.tm_sec=sec?pa_atoi(sec):0; 
1.66      paf       241:        //tmIn.tm_[msec<<no such, waits reimplementation of the class]=f(msec);
1.95      moko      242: 
1.65      paf       243:        return tmIn;
1.41      paf       244: }
                    245: 
1.46      paf       246: static void _create(Request& r, MethodParams& params) {
                    247:        VDate& vdate=GET_SELF(r, VDate);
1.1       parser    248: 
1.82      misha     249:        if(params.count()==1){
1.92      moko      250:                if(params[0].is_string()){ // ^create[2002-04-25 18:14:00] ^create[18:14:00]
1.95      moko      251:                        const char *tz;
                    252:                        tm tmIn=cstr_to_time_t(params[0].get_string()->cstrm(), &tz);
                    253:                        if(tz)
                    254:                                vdate.set_tz(tz);
1.94      moko      255:                        vdate.set_tm(tmIn);
1.82      misha     256:                } else { // ^create(float days) or ^create[date object]
1.94      moko      257:                        vdate.set_time(round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY));
1.28      paf       258:                }
1.74      misha     259:        } else { // ^create(y;m;d[;h[;m[;s]]])
1.75      misha     260:                assert(params.count()<=6);
1.56      paf       261:                tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
1.1       parser    262:                tmIn.tm_isdst=-1;
1.94      moko      263:                tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));
1.46      paf       264:                tmIn.tm_mon=params.as_int(1, "month must be int", r)-1;
                    265:                tmIn.tm_mday=params.count()>2?params.as_int(2, "mday must be int", r):1;
1.95      moko      266:                if(params.count()>3) tmIn.tm_hour=params.as_int(3, "hour must be int", r);
1.46      paf       267:                if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r);
                    268:                if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r);
1.94      moko      269:                vdate.set_tm(tmIn);
1.74      misha     270:        };
1.1       parser    271: }
                    272: 
1.90      misha     273: static void _sql_string(Request& r, MethodParams& params) {
1.46      paf       274:        VDate& vdate=GET_SELF(r, VDate);
1.85      misha     275: 
1.90      misha     276:        VDate::sql_string_type format = VDate::sql_string_datetime;
                    277:        if(params.count() > 0) {
                    278:                const String& what=params.as_string(0, "'type' must be string");
                    279:                if(what.is_empty() || what == "datetime")
                    280:                        format = VDate::sql_string_datetime;
                    281:                else if(what == "date")
                    282:                        format=VDate::sql_string_date;
                    283:                else if(what == "time")
                    284:                        format=VDate::sql_string_time;
                    285:                else
                    286:                        throw Exception(PARSER_RUNTIME, &what, "'type' must be 'date', 'time' or 'datetime'");
                    287:        }
                    288: 
                    289:        r.write_assign_lang(*vdate.get_sql_string(format));
1.1       parser    290: }
                    291: 
1.80      misha     292: static void _gmt_string(Request& r, MethodParams&) {
                    293:        VDate& vdate=GET_SELF(r, VDate);
                    294: 
1.88      misha     295:        r.write_assign_lang(*vdate.get_gmt_string());
1.80      misha     296: }
                    297: 
1.100     moko      298: static void _iso_string(Request& r, MethodParams& params) {
1.95      moko      299:        VDate& vdate=GET_SELF(r, VDate);
                    300: 
1.100     moko      301:        VDate::iso_string_type format=VDate::iso_string_default;
                    302: 
                    303:        if(params.count()>0)
                    304:                if(HashStringValue* options=params.as_hash(0)){
                    305:                        int valid_options=0;
                    306:                        if(Value* vshow_ms=options->get("ms")){
                    307:                                if(r.process_to_value(*vshow_ms).as_bool())
                    308:                                        format=VDate::iso_string_type(format|VDate::iso_string_ms);
                    309:                                valid_options++;
                    310:                        }
                    311:                        if(Value* vshow_colon=options->get("colon")){
                    312:                                if(!r.process_to_value(*vshow_colon).as_bool())
                    313:                                        format=VDate::iso_string_type(format|VDate::iso_string_no_colon);
                    314:                                valid_options++;
                    315:                        }
                    316:                        if(Value* vshow_z=options->get("z")){
                    317:                                if(!r.process_to_value(*vshow_z).as_bool())
                    318:                                        format=VDate::iso_string_type(format|VDate::iso_string_no_z);
                    319:                                valid_options++;
                    320:                        }
                    321:                        if(valid_options != options->count())
                    322:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    323:                }
                    324: 
                    325:        r.write_assign_lang(*vdate.get_iso_string(format));
1.95      moko      326: }
                    327: 
1.46      paf       328: static void _roll(Request& r, MethodParams& params) {
                    329:        const String& what=params.as_string(0, "'what' must be string");
1.43      paf       330:        int oyear=0;
                    331:        int omonth=0;
                    332:        int oday=0;
1.1       parser    333:        int *offset;
                    334:        if(what=="year") offset=&oyear;
                    335:        else if(what=="month") offset=&omonth;
                    336:        else if(what=="day") offset=&oday;
1.48      paf       337:        else if(what=="TZ") {
                    338:                const String& argument_tz=params.as_string(1, "'TZ' must be string");
1.95      moko      339:                if(&r.get_self() == date_class){
                    340:                        VDate::set_default_tz(argument_tz.cstr());
                    341:                } else {
                    342:                        VDate& vdate=GET_SELF(r, VDate);
                    343:                        vdate.set_tz(argument_tz.cstr());
                    344:                        vdate.set_time(vdate.get_time());
                    345:                }
1.48      paf       346:                return;
                    347:        } else
1.94      moko      348:                throw Exception(PARSER_RUNTIME, &what, "must be year|month|day|TZ");
                    349: 
                    350:        if(&r.get_self() == date_class)
                    351:                throw Exception(PARSER_RUNTIME, &what, "must be TZ to be called statically");
                    352: 
                    353:        VDate& vdate=GET_SELF(r, VDate);
1.1       parser    354:        
1.46      paf       355:        *offset=params.as_int(1, "offset must be int", r);
1.1       parser    356: 
1.94      moko      357:        tm tmIn=vdate.get_tm();
1.13      paf       358:        tm tmSaved=tmIn;
1.43      paf       359:        int adjust_day=0;
                    360:        while(true) {
                    361:                tmIn.tm_year+=oyear;
                    362:                tmIn.tm_mon+=omonth;
                    363:                tmIn.tm_mday+=oday+adjust_day;
1.94      moko      364:                tmIn.tm_hour=24/2;
1.43      paf       365:                tmIn.tm_min=0;
                    366:                tmIn.tm_sec=0;
1.94      moko      367:                int saved_day=tmIn.tm_mday;
                    368:                vdate.set_tm(tmIn); /* normalize */
                    369: 
                    370:                if(oday==0 && tmIn.tm_mday!=saved_day /* but it changed */ ) {
                    371:                        if(adjust_day <= -3 /* 31->28 max, so never, but... */ )
                    372:                                throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "bad resulting time (day hole still with %d day adjustment)", adjust_day );
1.43      paf       373:                        
                    374:                        tmIn=tmSaved; // restoring
                    375:                        --adjust_day; //retrying with prev day
                    376:                } else
1.94      moko      377:                        break;
1.43      paf       378:        }
1.13      paf       379: 
1.94      moko      380:        tmIn.tm_hour=tmSaved.tm_hour;
                    381:        tmIn.tm_min=tmSaved.tm_min;
                    382:        tmIn.tm_sec=tmSaved.tm_sec;
                    383:        tmIn.tm_isdst=-1;
                    384: 
                    385:        vdate.set_tm(tmIn);
1.1       parser    386: }
                    387: 
1.46      paf       388: static Table& fill_month_days(Request& r, MethodParams& params, bool rus){
1.45      paf       389:        Table::Action_options table_options;
1.46      paf       390:        Table& result=*new Table(date_calendar_table_template, table_options);
                    391:        
1.94      moko      392:        tm tmIn;
                    393:        memset(&tmIn, 0, sizeof(tmIn));
                    394:        tmIn.tm_year=to_year(params.as_int(1, "year must be int", r));
                    395:        tmIn.tm_mon=to_month(params.as_int(2, "month must be int", r));
1.55      paf       396:        tmIn.tm_mday=1;
                    397: 
1.94      moko      398:        VDate t(tmIn); /* normalize */
                    399:        int weekDay1=tmIn.tm_wday;
                    400: 
                    401:        if(rus)
1.10      parser    402:                weekDay1=weekDay1?weekDay1-1:6; //sunday last
1.94      moko      403:        int monthDays=VDate::getMonthDays(tmIn.tm_year, tmIn.tm_mon);
1.43      paf       404:        
                    405:        for(int _day=1-weekDay1; _day<=monthDays;) {
1.46      paf       406:                Table::element_type row(new ArrayString(7));
1.34      paf       407:                // calculating year week no [1..54]
1.85      misha     408:                int weekyear=0; // surely would be assigned to, but to calm down compiler
                    409:                int weekno=0; // same
1.34      paf       410:                // 0..6 week days-cells fill with month days
1.43      paf       411:                for(int wday=0; wday<7; wday++, _day++) {
1.85      misha     412:                        *row+=(_day>=1 && _day<=monthDays)?new String(_day, "%02d"):new String();
1.34      paf       413: 
                    414:                        if(wday==(rus?3:4)/*thursday*/) {
1.57      paf       415:                                tm tms;
1.94      moko      416:                                memset(&tms, 0, sizeof(tms));
1.57      paf       417:                                tms.tm_mday=_day;
1.94      moko      418:                                tms.tm_mon=tmIn.tm_mon;
                    419:                                tms.tm_year=tmIn.tm_year;
1.57      paf       420:                                
1.94      moko      421:                                VDate ts(tms); /*normalize*/
1.36      paf       422:                                weekyear=tms.tm_year+1900;
1.85      misha     423:                                weekno=VDate::CalcWeek(tms).week;
                    424:                        }
                    425:                }
                    426:                // appending week no
                    427:                *row+=new String(weekno, "%02d");
1.36      paf       428: 
1.85      misha     429:                // appending week year
                    430:                *row+=new String(weekyear, "%04d");
1.34      paf       431:                
1.46      paf       432:                result+=row;
                    433:        }
                    434:        
                    435:        return result;
1.10      parser    436: }
                    437: 
1.46      paf       438: static Table& fill_week_days(Request& r, MethodParams& params, bool rus){
                    439:        Table::columns_type columns(new ArrayString(4));
                    440:        *columns+=new String("year");
                    441:        *columns+=new String("month");
                    442:        *columns+=new String("day");
                    443:        *columns+=new String("weekday");
                    444:        Table& result=*new Table(columns);
                    445: 
1.55      paf       446:        tm tmIn;
1.94      moko      447:        memset(&tmIn, 0, sizeof(tmIn));
                    448:        tmIn.tm_year=to_year(params.as_int(1, "year must be int", r));
                    449:        tmIn.tm_mon=to_month(params.as_int(2, "month must be int", r));
                    450:        tmIn.tm_mday=params.as_int(3, "day must be int", r);
1.55      paf       451:        tmIn.tm_hour=18;
1.84      misha     452: 
1.94      moko      453:        VDate t(tmIn); /* normalize */
                    454:        int baseWeekDay=tmIn.tm_wday;
                    455: 
1.10      parser    456:        if(rus) 
                    457:                baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
1.46      paf       458:        
1.94      moko      459:        t.set_time(t.get_time()-baseWeekDay*SECS_PER_DAY);
1.46      paf       460:                
1.94      moko      461:        for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t.set_time(t.get_time()+SECS_PER_DAY)) {
1.46      paf       462:                Table::element_type row(new ArrayString(4));
1.94      moko      463: 
                    464:                tm tmOut=t.get_tm();
                    465:                *row+=new String(1900+tmOut.tm_year, "%04d");
                    466:                *row+=new String(1+tmOut.tm_mon, "%02d");
                    467:                *row+=new String(tmOut.tm_mday, "%02d");
                    468:                *row+=new String(tmOut.tm_wday, "%02d");
1.85      misha     469: 
1.46      paf       470:                result+=row;
                    471:        }
                    472:        
                    473:        return result;
1.10      parser    474: }
                    475: 
1.46      paf       476: static void _calendar(Request& r, MethodParams& params) {
                    477:        const String& what=params.as_string(0, "format must be strig");
1.10      parser    478:        bool rus=false;
                    479:        if(what=="rus")
                    480:                rus=true;
                    481:        else if(what=="eng")
                    482:                rus=false;
                    483:        else
1.94      moko      484:                throw Exception(PARSER_RUNTIME, &what, "must be rus|eng");
1.10      parser    485: 
1.46      paf       486:        Table* table;
                    487:        if(params.count()==1+2) 
                    488:                table=&fill_month_days(r, params, rus);
1.10      parser    489:        else // 1+3
1.46      paf       490:                table=&fill_week_days(r, params, rus);
1.10      parser    491: 
1.46      paf       492:        r.write_no_lang(*new VTable(table));
1.10      parser    493: }
                    494: 
1.49      paf       495: static void _unix_timestamp(Request& r, MethodParams& params) {
                    496:        VDate& vdate=GET_SELF(r, VDate);
                    497: 
                    498:        if(params.count()==0) { 
                    499:                // ^date.unix-timestamp[]
1.94      moko      500:                r.write_no_lang(*new VDouble((double)vdate.get_time()));
1.49      paf       501:        } else {
1.50      paf       502:                if(vdate.get_time())
1.94      moko      503:                        throw Exception(PARSER_RUNTIME, 0, "date object already constructed");
1.50      paf       504:                // ^unix-timestamp(time_t)
1.94      moko      505:                vdate.set_time(params.as_double(0, "Unix timestamp must be number", r));
1.49      paf       506:        }
                    507: }
                    508: 
1.79      misha     509: static void _last_day(Request& r, MethodParams& params) {
1.94      moko      510:        tm tmIn;
1.78      misha     511:        if(&r.get_self() == date_class) {
1.81      misha     512:                if(params.count() != 2)
1.94      moko      513:                        throw Exception(PARSER_RUNTIME, 0, "year and month must be defined");
1.81      misha     514:                // ^date:lastday(year;month)
1.94      moko      515:                tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));
                    516:                tmIn.tm_mon=to_month(params.as_int(1, "month must be int", r));
1.78      misha     517:        } else {
1.94      moko      518:                if(params.count() != 0)
                    519:                        throw Exception(PARSER_RUNTIME, 0, "year and month must not be defined");
1.78      misha     520:                // ^date.lastday[]
1.94      moko      521:                tmIn=GET_SELF(r, VDate).get_tm();
1.78      misha     522:        }
1.94      moko      523:        r.write_no_lang(*new VInt(VDate::getMonthDays(tmIn.tm_year, tmIn.tm_mon)));
1.78      misha     524: }
                    525: 
1.1       parser    526: // constructor
                    527: 
1.46      paf       528: MDate::MDate(): Methoded("date") {
1.78      misha     529:        // ^date::now[]
1.90      misha     530:        // ^date::now(offset float days)
1.23      paf       531:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1       parser    532: 
1.90      misha     533:        // ^date::today[]
                    534:        add_native_method("today", Method::CT_DYNAMIC, _today, 0, 0);
                    535: 
1.78      misha     536:        // ^date::create(float days)
1.90      misha     537:        // ^date::create[date]
                    538:        // ^date::create(year;month;day[;hour[;minute[;sec]]])
                    539:        // ^date::create[yyyy-mm-dd[ hh:mm:ss]]
                    540:        // ^date::create[hh:mm:ss]
1.17      paf       541:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
1.28      paf       542:        // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.17      paf       543:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1       parser    544: 
1.78      misha     545:        // ^date.sql-string[]
1.90      misha     546:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 1);
1.1       parser    547: 
1.80      misha     548:        // ^date.gmt-string[]
                    549:        add_native_method("gmt-string", Method::CT_DYNAMIC, _gmt_string, 0, 0);
                    550: 
1.100     moko      551:        // ^date.iso-string[$.colon(true) $.z(true) $.ms(false)]
                    552:        add_native_method("iso-string", Method::CT_DYNAMIC, _iso_string, 0, 1);
1.95      moko      553: 
1.78      misha     554:        // ^date:lastday(year;month)
                    555:        // ^date.lastday[]
1.79      misha     556:        add_native_method("last-day", Method::CT_ANY, _last_day, 0, 2);
1.78      misha     557: 
1.90      misha     558:        // ^date.roll[year|month|day](+/- 1)
1.94      moko      559:        add_native_method("roll", Method::CT_ANY, _roll, 2, 2);
1.10      parser    560: 
1.90      misha     561:        // ^date:calendar[rus|eng](year;month)  = table
                    562:        // ^date:calendar[rus|eng](year;month;day) = table
1.10      parser    563:        add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1       parser    564: 
1.78      misha     565:        // ^date.unix-timestamp[]
1.90      misha     566:        // ^date::unix-timestamp(timestamp)
1.49      paf       567:        add_native_method("unix-timestamp", Method::CT_DYNAMIC, _unix_timestamp, 0, 1);
1.1       parser    568: }

E-mail: