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

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

E-mail: