Annotation of parser3/src/classes/date.C, revision 1.10
1.1 parser 1: /** @file
2: Parser: @b date parser class.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.8 parser 5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1 parser 6:
1.10 ! parser 7: $Id: date.C,v 1.9 2001/10/08 16:42:06 parser 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:
40: static void _set(Request& r, const String& method_name, MethodParams *params) {
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)
63: PTHROW(0, 0,
64: &method_name,
65: "invalid datetime");
66: } else
67: PTHROW(0, 0,
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.5 parser 79: size=strftime(buf, size, "%Y-%m-%d %H:%M:%S", gmtime(&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
103: PTHROW(0, 0,
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))
109: PTHROW(0, 0,
110: &method_name,
111: "offset must be +/- 1");
112:
113: time_t tIn=vdate->get_time();
114: tm *tmIn=localtime(&tIn);
115: tmIn->tm_year+=oyear;
116: time_t t=mktime(tmIn);
117: if(t<0)
118: PTHROW(0, 0,
119: &method_name,
120: "invalid datetime");
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: vdate->set_time(t);
124: }
125:
1.10 ! parser 126: static Table *fill_month_days(Request& r,
! 127: const String& method_name, MethodParams *params, bool rus){
! 128: Pool& pool=r.pool();
! 129: Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/);
! 130:
! 131: int year=params->as_int(1, "year must be int", r);
! 132: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
! 133:
! 134: tm tmIn={0, 0, 0, 1, month, year-1900};
! 135: time_t t=mktime(&tmIn);
! 136: if(t<0)
! 137: PTHROW(0, 0,
! 138: &method_name,
! 139: "invalid date");
! 140: tm *tmOut=localtime(&t);
! 141:
! 142: int weekDay1=tmOut->tm_wday;
! 143: if(rus)
! 144: weekDay1=weekDay1?weekDay1-1:6; //sunday last
! 145: int monthDays=getMonthDays(year, month);
! 146:
! 147: for(int _day=1-weekDay1; _day<=monthDays;) {
! 148: Array& row=*new(pool) Array(pool, 7);
! 149: for(int wday=0; wday<7; wday++, _day++) {
! 150: String *cell=new(pool) String(pool);
! 151: if(_day>=1 && _day<=monthDays) {
! 152: char *buf=(char *)pool.malloc(2+1);
! 153: cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day),
! 154: method_name.origin().file, method_name.origin().line);
! 155: }
! 156: row+=cell;
! 157: }
! 158: *result+=&row;
! 159: }
! 160:
! 161: return result;
! 162: }
! 163:
! 164: static Table *fill_week_days(Request& r,
! 165: const String& method_name, MethodParams *params, bool rus){
! 166: Pool& pool=r.pool();
! 167: Array& columns=*new(pool) Array(pool, 4);
! 168: Table *result=new(pool) Table(pool, &method_name, &columns);
! 169:
! 170: int year=params->as_int(1, "year must be int", r);
! 171: int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1;
! 172: int day=params->as_int(3, "day must be int", r);
! 173:
! 174: tm tmIn={0, 0, 18, day, month, year-1900};
! 175: time_t t=mktime(&tmIn);
! 176: if(t<0)
! 177: PTHROW(0, 0,
! 178: &method_name,
! 179: "invalid date");
! 180: tm *tmOut=localtime(&t);
! 181:
! 182: int baseWeekDay=tmOut->tm_wday;
! 183: if(rus)
! 184: baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
! 185:
! 186: t-=baseWeekDay*SECS_PER_DAY;
! 187:
! 188: for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
! 189: tm *tmOut=localtime(&t);
! 190: Array& row=*new(pool) Array(pool, 4);
! 191: #define WDFILL(size, value) { \
! 192: char *buf=(char *)pool.malloc(size+1); \
! 193: String *cell=new(pool) String(pool); \
! 194: cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
! 195: method_name.origin().file, \
! 196: method_name.origin().line); \
! 197: row+=cell; \
! 198: }
! 199: WDFILL(4, 1900+tmOut->tm_year);
! 200: WDFILL(2, 1+tmOut->tm_mon);
! 201: WDFILL(2, tmOut->tm_mday);
! 202: WDFILL(2, tmOut->tm_wday);
! 203: *result+=&row;
! 204: }
! 205:
! 206: return result;
! 207: }
! 208:
! 209: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
! 210: Pool& pool=r.pool();
! 211:
! 212: const String& what=params->as_string(0, "format must be strig");
! 213: bool rus=false;
! 214: if(what=="rus")
! 215: rus=true;
! 216: else if(what=="eng")
! 217: rus=false;
! 218: else
! 219: PTHROW(0, 0,
! 220: &what,
! 221: "must be rus|eng");
! 222:
! 223: Table *table;
! 224: if(params->size()==1+2)
! 225: table=fill_month_days(r, method_name, params, rus);
! 226: else // 1+3
! 227: table=fill_week_days(r, method_name, params, rus);
! 228:
! 229: VTable& result=*new(pool) VTable(pool, table);
! 230: result.set_name(method_name);
! 231: r.write_no_lang(result);
! 232: }
! 233:
1.1 parser 234: // constructor
235:
236: MDate::MDate(Pool& apool) : Methoded(apool) {
237: set_name(*NEW String(pool(), DATE_CLASS_NAME));
238:
239:
240: // ^now[]
241: add_native_method("now", Method::CT_DYNAMIC, _now, 0, 0);
242:
243: // ^set(float days)
244: add_native_method("set", Method::CT_DYNAMIC, _set, 1, 6);
245:
1.6 parser 246: // ^sql-string[]
1.5 parser 247: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.1 parser 248:
249: // ^roll(year|month|day;+/- 1)
250: add_native_method("roll", Method::CT_DYNAMIC, _roll, 2, 2);
1.10 ! parser 251:
! 252: // ^date:calendar[month|montheng;year;month] = table
! 253: // ^date:calendar[week|weekeng;year;month;day] = table
! 254: add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1 parser 255:
256: }
257: // global variable
258:
259: Methoded *date_class;
260:
261: // creator
262:
263: Methoded *MDate_create(Pool& pool) {
264: return date_class=new(pool) MDate(pool);
265: }
E-mail: