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

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

E-mail: