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