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