Annotation of parser3/src/classes/date.C, revision 1.18
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.18 ! paf 7: $Id: date.C,v 1.17 2002/02/18 12:13:42 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.11 parser 63: throw Exception(0, 0,
1.1 parser 64: &method_name,
65: "invalid datetime");
66: } else
1.11 parser 67: throw Exception(0, 0,
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.11 parser 103: throw Exception(0, 0,
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: if(!(*offset==1 || *offset==-1))
1.11 parser 109: throw Exception(0, 0,
1.1 parser 110: &method_name,
111: "offset must be +/- 1");
112:
1.13 paf 113: time_t self_time=vdate->get_time();
114: tm tmIn=*localtime(&self_time);
115: // we will preserve daytime from day-light-saving shifts across roll
116: tm tmSaved=tmIn;
117: tmIn.tm_hour=24/2; tmIn.tm_min=tmIn.tm_sec=0;
118:
119: tmIn.tm_year+=oyear;
120: time_t t=mktime(&tmIn);
121: t+=omonth*getMonthDays(tmIn.tm_year, (tmIn.tm_mon+(omonth<0?-1:0)+12)%12)*SECS_PER_DAY;
122: t+=oday*SECS_PER_DAY;
123:
124: tm *tmOut=localtime(&t);
125: if(!tmOut)
1.11 parser 126: throw Exception(0, 0,
1.1 parser 127: &method_name,
1.13 paf 128: "bad resulting time (seconds from epoch=%ld)", t);
129:
130: tmOut->tm_hour=tmSaved.tm_hour;
131: tmOut->tm_min=tmSaved.tm_min;
132: tmOut->tm_sec=tmSaved.tm_sec;
133: {
134: time_t t=mktime(tmOut);
135: if(t<0)
136: throw Exception(0, 0,
137: &method_name,
138: "bad resulting time (after reconstruction)");
139:
140: vdate->set_time(t);
141: }
1.1 parser 142: }
143:
1.10 parser 144: static Table *fill_month_days(Request& r,
145: const String& method_name, MethodParams *params, bool rus){
146: Pool& pool=r.pool();
147: Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/);
148:
149: int year=params->as_int(1, "year must be int", r);
150: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
151:
152: tm tmIn={0, 0, 0, 1, month, year-1900};
153: time_t t=mktime(&tmIn);
154: if(t<0)
1.11 parser 155: throw Exception(0, 0,
1.10 parser 156: &method_name,
157: "invalid date");
158: tm *tmOut=localtime(&t);
159:
160: int weekDay1=tmOut->tm_wday;
161: if(rus)
162: weekDay1=weekDay1?weekDay1-1:6; //sunday last
163: int monthDays=getMonthDays(year, month);
164:
165: for(int _day=1-weekDay1; _day<=monthDays;) {
166: Array& row=*new(pool) Array(pool, 7);
167: for(int wday=0; wday<7; wday++, _day++) {
168: String *cell=new(pool) String(pool);
169: if(_day>=1 && _day<=monthDays) {
170: char *buf=(char *)pool.malloc(2+1);
171: cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day),
172: method_name.origin().file, method_name.origin().line);
173: }
174: row+=cell;
175: }
176: *result+=&row;
177: }
178:
179: return result;
180: }
181:
182: static Table *fill_week_days(Request& r,
183: const String& method_name, MethodParams *params, bool rus){
184: Pool& pool=r.pool();
185: Array& columns=*new(pool) Array(pool, 4);
1.18 ! paf 186: columns+=new(pool) String(pool, "year");
! 187: columns+=new(pool) String(pool, "month");
! 188: columns+=new(pool) String(pool, "day");
! 189: columns+=new(pool) String(pool, "week_day");
1.10 parser 190: Table *result=new(pool) Table(pool, &method_name, &columns);
191:
192: int year=params->as_int(1, "year must be int", r);
193: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
194: int day=params->as_int(3, "day must be int", r);
195:
196: tm tmIn={0, 0, 18, day, month, year-1900};
197: time_t t=mktime(&tmIn);
198: if(t<0)
1.11 parser 199: throw Exception(0, 0,
1.10 parser 200: &method_name,
201: "invalid date");
202: tm *tmOut=localtime(&t);
203:
204: int baseWeekDay=tmOut->tm_wday;
205: if(rus)
206: baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
207:
208: t-=baseWeekDay*SECS_PER_DAY;
209:
210: for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
211: tm *tmOut=localtime(&t);
212: Array& row=*new(pool) Array(pool, 4);
213: #define WDFILL(size, value) { \
214: char *buf=(char *)pool.malloc(size+1); \
215: String *cell=new(pool) String(pool); \
216: cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
217: method_name.origin().file, \
218: method_name.origin().line); \
219: row+=cell; \
220: }
221: WDFILL(4, 1900+tmOut->tm_year);
222: WDFILL(2, 1+tmOut->tm_mon);
223: WDFILL(2, tmOut->tm_mday);
224: WDFILL(2, tmOut->tm_wday);
225: *result+=&row;
226: }
227:
228: return result;
229: }
230:
231: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
232: Pool& pool=r.pool();
233:
234: const String& what=params->as_string(0, "format must be strig");
235: bool rus=false;
236: if(what=="rus")
237: rus=true;
238: else if(what=="eng")
239: rus=false;
240: else
1.11 parser 241: throw Exception(0, 0,
1.10 parser 242: &what,
243: "must be rus|eng");
244:
245: Table *table;
246: if(params->size()==1+2)
247: table=fill_month_days(r, method_name, params, rus);
248: else // 1+3
249: table=fill_week_days(r, method_name, params, rus);
250:
251: VTable& result=*new(pool) VTable(pool, table);
252: result.set_name(method_name);
253: r.write_no_lang(result);
254: }
255:
1.1 parser 256: // constructor
257:
258: MDate::MDate(Pool& apool) : Methoded(apool) {
259: set_name(*NEW String(pool(), DATE_CLASS_NAME));
260:
261:
262: // ^now[]
263: add_native_method("now", Method::CT_DYNAMIC, _now, 0, 0);
264:
265: // ^set(float days)
1.17 paf 266: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
267: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1 parser 268:
1.6 parser 269: // ^sql-string[]
1.5 parser 270: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1 parser 271:
272: // ^roll(year|month|day;+/- 1)
273: add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10 parser 274:
275: // ^date:calendar[month|montheng;year;month] = table
276: // ^date:calendar[week|weekeng;year;month;day] = table
277: add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1 parser 278:
279: }
280: // global variable
281:
282: Methoded *date_class;
283:
284: // creator
285:
286: Methoded *MDate_create(Pool& pool) {
287: return date_class=new(pool) MDate(pool);
288: }
E-mail: