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

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

E-mail: