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