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