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

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.105   ! moko       16: volatile const char * IDENT_DATE_C="$Id: date.C,v 1.104 2016/07/22 16:53:46 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: 
1.103     moko       30: DECLARE_CLASS_VAR(date, new MDate);
1.46      paf        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.104     moko       73: 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.102     moko      257:                        if(Value* adate=params[0].as(VDATE_TYPE))
                    258:                                vdate.set_tz(static_cast<VDate*>(adate)->get_tz());
1.94      moko      259:                        vdate.set_time(round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY));
1.28      paf       260:                }
1.102     moko      261:        } else { // ^create(y;m;d[;h[;m[;s[;TZ]]]])
1.56      paf       262:                tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
1.1       parser    263:                tmIn.tm_isdst=-1;
1.94      moko      264:                tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));
1.46      paf       265:                tmIn.tm_mon=params.as_int(1, "month must be int", r)-1;
                    266:                tmIn.tm_mday=params.count()>2?params.as_int(2, "mday must be int", r):1;
1.95      moko      267:                if(params.count()>3) tmIn.tm_hour=params.as_int(3, "hour must be int", r);
1.46      paf       268:                if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r);
                    269:                if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r);
1.102     moko      270:                if(params.count()>6) vdate.set_tz(params.as_string(6, "TZ must be string").cstr());
1.94      moko      271:                vdate.set_tm(tmIn);
1.74      misha     272:        };
1.1       parser    273: }
                    274: 
1.90      misha     275: static void _sql_string(Request& r, MethodParams& params) {
1.46      paf       276:        VDate& vdate=GET_SELF(r, VDate);
1.85      misha     277: 
1.90      misha     278:        VDate::sql_string_type format = VDate::sql_string_datetime;
                    279:        if(params.count() > 0) {
                    280:                const String& what=params.as_string(0, "'type' must be string");
                    281:                if(what.is_empty() || what == "datetime")
                    282:                        format = VDate::sql_string_datetime;
                    283:                else if(what == "date")
                    284:                        format=VDate::sql_string_date;
                    285:                else if(what == "time")
                    286:                        format=VDate::sql_string_time;
                    287:                else
                    288:                        throw Exception(PARSER_RUNTIME, &what, "'type' must be 'date', 'time' or 'datetime'");
                    289:        }
                    290: 
                    291:        r.write_assign_lang(*vdate.get_sql_string(format));
1.1       parser    292: }
                    293: 
1.80      misha     294: static void _gmt_string(Request& r, MethodParams&) {
                    295:        VDate& vdate=GET_SELF(r, VDate);
                    296: 
1.88      misha     297:        r.write_assign_lang(*vdate.get_gmt_string());
1.80      misha     298: }
                    299: 
1.100     moko      300: static void _iso_string(Request& r, MethodParams& params) {
1.95      moko      301:        VDate& vdate=GET_SELF(r, VDate);
                    302: 
1.100     moko      303:        VDate::iso_string_type format=VDate::iso_string_default;
                    304: 
                    305:        if(params.count()>0)
                    306:                if(HashStringValue* options=params.as_hash(0)){
                    307:                        int valid_options=0;
                    308:                        if(Value* vshow_ms=options->get("ms")){
1.105   ! moko      309:                                if(r.process(*vshow_ms).as_bool())
1.100     moko      310:                                        format=VDate::iso_string_type(format|VDate::iso_string_ms);
                    311:                                valid_options++;
                    312:                        }
                    313:                        if(Value* vshow_colon=options->get("colon")){
1.105   ! moko      314:                                if(!r.process(*vshow_colon).as_bool())
1.100     moko      315:                                        format=VDate::iso_string_type(format|VDate::iso_string_no_colon);
                    316:                                valid_options++;
                    317:                        }
                    318:                        if(Value* vshow_z=options->get("z")){
1.105   ! moko      319:                                if(!r.process(*vshow_z).as_bool())
1.100     moko      320:                                        format=VDate::iso_string_type(format|VDate::iso_string_no_z);
                    321:                                valid_options++;
                    322:                        }
                    323:                        if(valid_options != options->count())
                    324:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    325:                }
                    326: 
                    327:        r.write_assign_lang(*vdate.get_iso_string(format));
1.95      moko      328: }
                    329: 
