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

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

E-mail: