Annotation of parser3/src/classes/date.C, revision 1.22
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.1 parser 6:
1.22 ! paf 7: $Id: date.C,v 1.21 2002/03/26 12:19:06 paf Exp $
1.1 parser 8: */
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: // defines
17:
18: #define DATE_CLASS_NAME "date"
19:
20: // class
21:
22: class MDate : public Methoded {
23: public: // VStateless_class
24: Value *create_new_value(Pool& pool) { return new(pool) VDate(pool, 0); }
25:
26: public:
27: MDate(Pool& pool);
28: public: // Methoded
29: bool used_directly() { return true; }
30: };
31:
32: // methods
33:
34: static void _now(Request& r, const String& method_name, MethodParams *) {
35: Pool& pool=r.pool();
36: VDate *vdate=static_cast<VDate *>(r.self);
37: vdate->set_time(time(0));
38: }
39:
1.17 paf 40: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.1 parser 41: Pool& pool=r.pool();
42: VDate *vdate=static_cast<VDate *>(r.self);
43:
44: time_t time;
45: if(params->size()==1) // ^set(float days)
1.9 parser 46: time=(time_t)(params->as_double(0, "float days must be double", r)*SECS_PER_DAY);
1.1 parser 47: else if(params->size()>=3) { // ^set(y;m;d[;h[;m[;s]]])
48: tm tmIn={0};
49: tmIn.tm_isdst=-1;
1.9 parser 50: int year=params->as_int(0, "year must be int", r);
1.1 parser 51: if(year<70) // 0..69 -> 100..169 [2000..2069]
52: year+=100;
1.3 parser 53: if(year>=1900)
54: year-=1900;
1.1 parser 55: tmIn.tm_year=year;
1.9 parser 56: tmIn.tm_mon=params->as_int(1, "month must be int", r)-1;
57: tmIn.tm_mday=params->as_int(2, "mday must be int", r);
58: if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r);
59: if(params->size()>4) tmIn.tm_min=params->as_int(4, "minutes must be int", r);
60: if(params->size()>5) tmIn.tm_sec=params->as_int(5, "seconds must be int", r);
1.1 parser 61: time=mktime(&tmIn);
62: if(time<0)
1.22 ! paf 63: throw Exception(0,
1.1 parser 64: &method_name,
65: "invalid datetime");
66: } else
1.22 ! paf 67: throw Exception("parser.runtime",
1.1 parser 68: &method_name,
69: "invalid params count, must be 1 or >=3");
70: vdate->set_time(time);
71: }
72:
1.5 parser 73: static void _sql_string(Request& r, const String& method_name, MethodParams *) {
1.1 parser 74: Pool& pool=r.pool();
75: VDate *vdate=static_cast<VDate *>(r.self);
1.2 parser 76: int size=1+ 4+1+2+1+2 +1+ 2+1+2+1+2 +1 +1;
1.1 parser 77: char *buf=(char *)pool.malloc(size);
78: time_t time=vdate->get_time();
1.14 paf 79: size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", localtime(&time));
1.1 parser 80:
1.4 parser 81: String& string=*new(pool) String(pool);
82: string.APPEND_CLEAN(buf, size,
83: method_name.origin().file,
84: method_name.origin().line);
85: Value& result=*new(pool) VString(string);
1.1 parser 86: r.write_assign_lang(result);
87: }
88:
89:
90: static void _roll(Request& r, const String& method_name, MethodParams *params) {
91: Pool& pool=r.pool();
92: VDate *vdate=static_cast<VDate *>(r.self);
93:
94: const String& what=params->as_string(0, "'what' must be string");
95: int oyear=0;
96: int omonth=0;
97: int oday=0;
98: int *offset;
99: if(what=="year") offset=&oyear;
100: else if(what=="month") offset=&omonth;
101: else if(what=="day") offset=&oday;
102: else
1.22 ! paf 103: throw Exception("parser.runtime",
1.1 parser 104: &what,
105: "must be year|month|day");
106:
1.9 parser 107: *offset=params->as_int(1, "offset must be int", r);
1.1 parser 108:
1.13 paf 109: time_t self_time=vdate->get_time();
110: tm tmIn=*localtime(&self_time);
111: tm tmSaved=tmIn;
112:
1.21 paf 113: tmIn.tm_year+=oyear;
114: tmIn.tm_mon+=omonth;
115: tmIn.tm_mday+=oday;
116: tmIn.tm_hour=24/2;
117: tmIn.tm_min=0;
118: tmIn.tm_sec=0;
119: time_t t=mktime/*normalizetime*/(&tmIn);
120: if(t<0)
1.22 ! paf 121: throw Exception(0,
1.21 paf 122: &method_name,
123: "bad resulting time (after roll)");
124:
1.13 paf 125: tm *tmOut=localtime(&t);
126: if(!tmOut)
1.22 ! paf 127: throw Exception(0,
1.1 parser 128: &method_name,
1.13 paf 129: "bad resulting time (seconds from epoch=%ld)", t);
130:
131: tmOut->tm_hour=tmSaved.tm_hour;
132: tmOut->tm_min=tmSaved.tm_min;
133: tmOut->tm_sec=tmSaved.tm_sec;
1.20 paf 134: tmOut->tm_isdst=-1;
1.13 paf 135: {
1.21 paf 136: time_t t=mktime/*normalizetime*/(tmOut);
137: if(
138: tmOut->tm_hour!=tmSaved.tm_hour
139: ||tmOut->tm_min!=tmSaved.tm_min)
1.22 ! paf 140: throw Exception(0,
1.21 paf 141: &method_name,
142: "bad resulting time (timeline hole)");
143:
1.13 paf 144: if(t<0)
1.22 ! paf 145: throw Exception(0,
1.21 paf 146: &method_name,
147: "bad resulting time (after reconstruction)");
1.13 paf 148:
149: vdate->set_time(t);
150: }
1.1 parser 151: }
152:
1.10 parser 153: static Table *fill_month_days(Request& r,
154: const String& method_name, MethodParams *params, bool rus){
155: Pool& pool=r.pool();
156: Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/);
157:
158: int year=params->as_int(1, "year must be int", r);
159: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
160:
161: tm tmIn={0, 0, 0, 1, month, year-1900};
162: time_t t=mktime(&tmIn);
163: if(t<0)
1.22 ! paf 164: throw Exception(0,
1.10 parser 165: &method_name,
166: "invalid date");
167: tm *tmOut=localtime(&t);
168:
169: int weekDay1=tmOut->tm_wday;
170: if(rus)
171: weekDay1=weekDay1?weekDay1-1:6; //sunday last
172: int monthDays=getMonthDays(year, month);
173:
174: for(int _day=1-weekDay1; _day<=monthDays;) {
175: Array& row=*new(pool) Array(pool, 7);
176: for(int wday=0; wday<7; wday++, _day++) {
177: String *cell=new(pool) String(pool);
178: if(_day>=1 && _day<=monthDays) {
179: char *buf=(char *)pool.malloc(2+1);
180: cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day),
181: method_name.origin().file, method_name.origin().line);
182: }
183: row+=cell;
184: }
185: *result+=&row;
186: }
187:
188: return result;
189: }
190:
191: static Table *fill_week_days(Request& r,
192: const String& method_name, MethodParams *params, bool rus){
193: Pool& pool=r.pool();
194: Array& columns=*new(pool) Array(pool, 4);
1.18 paf 195: columns+=new(pool) String(pool, "year");
196: columns+=new(pool) String(pool, "month");
197: columns+=new(pool) String(pool, "day");
1.19 paf 198: columns+=new(pool) String(pool, "weekday");
1.10 parser 199: Table *result=new(pool) Table(pool, &method_name, &columns);
200:
201: int year=params->as_int(1, "year must be int", r);
202: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
203: int day=params->as_int(3, "day must be int", r);
204:
205: tm tmIn={0, 0, 18, day, month, year-1900};
206: time_t t=mktime(&tmIn);
207: if(t<0)
1.22 ! paf 208: throw Exception(0,
1.10 parser 209: &method_name,
210: "invalid date");
211: tm *tmOut=localtime(&t);
212:
213: int baseWeekDay=tmOut->tm_wday;
214: if(rus)
215: baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
216:
217: t-=baseWeekDay*SECS_PER_DAY;
218:
219: for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
220: tm *tmOut=localtime(&t);
221: Array& row=*new(pool) Array(pool, 4);
222: #define WDFILL(size, value) { \
223: char *buf=(char *)pool.malloc(size+1); \
224: String *cell=new(pool) String(pool); \
225: cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
226: method_name.origin().file, \
227: method_name.origin().line); \
228: row+=cell; \
229: }
230: WDFILL(4, 1900+tmOut->tm_year);
231: WDFILL(2, 1+tmOut->tm_mon);
232: WDFILL(2, tmOut->tm_mday);
233: WDFILL(2, tmOut->tm_wday);
234: *result+=&row;
235: }
236:
237: return result;
238: }
239:
240: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
241: Pool& pool=r.pool();
242:
243: const String& what=params->as_string(0, "format must be strig");
244: bool rus=false;
245: if(what=="rus")
246: rus=true;
247: else if(what=="eng")
248: rus=false;
249: else
1.22 ! paf 250: throw Exception("parser.runtime",
1.10 parser 251: &what,
252: "must be rus|eng");
253:
254: Table *table;
255: if(params->size()==1+2)
256: table=fill_month_days(r, method_name, params, rus);
257: else // 1+3
258: table=fill_week_days(r, method_name, params, rus);
259:
260: VTable& result=*new(pool) VTable(pool, table);
261: result.set_name(method_name);
262: r.write_no_lang(result);
263: }
264:
1.1 parser 265: // constructor
266:
267: MDate::MDate(Pool& apool) : Methoded(apool) {
268: set_name(*NEW String(pool(), DATE_CLASS_NAME));
269:
270:
271: // ^now[]
272: add_native_method("now", Method::CT_DYNAMIC, _now, 0, 0);
273:
274: // ^set(float days)
1.17 paf 275: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
276: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1 parser 277:
1.6 parser 278: // ^sql-string[]
1.5 parser 279: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1 parser 280:
281: // ^roll(year|month|day;+/- 1)
282: add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10 parser 283:
284: // ^date:calendar[month|montheng;year;month] = table
285: // ^date:calendar[week|weekeng;year;month;day] = table
286: add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1 parser 287:
288: }
289: // global variable
290:
291: Methoded *date_class;
292:
293: // creator
294:
295: Methoded *MDate_create(Pool& pool) {
296: return date_class=new(pool) MDate(pool);
297: }
E-mail: