Annotation of parser3/src/classes/date.C, revision 1.91
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.91 ! moko 16: volatile const char * IDENT_DATE_C="$Id: 2012-03-06 07:41:49 $" IDENT_PA_VDATE_H;
! 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:
70: vdate.set_time(today);
71: }
72:
1.59 paf 73: /// shrinked range: 1970/1/1 to 2038/1/1
1.78 misha 74: static int to_year(int iyear) {
1.59 paf 75: if(iyear<1970 || iyear>2038)
1.83 misha 76: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.59 paf 77: 0,
78: "year '%d' is out of valid range", iyear);
1.78 misha 79: return iyear;
80: }
81:
82: static int to_month(int imonth) {
83: return max(1, min(imonth, 12)) -1;
1.59 paf 84: }
85:
1.78 misha 86: static int to_tm_year(int iyear) {
87: return to_year(iyear)-1900;
88: }
89:
90:
1.41 paf 91: // 2002-04-25 18:14:00
92: // 18:14:00
93: // 2002:04:25 [+maybe time]
1.65 paf 94: /*not static, used in image.C*/ tm cstr_to_time_t(char *cstr) {
1.64 paf 95: if( !cstr || !*cstr )
1.83 misha 96: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.64 paf 97: 0,
98: "empty string is not valid datetime");
99:
1.41 paf 100: char *cur=cstr;
1.73 paf 101: char date_delim=isdigit((unsigned char)cur[0])&&isdigit((unsigned char)cur[1])&&isdigit((unsigned char)cur[2])&&isdigit((unsigned char)cur[3])&&cur[4]==':'?':'
1.41 paf 102: :'-';
1.46 paf 103: const char* year=lsplit(&cur, date_delim);
104: const char* month=lsplit(&cur, date_delim);
105: const char* mday=lsplit(&cur, ' ');
1.41 paf 106: if(!month)
107: cur=cstr;
1.46 paf 108: const char* hour=lsplit(&cur, ':');
109: const char* min=lsplit(&cur, ':');
1.66 paf 110: const char* sec=lsplit(&cur, '.');
111: const char* msec=cur;
1.41 paf 112:
1.56 paf 113: tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
1.41 paf 114: tmIn.tm_isdst=-1;
115: if(!month)
116: if(min) {
117: year=mday=0; // HH:MM
118: time_t t=time(0);
119: tm *tmNow=localtime(&t);
120: tmIn.tm_year=tmNow->tm_year;
121: tmIn.tm_mon=tmNow->tm_mon;
122: tmIn.tm_mday=tmNow->tm_mday;
123: goto date_part_set;
124: } else
1.66 paf 125: hour=min=sec=msec=0; // not YYYY- & not HH: = just YYYY
1.61 paf 126: tmIn.tm_year=to_tm_year(pa_atoi(year));
1.60 paf 127: tmIn.tm_mon=month?pa_atoi(month)-1:0;
128: tmIn.tm_mday=mday?pa_atoi(mday):1;
1.41 paf 129: date_part_set:
1.65 paf 130: tmIn.tm_hour=hour?pa_atoi(hour):0;
1.60 paf 131: tmIn.tm_min=min?pa_atoi(min):0;
1.64 paf 132: tmIn.tm_sec=sec?pa_atoi(sec):0;
1.66 paf 133: //tmIn.tm_[msec<<no such, waits reimplementation of the class]=f(msec);
1.65 paf 134: return tmIn;
1.41 paf 135: }
136:
1.46 paf 137: static void _create(Request& r, MethodParams& params) {
138: VDate& vdate=GET_SELF(r, VDate);
1.1 parser 139:
1.82 misha 140: if(params.count()==1){
141: if(const String* sdate=params[0].get_string()){ // ^create[2002-04-25 18:14:00] ^create[18:14:00]
1.65 paf 142: vdate.set_time(cstr_to_time_t(sdate->cstrm()));
1.82 misha 143: } else { // ^create(float days) or ^create[date object]
1.65 paf 144: time_t t=(time_t)round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY);
1.28 paf 145: if(t<0 || !localtime(&t))
1.83 misha 146: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.46 paf 147: 0,
1.28 paf 148: "invalid datetime");
1.65 paf 149: vdate.set_time(t);
1.28 paf 150: }
1.74 misha 151: } else { // ^create(y;m;d[;h[;m[;s]]])
1.75 misha 152: assert(params.count()<=6);
1.56 paf 153: tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
1.1 parser 154: tmIn.tm_isdst=-1;
1.61 paf 155: tmIn.tm_year=to_tm_year(params.as_int(0, "year must be int", r));
1.46 paf 156: tmIn.tm_mon=params.as_int(1, "month must be int", r)-1;
157: tmIn.tm_mday=params.count()>2?params.as_int(2, "mday must be int", r):1;
1.64 paf 158: int savedHour=0;
159: if(params.count()>3) savedHour=tmIn.tm_hour=params.as_int(3, "hour must be int", r);
1.46 paf 160: if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r);
161: if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r);
1.65 paf 162: vdate.set_time(tmIn);
1.74 misha 163: };
1.1 parser 164: }
165:
1.90 misha 166: static void _sql_string(Request& r, MethodParams& params) {
1.46 paf 167: VDate& vdate=GET_SELF(r, VDate);
1.85 misha 168:
1.90 misha 169: VDate::sql_string_type format = VDate::sql_string_datetime;
170: if(params.count() > 0) {
171: const String& what=params.as_string(0, "'type' must be string");
172: if(what.is_empty() || what == "datetime")
173: format = VDate::sql_string_datetime;
174: else if(what == "date")
175: format=VDate::sql_string_date;
176: else if(what == "time")
177: format=VDate::sql_string_time;
178: else
179: throw Exception(PARSER_RUNTIME, &what, "'type' must be 'date', 'time' or 'datetime'");
180: }
181:
182: r.write_assign_lang(*vdate.get_sql_string(format));
1.1 parser 183: }
184:
1.80 misha 185: static void _gmt_string(Request& r, MethodParams&) {
186: VDate& vdate=GET_SELF(r, VDate);
187:
1.88 misha 188: r.write_assign_lang(*vdate.get_gmt_string());
1.80 misha 189: }
190:
1.46 paf 191: static void _roll(Request& r, MethodParams& params) {
192: VDate& vdate=GET_SELF(r, VDate);
1.1 parser 193:
1.46 paf 194: const String& what=params.as_string(0, "'what' must be string");
1.43 paf 195: int oyear=0;
196: int omonth=0;
197: int oday=0;
1.1 parser 198: int *offset;
199: if(what=="year") offset=&oyear;
200: else if(what=="month") offset=&omonth;
201: else if(what=="day") offset=&oday;
1.48 paf 202: else if(what=="TZ") {
203: const String& argument_tz=params.as_string(1, "'TZ' must be string");
204: vdate.set_tz(&argument_tz);
205: return;
206: } else
1.77 misha 207: throw Exception(PARSER_RUNTIME,
1.1 parser 208: &what,
1.48 paf 209: "must be year|month|day|TZ");
1.1 parser 210:
1.46 paf 211: *offset=params.as_int(1, "offset must be int", r);
1.1 parser 212:
1.46 paf 213: time_t self_time=vdate.get_time();
1.13 paf 214: tm tmIn=*localtime(&self_time);
215: tm tmSaved=tmIn;
1.43 paf 216: int adjust_day=0;
217: time_t t_changed_date;
218: while(true) {
219: tmIn.tm_year+=oyear;
220: tmIn.tm_mon+=omonth;
221: tmIn.tm_mday+=oday+adjust_day;
222: tmIn.tm_hour=24/2;
223: tmIn.tm_min=0;
224: tmIn.tm_sec=0;
1.44 paf 225: int saved_mon=(tmIn.tm_mon+12*100)%12; // crossing year boundary backwards
1.43 paf 226: t_changed_date=mktime/*normalizetime*/(&tmIn);
227: if(t_changed_date<0)
1.83 misha 228: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.46 paf 229: 0,
1.43 paf 230: "bad resulting time (rolled out of valid date range)");
1.46 paf 231: if(oday==0 && tmIn.tm_mon!=saved_mon/*but it changed*/) {
1.43 paf 232: if(adjust_day <= -3/*31->28 max, so never, but...*/)
1.83 misha 233: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.46 paf 234: 0,
1.43 paf 235: "bad resulting time (day hole still with %d day adjustment)", adjust_day );
236:
237: tmIn=tmSaved; // restoring
238: --adjust_day; //retrying with prev day
239: } else
240: break;
241: }
1.13 paf 242:
1.43 paf 243: tm *tmOut=localtime(&t_changed_date);
1.13 paf 244: if(!tmOut)
1.83 misha 245: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.46 paf 246: 0,
1.43 paf 247: "bad resulting time (seconds from epoch=%d)", t_changed_date);
1.13 paf 248:
1.43 paf 249: tmOut->tm_hour=tmSaved.tm_hour;
250: tmOut->tm_min=tmSaved.tm_min;
251: tmOut->tm_sec=tmSaved.tm_sec;
1.20 paf 252: tmOut->tm_isdst=-1;
1.13 paf 253: {
1.43 paf 254: time_t t_changed_time=mktime/*normalizetime*/(tmOut);
1.63 paf 255: /*autofix: in msk timezone last sunday of march hour hole: [2am->3am)
1.21 paf 256: if(
257: tmOut->tm_hour!=tmSaved.tm_hour
258: ||tmOut->tm_min!=tmSaved.tm_min)
1.22 paf 259: throw Exception(0,
1.46 paf 260: 0,
1.24 paf 261: "bad resulting time (hour hole)");
1.43 paf 262: */
1.21 paf 263:
1.43 paf 264: if(t_changed_time<0)
1.83 misha 265: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.46 paf 266: 0,
1.21 paf 267: "bad resulting time (after reconstruction)");
1.13 paf 268:
1.46 paf 269: vdate.set_time(t_changed_time);
1.13 paf 270: }
1.1 parser 271: }
272:
1.46 paf 273: static Table& fill_month_days(Request& r, MethodParams& params, bool rus){
1.45 paf 274: Table::Action_options table_options;
1.46 paf 275: Table& result=*new Table(date_calendar_table_template, table_options);
276:
1.78 misha 277: int year=to_year(params.as_int(1, "year must be int", r));
278: int month=to_month(params.as_int(2, "month must be int", r));
1.43 paf 279:
1.55 paf 280: tm tmIn;
1.56 paf 281: memset(&tmIn, 0, sizeof(tmIn));
1.55 paf 282: tmIn.tm_mday=1;
283: tmIn.tm_mon=month;
1.58 paf 284: tmIn.tm_year=year-1900;
1.55 paf 285:
1.43 paf 286: time_t t=mktime(&tmIn);
1.10 parser 287: if(t<0)
1.83 misha 288: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.46 paf 289: 0,
1.10 parser 290: "invalid date");
1.43 paf 291: tm *tmOut=localtime(&t);
292:
293: int weekDay1=tmOut->tm_wday;
1.10 parser 294: if(rus)
295: weekDay1=weekDay1?weekDay1-1:6; //sunday last
1.43 paf 296: int monthDays=getMonthDays(year, month);
297:
298: for(int _day=1-weekDay1; _day<=monthDays;) {
1.46 paf 299: Table::element_type row(new ArrayString(7));
1.34 paf 300: // calculating year week no [1..54]
1.85 misha 301: int weekyear=0; // surely would be assigned to, but to calm down compiler
302: int weekno=0; // same
1.34 paf 303: // 0..6 week days-cells fill with month days
1.43 paf 304: for(int wday=0; wday<7; wday++, _day++) {
1.85 misha 305: *row+=(_day>=1 && _day<=monthDays)?new String(_day, "%02d"):new String();
1.34 paf 306:
307: if(wday==(rus?3:4)/*thursday*/) {
1.57 paf 308: tm tms;
309: memset(&tms, 0, sizeof(tmIn));
310: tms.tm_mday=_day;
311: tms.tm_mon=month;
1.58 paf 312: tms.tm_year=year-1900;
1.57 paf 313:
1.34 paf 314: /*normalize*/mktime(&tms);
1.36 paf 315: weekyear=tms.tm_year+1900;
1.85 misha 316: weekno=VDate::CalcWeek(tms).week;
317: }
318: }
319: // appending week no
320: *row+=new String(weekno, "%02d");
1.36 paf 321:
1.85 misha 322: // appending week year
323: *row+=new String(weekyear, "%04d");
1.34 paf 324:
1.46 paf 325: result+=row;
326: }
327:
328: return result;
1.10 parser 329: }
330:
1.46 paf 331: static Table& fill_week_days(Request& r, MethodParams& params, bool rus){
332: Table::columns_type columns(new ArrayString(4));
333: *columns+=new String("year");
334: *columns+=new String("month");
335: *columns+=new String("day");
336: *columns+=new String("weekday");
337: Table& result=*new Table(columns);
338:
1.78 misha 339: int year=to_year(params.as_int(1, "year must be int", r));
340: int month=to_month(params.as_int(2, "month must be int", r));
1.46 paf 341: int day=params.as_int(3, "day must be int", r);
342:
1.55 paf 343: tm tmIn;
1.56 paf 344: memset(&tmIn, 0, sizeof(tmIn));
1.55 paf 345: tmIn.tm_hour=18;
346: tmIn.tm_mday=day;
347: tmIn.tm_mon=month;
1.58 paf 348: tmIn.tm_year=year-1900;
1.55 paf 349:
1.46 paf 350: time_t t=mktime(&tmIn);
1.10 parser 351: if(t<0)
1.83 misha 352: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.46 paf 353: 0,
1.10 parser 354: "invalid date");
1.46 paf 355: tm *tmOut=localtime(&t);
1.84 misha 356:
1.46 paf 357: int baseWeekDay=tmOut->tm_wday;
1.10 parser 358: if(rus)
359: baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
1.46 paf 360:
361: t-=baseWeekDay*SECS_PER_DAY;
362:
363: for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
364: tm *tmOut=localtime(&t);
365: Table::element_type row(new ArrayString(4));
1.85 misha 366:
367: *row+=new String(1900+tmOut->tm_year, "%04d");
368: *row+=new String(1+tmOut->tm_mon, "%02d");
369: *row+=new String(tmOut->tm_mday, "%02d");
370: *row+=new String(tmOut->tm_wday, "%02d");
371:
1.46 paf 372: result+=row;
373: }
374:
375: return result;
1.10 parser 376: }
377:
1.46 paf 378: static void _calendar(Request& r, MethodParams& params) {
379: const String& what=params.as_string(0, "format must be strig");
1.10 parser 380: bool rus=false;
381: if(what=="rus")
382: rus=true;
383: else if(what=="eng")
384: rus=false;
385: else
1.77 misha 386: throw Exception(PARSER_RUNTIME,
1.10 parser 387: &what,
388: "must be rus|eng");
389:
1.46 paf 390: Table* table;
391: if(params.count()==1+2)
392: table=&fill_month_days(r, params, rus);
1.10 parser 393: else // 1+3
1.46 paf 394: table=&fill_week_days(r, params, rus);
1.10 parser 395:
1.46 paf 396: r.write_no_lang(*new VTable(table));
1.10 parser 397: }
398:
1.49 paf 399: static void _unix_timestamp(Request& r, MethodParams& params) {
400: VDate& vdate=GET_SELF(r, VDate);
401:
402: if(params.count()==0) {
403: // ^date.unix-timestamp[]
404: r.write_no_lang(*new VInt((int)vdate.get_time()));
405: } else {
1.50 paf 406: if(vdate.get_time())
1.83 misha 407: throw Exception(PARSER_RUNTIME,
1.50 paf 408: 0,
409: "date object already constructed");
1.51 paf 410:
1.50 paf 411: // ^unix-timestamp(time_t)
412: time_t t=(time_t)params.as_int(0, "Unix timestamp must be integer", r);
1.49 paf 413:
1.50 paf 414: vdate.set_time(t);
1.49 paf 415: }
416: }
417:
1.79 misha 418: static void _last_day(Request& r, MethodParams& params) {
1.78 misha 419: int year;
420: int month;
421: if(&r.get_self() == date_class) {
1.81 misha 422: if(params.count() != 2)
1.78 misha 423: throw Exception(PARSER_RUNTIME,
424: 0,
425: "year and month must be defined");
1.81 misha 426:
427: // ^date:lastday(year;month)
428: year=to_year(params.as_int(0, "year must be int", r));
429: month=to_month(params.as_int(1, "month must be int", r));
1.78 misha 430: } else {
431: // ^date.lastday[]
432: tm &tmIn=GET_SELF(r, VDate).get_localtime();
433: year=tmIn.tm_year+1900;
434: month=tmIn.tm_mon;
435: }
1.81 misha 436: r.write_no_lang(*new VInt(getMonthDays(year, month)));
1.78 misha 437: }
438:
1.49 paf 439:
1.1 parser 440: // constructor
441:
1.46 paf 442: MDate::MDate(): Methoded("date") {
1.78 misha 443: // ^date::now[]
1.90 misha 444: // ^date::now(offset float days)
1.23 paf 445: add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1 parser 446:
1.90 misha 447: // ^date::today[]
448: add_native_method("today", Method::CT_DYNAMIC, _today, 0, 0);
449:
1.78 misha 450: // ^date::create(float days)
1.90 misha 451: // ^date::create[date]
452: // ^date::create(year;month;day[;hour[;minute[;sec]]])
453: // ^date::create[yyyy-mm-dd[ hh:mm:ss]]
454: // ^date::create[hh:mm:ss]
1.17 paf 455: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
1.28 paf 456: // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.17 paf 457: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1 parser 458:
1.78 misha 459: // ^date.sql-string[]
1.90 misha 460: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 1);
1.1 parser 461:
1.80 misha 462: // ^date.gmt-string[]
463: add_native_method("gmt-string", Method::CT_DYNAMIC, _gmt_string, 0, 0);
464:
1.78 misha 465: // ^date:lastday(year;month)
466: // ^date.lastday[]
1.79 misha 467: add_native_method("last-day", Method::CT_ANY, _last_day, 0, 2);
1.78 misha 468:
1.90 misha 469: // ^date.roll[year|month|day](+/- 1)
1.1 parser 470: add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10 parser 471:
1.90 misha 472: // ^date:calendar[rus|eng](year;month) = table
473: // ^date:calendar[rus|eng](year;month;day) = table
1.10 parser 474: add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1 parser 475:
1.78 misha 476: // ^date.unix-timestamp[]
1.90 misha 477: // ^date::unix-timestamp(timestamp)
1.49 paf 478: add_native_method("unix-timestamp", Method::CT_DYNAMIC, _unix_timestamp, 0, 1);
1.1 parser 479: }
E-mail: