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

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

E-mail: