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