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

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

E-mail: