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