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