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