1.46      paf       330: static void _roll(Request& r, MethodParams& params) {
                    331:        const String& what=params.as_string(0, "'what' must be string");
1.43      paf       332:        int oyear=0;
                    333:        int omonth=0;
                    334:        int oday=0;
1.1       parser    335:        int *offset;
                    336:        if(what=="year") offset=&oyear;
                    337:        else if(what=="month") offset=&omonth;
                    338:        else if(what=="day") offset=&oday;
1.48      paf       339:        else if(what=="TZ") {
                    340:                const String& argument_tz=params.as_string(1, "'TZ' must be string");
1.95      moko      341:                if(&r.get_self() == date_class){
                    342:                        VDate::set_default_tz(argument_tz.cstr());
                    343:                } else {
                    344:                        VDate& vdate=GET_SELF(r, VDate);
                    345:                        vdate.set_tz(argument_tz.cstr());
                    346:                        vdate.set_time(vdate.get_time());
                    347:                }
1.48      paf       348:                return;
                    349:        } else
1.94      moko      350:                throw Exception(PARSER_RUNTIME, &what, "must be year|month|day|TZ");
                    351: 
                    352:        if(&r.get_self() == date_class)
                    353:                throw Exception(PARSER_RUNTIME, &what, "must be TZ to be called statically");
                    354: 
                    355:        VDate& vdate=GET_SELF(r, VDate);
1.1       parser    356:        
1.46      paf       357:        *offset=params.as_int(1, "offset must be int", r);
1.1       parser    358: 
1.94      moko      359:        tm tmIn=vdate.get_tm();
1.13      paf       360:        tm tmSaved=tmIn;
1.43      paf       361:        int adjust_day=0;
                    362:        while(true) {
                    363:                tmIn.tm_year+=oyear;
                    364:                tmIn.tm_mon+=omonth;
                    365:                tmIn.tm_mday+=oday+adjust_day;
1.94      moko      366:                tmIn.tm_hour=24/2;
1.43      paf       367:                tmIn.tm_min=0;
                    368:                tmIn.tm_sec=0;
1.94      moko      369:                int saved_day=tmIn.tm_mday;
                    370:                vdate.set_tm(tmIn); /* normalize */
                    371: 
                    372:                if(oday==0 && tmIn.tm_mday!=saved_day /* but it changed */ ) {
                    373:                        if(adjust_day <= -3 /* 31->28 max, so never, but... */ )
                    374:                                throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "bad resulting time (day hole still with %d day adjustment)", adjust_day );
1.43      paf       375:                        
                    376:                        tmIn=tmSaved; // restoring
                    377:                        --adjust_day; //retrying with prev day
                    378:                } else
1.94      moko      379:                        break;
1.43      paf       380:        }
1.13      paf       381: 
1.94      moko      382:        tmIn.tm_hour=tmSaved.tm_hour;
                    383:        tmIn.tm_min=tmSaved.tm_min;
                    384:        tmIn.tm_sec=tmSaved.tm_sec;
                    385:        tmIn.tm_isdst=-1;
                    386: 
                    387:        vdate.set_tm(tmIn);
1.1       parser    388: }
                    389: 
1.46      paf       390: static Table& fill_month_days(Request& r, MethodParams& params, bool rus){
1.45      paf       391:        Table::Action_options table_options;
1.46      paf       392:        Table& result=*new Table(date_calendar_table_template, table_options);
                    393:        
1.94      moko      394:        tm tmIn;
                    395:        memset(&tmIn, 0, sizeof(tmIn));
                    396:        tmIn.tm_year=to_year(params.as_int(1, "year must be int", r));
                    397:        tmIn.tm_mon=to_month(params.as_int(2, "month must be int", r));
1.55      paf       398:        tmIn.tm_mday=1;
                    399: 
1.94      moko      400:        VDate t(tmIn); /* normalize */
                    401:        int weekDay1=tmIn.tm_wday;
                    402: 
                    403:        if(rus)
1.10      parser    404:                weekDay1=weekDay1?weekDay1-1:6; //sunday last
1.94      moko      405:        int monthDays=VDate::getMonthDays(tmIn.tm_year, tmIn.tm_mon);
1.43      paf       406:        
                    407:        for(int _day=1-weekDay1; _day<=monthDays;) {
1.46      paf       408:                Table::element_type row(new ArrayString(7));
1.34      paf       409:                // calculating year week no [1..54]
1.85      misha     410:                int weekyear=0; // surely would be assigned to, but to calm down compiler
                    411:                int weekno=0; // same
1.34      paf       412:                // 0..6 week days-cells fill with month days
1.43      paf       413:                for(int wday=0; wday<7; wday++, _day++) {
1.85      misha     414:                        *row+=(_day>=1 && _day<=monthDays)?new String(_day, "%02d"):new String();
1.34      paf       415: 
                    416:                        if(wday==(rus?3:4)/*thursday*/) {
1.57      paf       417:                                tm tms;
1.94      moko      418:                                memset(&tms, 0, sizeof(tms));
1.57      paf       419:                                tms.tm_mday=_day;
1.94      moko      420:                                tms.tm_mon=tmIn.tm_mon;
                    421:                                tms.tm_year=tmIn.tm_year;
1.57      paf       422:                                
1.94      moko      423:                                VDate ts(tms); /*normalize*/
1.36      paf       424:                                weekyear=tms.tm_year+1900;
1.85      misha     425:                                weekno=VDate::CalcWeek(tms).week;
                    426:                        }
                    427:                }
                    428:                // appending week no
                    429:                *row+=new String(weekno, "%02d");
1.36      paf       430: 
1.85      misha     431:                // appending week year
                    432:                *row+=new String(weekyear, "%04d");
1.34      paf       433:                
1.46      paf       434:                result+=row;
                    435:        }
                    436:        
                    437:        return result;
1.10      parser    438: }
                    439: 
1.46      paf       440: static Table& fill_week_days(Request& r, MethodParams& params, bool rus){
                    441:        Table::columns_type columns(new ArrayString(4));
                    442:        *columns+=new String("year");
                    443:        *columns+=new String("month");
                    444:        *columns+=new String("day");
                    445:        *columns+=new String("weekday");
                    446:        Table& result=*new Table(columns);
                    447: 
1.55      paf       448:        tm tmIn;
1.94      moko      449:        memset(&tmIn, 0, sizeof(tmIn));
                    450:        tmIn.tm_year=to_year(params.as_int(1, "year must be int", r));
                    451:        tmIn.tm_mon=to_month(params.as_int(2, "month must be int", r));
                    452:        tmIn.tm_mday=params.as_int(3, "day must be int", r);
1.55      paf       453:        tmIn.tm_hour=18;
1.84      misha     454: 
1.94      moko      455:        VDate t(tmIn); /* normalize */
                    456:        int baseWeekDay=tmIn.tm_wday;
                    457: 
1.10      parser    458:        if(rus) 
                    459:                baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
1.46      paf       460:        
1.94      moko      461:        t.set_time(t.get_time()-baseWeekDay*SECS_PER_DAY);
1.46      paf       462:                
1.94      moko      463:        for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t.set_time(t.get_time()+SECS_PER_DAY)) {
1.46      paf       464:                Table::element_type row(new ArrayString(4));
1.94      moko      465: 
                    466:                tm tmOut=t.get_tm();
                    467:                *row+=new String(1900+tmOut.tm_year, "%04d");
                    468:                *row+=new String(1+tmOut.tm_mon, "%02d");
                    469:                *row+=new String(tmOut.tm_mday, "%02d");
                    470:                *row+=new String(tmOut.tm_wday, "%02d");
1.85      misha     471: 
1.46      paf       472:                result+=row;
                    473:        }
                    474:        
                    475:        return result;
1.10      parser    476: }
                    477: 
1.46      paf       478: static void _calendar(Request& r, MethodParams& params) {
                    479:        const String& what=params.as_string(0, "format must be strig");
1.10      parser    480:        bool rus=false;
                    481:        if(what=="rus")
                    482:                rus=true;
                    483:        else if(what=="eng")
                    484:                rus=false;
                    485:        else
1.94      moko      486:                throw Exception(PARSER_RUNTIME, &what, "must be rus|eng");
1.10      parser    487: 
1.46      paf       488:        Table* table;
                    489:        if(params.count()==1+2) 
                    490:                table=&fill_month_days(r, params, rus);
1.10      parser    491:        else // 1+3
1.46      paf       492:                table=&fill_week_days(r, params, rus);
1.10      parser    493: 
1.46      paf       494:        r.write_no_lang(*new VTable(table));
1.10      parser    495: }
                    496: 
1.49      paf       497: static void _unix_timestamp(Request& r, MethodParams& params) {
                    498:        VDate& vdate=GET_SELF(r, VDate);
                    499: 
                    500:        if(params.count()==0) { 
                    501:                // ^date.unix-timestamp[]
1.94      moko      502:                r.write_no_lang(*new VDouble((double)vdate.get_time()));
1.49      paf       503:        } else {
1.50      paf       504:                if(vdate.get_time())
1.94      moko      505:                        throw Exception(PARSER_RUNTIME, 0, "date object already constructed");
1.50      paf       506:                // ^unix-timestamp(time_t)
1.94      moko      507:                vdate.set_time(params.as_double(0, "Unix timestamp must be number", r));
1.49      paf       508:        }
                    509: }
                    510: 
1.79      misha     511: static void _last_day(Request& r, MethodParams& params) {
1.94      moko      512:        tm tmIn;
1.78      misha     513:        if(&r.get_self() == date_class) {
1.81      misha     514:                if(params.count() != 2)
1.94      moko      515:                        throw Exception(PARSER_RUNTIME, 0, "year and month must be defined");
1.81      misha     516:                // ^date:lastday(year;month)
1.94      moko      517:                tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));
                    518:                tmIn.tm_mon=to_month(params.as_int(1, "month must be int", r));
1.78      misha     519:        } else {
1.94      moko      520:                if(params.count() != 0)
                    521:                        throw Exception(PARSER_RUNTIME, 0, "year and month must not be defined");
1.78      misha     522:                // ^date.lastday[]
1.94      moko      523:                tmIn=GET_SELF(r, VDate).get_tm();
1.78      misha     524:        }
1.94      moko      525:        r.write_no_lang(*new VInt(VDate::getMonthDays(tmIn.tm_year, tmIn.tm_mon)));
1.78      misha     526: }
                    527: 
1.1       parser    528: // constructor
                    529: 
1.46      paf       530: MDate::MDate(): Methoded("date") {
1.78      misha     531:        // ^date::now[]
1.90      misha     532:        // ^date::now(offset float days)
1.23      paf       533:        add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1       parser    534: 
1.90      misha     535:        // ^date::today[]
                    536:        add_native_method("today", Method::CT_DYNAMIC, _today, 0, 0);
                    537: 
1.78      misha     538:        // ^date::create(float days)
1.90      misha     539:        // ^date::create[date]
1.102     moko      540:        // ^date::create(year;month;day[;hour[;minute[;sec[;TZ]]]])
1.90      misha     541:        // ^date::create[yyyy-mm-dd[ hh:mm:ss]]
                    542:        // ^date::create[hh:mm:ss]
1.102     moko      543:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 7);
1.28      paf       544:        // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.102     moko      545:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 7);
1.1       parser    546: 
1.78      misha     547:        // ^date.sql-string[]
1.90      misha     548:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 1);
1.1       parser    549: 
1.80      misha     550:        // ^date.gmt-string[]
                    551:        add_native_method("gmt-string", Method::CT_DYNAMIC, _gmt_string, 0, 0);
                    552: 
1.100     moko      553:        // ^date.iso-string[$.colon(true) $.z(true) $.ms(false)]
                    554:        add_native_method("iso-string", Method::CT_DYNAMIC, _iso_string, 0, 1);
1.95      moko      555: 
1.78      misha     556:        // ^date:lastday(year;month)
                    557:        // ^date.lastday[]
1.79      misha     558:        add_native_method("last-day", Method::CT_ANY, _last_day, 0, 2);
1.78      misha     559: 
1.90      misha     560:        // ^date.roll[year|month|day](+/- 1)
1.94      moko      561:        add_native_method("roll", Method::CT_ANY, _roll, 2, 2);
1.10      parser    562: 
1.90      misha     563:        // ^date:calendar[rus|eng](year;month)  = table
                    564:        // ^date:calendar[rus|eng](year;month;day) = table
1.10      parser    565:        add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1       parser    566: 
1.78      misha     567:        // ^date.unix-timestamp[]
1.90      misha     568:        // ^date::unix-timestamp(timestamp)
1.49      paf       569:        add_native_method("unix-timestamp", Method::CT_DYNAMIC, _unix_timestamp, 0, 1);
1.1       parser    570: }

E-mail: