Annotation of parser3/src/classes/date.C, revision 1.40
1.1 parser 1: /** @file
2: Parser: @b date parser class.
3:
1.15 paf 4: Copyright (c) 2001, 2002 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.40 ! paf 8: static const char* IDENT_DATE_C="$Date: 2002/09/23 09:50:18 $";
1.1 parser 9:
10: #include "classes.h"
11: #include "pa_request.h"
12: #include "pa_vdouble.h"
13: #include "pa_vdate.h"
1.10 parser 14: #include "pa_vtable.h"
1.1 parser 15:
16: // class
17:
18: class MDate : public Methoded {
19: public: // VStateless_class
20: Value *create_new_value(Pool& pool) { return new(pool) VDate(pool, 0); }
21:
22: public:
23: MDate(Pool& pool);
24: public: // Methoded
25: bool used_directly() { return true; }
26: };
27:
28: // methods
29:
1.23 paf 30: static void _now(Request& r, const String& method_name, MethodParams *params) {
1.1 parser 31: Pool& pool=r.pool();
1.37 paf 32: VDate *vdate=static_cast<VDate *>(r.get_self());
1.23 paf 33:
34: time_t t=time(0);
35: if(params->size()==1) // ^now(offset)
1.39 paf 36: t+=(time_t)round(params->as_double(0, "offset must be double", r)*SECS_PER_DAY);
1.23 paf 37:
38: vdate->set_time(t);
1.1 parser 39: }
40:
1.28 paf 41: static int NN_year_to_NNNN(int year) {
42: if(year<70) // 0..69 -> 100..169 [2000..2069]
43: year+=100;
44: if(year>=1900)
45: year-=1900;
46: return year;
47: }
48:
49: /// @test 09 ok? [octal maybe]
1.17 paf 50: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.1 parser 51: Pool& pool=r.pool();
1.37 paf 52: VDate *vdate=static_cast<VDate *>(r.get_self());
1.1 parser 53:
1.23 paf 54: time_t t;
1.28 paf 55: if(params->size()==1) {
1.31 paf 56: // ^create[2002-04-25 18:14:00]
57: // ^create[18:14:00]
58: if(const String *sdate=params->get(0).get_string()) {
1.28 paf 59: char *cstr=sdate->cstr();
1.31 paf 60: char *cur=cstr;
61: const char *year=lsplit(&cur, '-');
62: const char *month=lsplit(&cur, '-');
63: const char *mday=lsplit(&cur, ' ');
64: if(!month)
65: cur=cstr;
66: const char *hour=lsplit(&cur, ':');
67: const char *min=lsplit(&cur, ':');
68: const char *sec=cur;
69:
1.28 paf 70: tm tmIn={0};
71: tmIn.tm_isdst=-1;
1.31 paf 72: if(!month)
73: if(min) {
74: year=mday=0; // HH:MM
75: time_t t=time(0);
76: tm *tmNow=localtime(&t);
77: tmIn.tm_year=tmNow->tm_year;
78: tmIn.tm_mon=tmNow->tm_mon;
79: tmIn.tm_mday=tmNow->tm_mday;
80: goto date_part_set;
81: } else
82: hour=min=sec=0; // not YYYY- & not HH: = just YYYY
1.28 paf 83: tmIn.tm_year=NN_year_to_NNNN(atoi(year));
84: tmIn.tm_mon=month?atoi(month)-1:0;
85: tmIn.tm_mday=mday?atoi(mday):1;
1.31 paf 86: date_part_set:
1.28 paf 87: tmIn.tm_hour=hour?atoi(hour):0;
88: tmIn.tm_min=min?atoi(min):0;
89: tmIn.tm_sec=sec?atoi(sec):0;
90: t=mktime(&tmIn);
91: if(t<0)
92: throw Exception(0,
93: sdate,
94: "invalid datetime");
95: } else { // ^create(float days)
1.38 paf 96: t=(time_t)round(params->as_double(0, "float days must be double", r)*SECS_PER_DAY);
1.28 paf 97: if(t<0 || !localtime(&t))
98: throw Exception(0,
99: &method_name,
100: "invalid datetime");
101: }
1.40 ! paf 102: } else if(params->size()>=2) { // ^create(y;m;d[;h[;m[;s]]])
1.1 parser 103: tm tmIn={0};
104: tmIn.tm_isdst=-1;
1.28 paf 105: tmIn.tm_year=NN_year_to_NNNN(params->as_int(0, "year must be int", r));
1.9 parser 106: tmIn.tm_mon=params->as_int(1, "month must be int", r)-1;
1.24 paf 107: tmIn.tm_mday=params->size()>2?params->as_int(2, "mday must be int", r):1;
1.9 parser 108: if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r);
109: if(params->size()>4) tmIn.tm_min=params->as_int(4, "minutes must be int", r);
110: if(params->size()>5) tmIn.tm_sec=params->as_int(5, "seconds must be int", r);
1.23 paf 111: t=mktime(&tmIn);
112: if(t<0)
1.22 paf 113: throw Exception(0,
1.1 parser 114: &method_name,
115: "invalid datetime");
116: } else
1.22 paf 117: throw Exception("parser.runtime",
1.1 parser 118: &method_name,
1.24 paf 119: "invalid params count, must be 1 or >=2");
1.23 paf 120: vdate->set_time(t);
1.1 parser 121: }
122:
1.5 parser 123: static void _sql_string(Request& r, const String& method_name, MethodParams *) {
1.1 parser 124: Pool& pool=r.pool();
1.37 paf 125: VDate *vdate=static_cast<VDate *>(r.get_self());
1.2 parser 126: int size=1+ 4+1+2+1+2 +1+ 2+1+2+1+2 +1 +1;
1.1 parser 127: char *buf=(char *)pool.malloc(size);
128: time_t time=vdate->get_time();
1.14 paf 129: size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", localtime(&time));
1.1 parser 130:
1.4 parser 131: String& string=*new(pool) String(pool);
132: string.APPEND_CLEAN(buf, size,
133: method_name.origin().file,
134: method_name.origin().line);
1.25 paf 135: r.write_assign_lang(string);
1.1 parser 136: }
137:
138:
139: static void _roll(Request& r, const String& method_name, MethodParams *params) {
140: Pool& pool=r.pool();
1.37 paf 141: VDate *vdate=static_cast<VDate *>(r.get_self());
1.1 parser 142:
143: const String& what=params->as_string(0, "'what' must be string");
144: int oyear=0;
145: int omonth=0;
146: int oday=0;
147: int *offset;
148: if(what=="year") offset=&oyear;
149: else if(what=="month") offset=&omonth;
150: else if(what=="day") offset=&oday;
151: else
1.22 paf 152: throw Exception("parser.runtime",
1.1 parser 153: &what,
154: "must be year|month|day");
155:
1.9 parser 156: *offset=params->as_int(1, "offset must be int", r);
1.1 parser 157:
1.13 paf 158: time_t self_time=vdate->get_time();
159: tm tmIn=*localtime(&self_time);
160: tm tmSaved=tmIn;
161:
1.21 paf 162: tmIn.tm_year+=oyear;
163: tmIn.tm_mon+=omonth;
164: tmIn.tm_mday+=oday;
165: tmIn.tm_hour=24/2;
166: tmIn.tm_min=0;
167: tmIn.tm_sec=0;
168: time_t t=mktime/*normalizetime*/(&tmIn);
169: if(t<0)
1.22 paf 170: throw Exception(0,
1.21 paf 171: &method_name,
172: "bad resulting time (after roll)");
1.24 paf 173: if(oday==0 && tmIn.tm_mday!=tmSaved.tm_mday)
174: throw Exception(0,
175: &method_name,
176: "bad resulting time (day hole)", t);
1.21 paf 177:
1.13 paf 178: tm *tmOut=localtime(&t);
179: if(!tmOut)
1.22 paf 180: throw Exception(0,
1.1 parser 181: &method_name,
1.13 paf 182: "bad resulting time (seconds from epoch=%ld)", t);
183:
184: tmOut->tm_hour=tmSaved.tm_hour;
185: tmOut->tm_min=tmSaved.tm_min;
186: tmOut->tm_sec=tmSaved.tm_sec;
1.20 paf 187: tmOut->tm_isdst=-1;
1.13 paf 188: {
1.21 paf 189: time_t t=mktime/*normalizetime*/(tmOut);
190: if(
191: tmOut->tm_hour!=tmSaved.tm_hour
192: ||tmOut->tm_min!=tmSaved.tm_min)
1.22 paf 193: throw Exception(0,
1.21 paf 194: &method_name,
1.24 paf 195: "bad resulting time (hour hole)");
1.21 paf 196:
1.13 paf 197: if(t<0)
1.22 paf 198: throw Exception(0,
1.21 paf 199: &method_name,
200: "bad resulting time (after reconstruction)");
1.13 paf 201:
202: vdate->set_time(t);
203: }
1.1 parser 204: }
205:
1.10 parser 206: static Table *fill_month_days(Request& r,
207: const String& method_name, MethodParams *params, bool rus){
208: Pool& pool=r.pool();
1.34 paf 209: Table *result=new(pool) Table(pool, *date_calendar_table_template);
1.10 parser 210:
211: int year=params->as_int(1, "year must be int", r);
212: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
213:
214: tm tmIn={0, 0, 0, 1, month, year-1900};
215: time_t t=mktime(&tmIn);
216: if(t<0)
1.22 paf 217: throw Exception(0,
1.10 parser 218: &method_name,
219: "invalid date");
220: tm *tmOut=localtime(&t);
221:
222: int weekDay1=tmOut->tm_wday;
223: if(rus)
224: weekDay1=weekDay1?weekDay1-1:6; //sunday last
225: int monthDays=getMonthDays(year, month);
226:
227: for(int _day=1-weekDay1; _day<=monthDays;) {
228: Array& row=*new(pool) Array(pool, 7);
1.34 paf 229: // calculating year week no [1..54]
230: char *weekno_buf;
231: size_t weekno_size;
1.36 paf 232: int weekyear;
1.34 paf 233: // 0..6 week days-cells fill with month days
1.10 parser 234: for(int wday=0; wday<7; wday++, _day++) {
1.34 paf 235: String *cell=new(pool) String(pool);
1.10 parser 236: if(_day>=1 && _day<=monthDays) {
237: char *buf=(char *)pool.malloc(2+1);
238: cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day),
239: method_name.origin().file, method_name.origin().line);
240: }
241: row+=cell;
1.34 paf 242:
243: if(wday==(rus?3:4)/*thursday*/) {
244: tm tms={0,0,0, _day, month, year-1900};
245: /*normalize*/mktime(&tms);
246:
1.36 paf 247: weekyear=tms.tm_year+1900;
248:
1.35 paf 249: const int weekno_buf_size=2+1/*for stupid snprintfs*/ +1;
1.34 paf 250:
251: // http://www.merlyn.demon.co.uk/weekinfo.htm
252: 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 253: int n=1 + (tms.tm_yday-(FirstThurs[weekyear % 28]-3))/7;
1.34 paf 254: weekno_buf=(char *)pool.malloc(weekno_buf_size);
255: weekno_size=snprintf(weekno_buf, weekno_buf_size, "%02d", n);
256: }
257:
1.10 parser 258: }
1.34 paf 259: // appending year week no
260: {
261: String *cell=new(pool) String(pool);
262: cell->APPEND_CLEAN(weekno_buf, weekno_size,
1.36 paf 263: method_name.origin().file, method_name.origin().line);
264: row+=cell;
265: }
266: // appending year week year
267: {
268: String *cell=new(pool) String(pool);
269: char *buf=(char *)pool.malloc(4+1);
270: cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", weekyear),
1.34 paf 271: method_name.origin().file, method_name.origin().line);
272: row+=cell;
273: }
1.10 parser 274: *result+=&row;
275: }
276:
277: return result;
278: }
279:
280: static Table *fill_week_days(Request& r,
281: const String& method_name, MethodParams *params, bool rus){
282: Pool& pool=r.pool();
283: Array& columns=*new(pool) Array(pool, 4);
1.18 paf 284: columns+=new(pool) String(pool, "year");
285: columns+=new(pool) String(pool, "month");
286: columns+=new(pool) String(pool, "day");
1.19 paf 287: columns+=new(pool) String(pool, "weekday");
1.10 parser 288: Table *result=new(pool) Table(pool, &method_name, &columns);
289:
290: int year=params->as_int(1, "year must be int", r);
291: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
292: int day=params->as_int(3, "day must be int", r);
293:
294: tm tmIn={0, 0, 18, day, month, year-1900};
295: time_t t=mktime(&tmIn);
296: if(t<0)
1.22 paf 297: throw Exception(0,
1.10 parser 298: &method_name,
299: "invalid date");
300: tm *tmOut=localtime(&t);
301:
302: int baseWeekDay=tmOut->tm_wday;
303: if(rus)
304: baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
305:
306: t-=baseWeekDay*SECS_PER_DAY;
307:
308: for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
309: tm *tmOut=localtime(&t);
310: Array& row=*new(pool) Array(pool, 4);
311: #define WDFILL(size, value) { \
312: char *buf=(char *)pool.malloc(size+1); \
313: String *cell=new(pool) String(pool); \
314: cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
315: method_name.origin().file, \
316: method_name.origin().line); \
317: row+=cell; \
318: }
319: WDFILL(4, 1900+tmOut->tm_year);
320: WDFILL(2, 1+tmOut->tm_mon);
321: WDFILL(2, tmOut->tm_mday);
322: WDFILL(2, tmOut->tm_wday);
323: *result+=&row;
324: }
325:
326: return result;
327: }
328:
329: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
330: Pool& pool=r.pool();
331:
332: const String& what=params->as_string(0, "format must be strig");
333: bool rus=false;
334: if(what=="rus")
335: rus=true;
336: else if(what=="eng")
337: rus=false;
338: else
1.22 paf 339: throw Exception("parser.runtime",
1.10 parser 340: &what,
341: "must be rus|eng");
342:
343: Table *table;
344: if(params->size()==1+2)
345: table=fill_month_days(r, method_name, params, rus);
346: else // 1+3
347: table=fill_week_days(r, method_name, params, rus);
348:
349: VTable& result=*new(pool) VTable(pool, table);
350: r.write_no_lang(result);
351: }
352:
1.1 parser 353: // constructor
354:
1.27 paf 355: MDate::MDate(Pool& apool) : Methoded(apool, "date") {
1.1 parser 356: // ^now[]
1.23 paf 357: add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1 parser 358:
1.30 paf 359: // ^create(float days)
1.17 paf 360: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
1.28 paf 361: // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.17 paf 362: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1 parser 363:
1.6 parser 364: // ^sql-string[]
1.5 parser 365: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1 parser 366:
367: // ^roll(year|month|day;+/- 1)
368: add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10 parser 369:
370: // ^date:calendar[month|montheng;year;month] = table
371: // ^date:calendar[week|weekeng;year;month;day] = table
372: add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1 parser 373:
374: }
375: // global variable
376:
377: Methoded *date_class;
378:
379: // creator
380:
381: Methoded *MDate_create(Pool& pool) {
382: return date_class=new(pool) MDate(pool);
383: }
E-mail: