Annotation of parser3/src/classes/date.C, revision 1.94
1.1 parser 1: /** @file
2: Parser: @b date parser class.
3:
1.91 moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.16 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.32 paf 6: */
1.1 parser 7:
8: #include "classes.h"
1.46 paf 9: #include "pa_vmethod_frame.h"
10:
1.1 parser 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:
1.94 ! moko 16: volatile const char * IDENT_DATE_C="$Id: date.C,v 1.93 2015/04/08 18:08:52 moko Exp $" IDENT_PA_VDATE_H;
1.91 moko 17:
1.1 parser 18: // class
19:
1.46 paf 20: class MDate: public Methoded {
1.1 parser 21: public: // VStateless_class
1.87 misha 22: Value* create_new_value(Pool&) { return new VDate(0); }
1.1 parser 23:
24: public:
1.46 paf 25: MDate();
1.1 parser 26: };
27:
1.46 paf 28: // global variable
29:
30: DECLARE_CLASS_VAR(date, new MDate, 0);
31:
32: // helpers
33:
34: class Date_calendar_table_template_columns: public ArrayString {
35: public:
36: Date_calendar_table_template_columns(): ArrayString(6+2) {
1.85 misha 37: for(int i=0; i<=6; i++)
38: *this+=new String(i, "%d"); // .i column name
39:
1.46 paf 40: *this+=new String("week");
41: *this+=new String("year");
42: }
43: };
44:
45:
46: Table date_calendar_table_template(new Date_calendar_table_template_columns);
47:
1.1 parser 48: // methods
49:
1.46 paf 50: static void _now(Request& r, MethodParams& params) {
51: VDate& vdate=GET_SELF(r, VDate);
1.23 paf 52:
53: time_t t=time(0);
1.46 paf 54: if(params.count()==1) // ^now(offset)
55: t+=(time_t)round(params.as_double(0, "offset must be double", r)*SECS_PER_DAY);
1.23 paf 56:
1.46 paf 57: vdate.set_time(t);
1.1 parser 58: }
59:
1.90 misha 60: static void _today(Request& r, MethodParams&) {
61: VDate& vdate=GET_SELF(r, VDate);
62:
63: time_t t=time(0);
64:
65: tm today=*localtime(&t);
66: today.tm_hour=0;
67: today.tm_min=0;
68: today.tm_sec=0;
69:
1.94 ! moko 70: vdate.set_tm(today);
1.90 misha 71: }
72:
1.78 misha 73: static int to_year(int iyear) {
1.94 ! moko 74: if(iyear<0 || iyear>9999)
! 75: throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "year '%d' is out of range 0..9999", iyear);
! 76: return iyear-1900;
1.78 misha 77: }
78:
79: static int to_month(int imonth) {
80: return max(1, min(imonth, 12)) -1;
1.59 paf 81: }
82:
1.41 paf 83: // 2002-04-25 18:14:00
84: // 18:14:00
85: // 2002:04:25 [+maybe time]
1.65 paf 86: /*not static, used in image.C*/ tm cstr_to_time_t(char *cstr) {
1.64 paf 87: if( !cstr || !*cstr )
1.94 ! moko 88: throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "empty string is not valid datetime");
1.64 paf 89:
1.41 paf 90: char *cur=cstr;
1.73 paf 91: char date_delim=isdigit((unsigned char)cur[0])&&isdigit((unsigned char)cur[1])&&isdigit((unsigned char)cur[2])&&isdigit((unsigned char)cur[3])&&cur[4]==':'?':'
1.41 paf 92: :'-';
1.46 paf 93: const char* year=lsplit(&cur, date_delim);
94: const char* month=lsplit(&cur, date_delim);
95: const char* mday=lsplit(&cur, ' ');
1.41 paf 96: if(!month)
97: cur=cstr;
1.46 paf 98: const char* hour=lsplit(&cur, ':');
99: const char* min=lsplit(&cur, ':');
1.66 paf 100: const char* sec=lsplit(&cur, '.');
101: const char* msec=cur;
1.41 paf 102:
1.94 ! moko 103: tm tmIn;
! 104: memset(&tmIn, 0, sizeof(tmIn));
1.41 paf 105: tmIn.tm_isdst=-1;
1.93 moko 106: if(!month) {
1.41 paf 107: if(min) {
108: year=mday=0; // HH:MM
109: time_t t=time(0);
110: tm *tmNow=localtime(&t);
111: tmIn.tm_year=tmNow->tm_year;
112: tmIn.tm_mon=tmNow->tm_mon;
113: tmIn.tm_mday=tmNow->tm_mday;
114: goto date_part_set;
115: } else
1.93 moko 116: hour=min=sec=msec=0; // not YYYY- & not HH: = just YYYY
117: }
1.94 ! moko 118: tmIn.tm_year=to_year(pa_atoi(year));
1.60 paf 119: tmIn.tm_mon=month?pa_atoi(month)-1:0;
120: tmIn.tm_mday=mday?pa_atoi(mday):1;
1.41 paf 121: date_part_set:
1.65 paf 122: tmIn.tm_hour=hour?pa_atoi(hour):0;
1.60 paf 123: tmIn.tm_min=min?pa_atoi(min):0;
1.64 paf 124: tmIn.tm_sec=sec?pa_atoi(sec):0;
1.66 paf 125: //tmIn.tm_[msec<<no such, waits reimplementation of the class]=f(msec);
1.65 paf 126: return tmIn;
1.41 paf 127: }
128:
1.46 paf 129: static void _create(Request& r, MethodParams& params) {
130: VDate& vdate=GET_SELF(r, VDate);
1.1 parser 131:
1.82 misha 132: if(params.count()==1){
1.92 moko 133: if(params[0].is_string()){ // ^create[2002-04-25 18:14:00] ^create[18:14:00]
1.94 ! moko 134: tm tmIn=cstr_to_time_t(params[0].get_string()->cstrm());
! 135: vdate.set_tm(tmIn);
1.82 misha 136: } else { // ^create(float days) or ^create[date object]
1.94 ! moko 137: vdate.set_time(round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY));
1.28 paf 138: }
1.74 misha 139: } else { // ^create(y;m;d[;h[;m[;s]]])
1.75 misha 140: assert(params.count()<=6);
1.56 paf 141: tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
1.1 parser 142: tmIn.tm_isdst=-1;
1.94 ! moko 143: tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));
1.46 paf 144: tmIn.tm_mon=params.as_int(1, "month must be int", r)-1;
145: tmIn.tm_mday=params.count()>2?params.as_int(2, "mday must be int", r):1;
1.64 paf 146: int savedHour=0;
147: if(params.count()>3) savedHour=tmIn.tm_hour=params.as_int(3, "hour must be int", r);
1.46 paf 148: if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r);
149: if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r);
1.94 ! moko 150: vdate.set_tm(tmIn);
1.74 misha 151: };
1.1 parser 152: }
153:
1.90 misha 154: static void _sql_string(Request& r, MethodParams& params) {
1.46 paf 155: VDate& vdate=GET_SELF(r, VDate);
1.85 misha 156:
1.90 misha 157: VDate::sql_string_type format = VDate::sql_string_datetime;
158: if(params.count() > 0) {
159: const String& what=params.as_string(0, "'type' must be string");
160: if(what.is_empty() || what == "datetime")
161: format = VDate::sql_string_datetime;
162: else if(what == "date")
163: format=VDate::sql_string_date;
164: else if(what == "time")
165: format=VDate::sql_string_time;
166: else
167: throw Exception(PARSER_RUNTIME, &what, "'type' must be 'date', 'time' or 'datetime'");
168: }
169:
170: r.write_assign_lang(*vdate.get_sql_string(format));
1.1 parser 171: }
172:
1.80 misha 173: static void _gmt_string(Request& r, MethodParams&) {
174: VDate& vdate=GET_SELF(r, VDate);
175:
1.88 misha 176: r.write_assign_lang(*vdate.get_gmt_string());
1.80 misha 177: }
178:
1.46 paf 179: static void _roll(Request& r, MethodParams& params) {
180: const String& what=params.as_string(0, "'what' must be string");
1.43 paf 181: int oyear=0;
182: int omonth=0;
183: int oday=0;
1.1 parser 184: int *offset;
185: if(what=="year") offset=&oyear;
186: else if(what=="month") offset=&omonth;
187: else if(what=="day") offset=&oday;
1.48 paf 188: else if(what=="TZ") {
189: const String& argument_tz=params.as_string(1, "'TZ' must be string");
1.94 ! moko 190: (&r.get_self() == date_class) ? VDate::set_default_tz(&argument_tz) : GET_SELF(r, VDate).set_tz(&argument_tz);
1.48 paf 191: return;
192: } else
1.94 ! moko 193: throw Exception(PARSER_RUNTIME, &what, "must be year|month|day|TZ");
! 194:
! 195: if(&r.get_self() == date_class)
! 196: throw Exception(PARSER_RUNTIME, &what, "must be TZ to be called statically");
! 197:
! 198: VDate& vdate=GET_SELF(r, VDate);
1.1 parser 199:
1.46 paf 200: *offset=params.as_int(1, "offset must be int", r);
1.1 parser 201:
1.94 ! moko 202: tm tmIn=vdate.get_tm();
1.13 paf 203: tm tmSaved=tmIn;
1.43 paf 204: int adjust_day=0;
205: while(true) {
206: tmIn.tm_year+=oyear;
207: tmIn.tm_mon+=omonth;
208: tmIn.tm_mday+=oday+adjust_day;
1.94 ! moko 209: tmIn.tm_hour=24/2;
1.43 paf 210: tmIn.tm_min=0;
211: tmIn.tm_sec=0;
1.94 ! moko 212: int saved_day=tmIn.tm_mday;
! 213: vdate.set_tm(tmIn); /* normalize */
! 214:
! 215: if(oday==0 && tmIn.tm_mday!=saved_day /* but it changed */ ) {
! 216: if(adjust_day <= -3 /* 31->28 max, so never, but... */ )
! 217: throw Exception(DATE_RANGE_EXCEPTION_TYPE, 0, "bad resulting time (day hole still with %d day adjustment)", adjust_day );
1.43 paf 218:
219: tmIn=tmSaved; // restoring
220: --adjust_day; //retrying with prev day
221: } else
1.94 ! moko 222: break;
1.43 paf 223: }
1.13 paf 224:
1.94 ! moko 225: tmIn.tm_hour=tmSaved.tm_hour;
! 226: tmIn.tm_min=tmSaved.tm_min;
! 227: tmIn.tm_sec=tmSaved.tm_sec;
! 228: tmIn.tm_isdst=-1;
! 229:
! 230: vdate.set_tm(tmIn);
1.1 parser 231: }
232:
1.46 paf 233: static Table& fill_month_days(Request& r, MethodParams& params, bool rus){
1.45 paf 234: Table::Action_options table_options;
1.46 paf 235: Table& result=*new Table(date_calendar_table_template, table_options);
236:
1.94 ! moko 237: tm tmIn;
! 238: memset(&tmIn, 0, sizeof(tmIn));
! 239: tmIn.tm_year=to_year(params.as_int(1, "year must be int", r));
! 240: tmIn.tm_mon=to_month(params.as_int(2, "month must be int", r));
1.55 paf 241: tmIn.tm_mday=1;
242:
1.94 ! moko 243: VDate t(tmIn); /* normalize */
! 244: int weekDay1=tmIn.tm_wday;
! 245:
! 246: if(rus)
1.10 parser 247: weekDay1=weekDay1?weekDay1-1:6; //sunday last
1.94 ! moko 248: int monthDays=VDate::getMonthDays(tmIn.tm_year, tmIn.tm_mon);
1.43 paf 249:
250: for(int _day=1-weekDay1; _day<=monthDays;) {
1.46 paf 251: Table::element_type row(new ArrayString(7));
1.34 paf 252: // calculating year week no [1..54]
1.85 misha 253: int weekyear=0; // surely would be assigned to, but to calm down compiler
254: int weekno=0; // same
1.34 paf 255: // 0..6 week days-cells fill with month days
1.43 paf 256: for(int wday=0; wday<7; wday++, _day++) {
1.85 misha 257: *row+=(_day>=1 && _day<=monthDays)?new String(_day, "%02d"):new String();
1.34 paf 258:
259: if(wday==(rus?3:4)/*thursday*/) {
1.57 paf 260: tm tms;
1.94 ! moko 261: memset(&tms, 0, sizeof(tms));
1.57 paf 262: tms.tm_mday=_day;
1.94 ! moko 263: tms.tm_mon=tmIn.tm_mon;
! 264: tms.tm_year=tmIn.tm_year;
1.57 paf 265:
1.94 ! moko 266: VDate ts(tms); /*normalize*/
1.36 paf 267: weekyear=tms.tm_year+1900;
1.85 misha 268: weekno=VDate::CalcWeek(tms).week;
269: }
270: }
271: // appending week no
272: *row+=new String(weekno, "%02d");
1.36 paf 273:
1.85 misha 274: // appending week year
275: *row+=new String(weekyear, "%04d");
1.34 paf 276:
1.46 paf 277: result+=row;
278: }
279:
280: return result;
1.10 parser 281: }
282:
1.46 paf 283: static Table& fill_week_days(Request& r, MethodParams& params, bool rus){
284: Table::columns_type columns(new ArrayString(4));
285: *columns+=new String("year");
286: *columns+=new String("month");
287: *columns+=new String("day");
288: *columns+=new String("weekday");
289: Table& result=*new Table(columns);
290:
1.55 paf 291: tm tmIn;
1.94 ! moko 292: memset(&tmIn, 0, sizeof(tmIn));
! 293: tmIn.tm_year=to_year(params.as_int(1, "year must be int", r));
! 294: tmIn.tm_mon=to_month(params.as_int(2, "month must be int", r));
! 295: tmIn.tm_mday=params.as_int(3, "day must be int", r);
1.55 paf 296: tmIn.tm_hour=18;
1.84 misha 297:
1.94 ! moko 298: VDate t(tmIn); /* normalize */
! 299: int baseWeekDay=tmIn.tm_wday;
! 300:
1.10 parser 301: if(rus)
302: baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
1.46 paf 303:
1.94 ! moko 304: t.set_time(t.get_time()-baseWeekDay*SECS_PER_DAY);
1.46 paf 305:
1.94 ! moko 306: for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t.set_time(t.get_time()+SECS_PER_DAY)) {
1.46 paf 307: Table::element_type row(new ArrayString(4));
1.94 ! moko 308:
! 309: tm tmOut=t.get_tm();
! 310: *row+=new String(1900+tmOut.tm_year, "%04d");
! 311: *row+=new String(1+tmOut.tm_mon, "%02d");
! 312: *row+=new String(tmOut.tm_mday, "%02d");
! 313: *row+=new String(tmOut.tm_wday, "%02d");
1.85 misha 314:
1.46 paf 315: result+=row;
316: }
317:
318: return result;
1.10 parser 319: }
320:
1.46 paf 321: static void _calendar(Request& r, MethodParams& params) {
322: const String& what=params.as_string(0, "format must be strig");
1.10 parser 323: bool rus=false;
324: if(what=="rus")
325: rus=true;
326: else if(what=="eng")
327: rus=false;
328: else
1.94 ! moko 329: throw Exception(PARSER_RUNTIME, &what, "must be rus|eng");
1.10 parser 330:
1.46 paf 331: Table* table;
332: if(params.count()==1+2)
333: table=&fill_month_days(r, params, rus);
1.10 parser 334: else // 1+3
1.46 paf 335: table=&fill_week_days(r, params, rus);
1.10 parser 336:
1.46 paf 337: r.write_no_lang(*new VTable(table));
1.10 parser 338: }
339:
1.49 paf 340: static void _unix_timestamp(Request& r, MethodParams& params) {
341: VDate& vdate=GET_SELF(r, VDate);
342:
343: if(params.count()==0) {
344: // ^date.unix-timestamp[]
1.94 ! moko 345: r.write_no_lang(*new VDouble((double)vdate.get_time()));
1.49 paf 346: } else {
1.50 paf 347: if(vdate.get_time())
1.94 ! moko 348: throw Exception(PARSER_RUNTIME, 0, "date object already constructed");
1.50 paf 349: // ^unix-timestamp(time_t)
1.94 ! moko 350: vdate.set_time(params.as_double(0, "Unix timestamp must be number", r));
1.49 paf 351: }
352: }
353:
1.79 misha 354: static void _last_day(Request& r, MethodParams& params) {
1.94 ! moko 355: tm tmIn;
1.78 misha 356: if(&r.get_self() == date_class) {
1.81 misha 357: if(params.count() != 2)
1.94 ! moko 358: throw Exception(PARSER_RUNTIME, 0, "year and month must be defined");
1.81 misha 359: // ^date:lastday(year;month)
1.94 ! moko 360: tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));
! 361: tmIn.tm_mon=to_month(params.as_int(1, "month must be int", r));
1.78 misha 362: } else {
1.94 ! moko 363: if(params.count() != 0)
! 364: throw Exception(PARSER_RUNTIME, 0, "year and month must not be defined");
1.78 misha 365: // ^date.lastday[]
1.94 ! moko 366: tmIn=GET_SELF(r, VDate).get_tm();
1.78 misha 367: }
1.94 ! moko 368: r.write_no_lang(*new VInt(VDate::getMonthDays(tmIn.tm_year, tmIn.tm_mon)));
1.78 misha 369: }
370:
1.1 parser 371: // constructor
372:
1.46 paf 373: MDate::MDate(): Methoded("date") {
1.78 misha 374: // ^date::now[]
1.90 misha 375: // ^date::now(offset float days)
1.23 paf 376: add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
1.1 parser 377:
1.90 misha 378: // ^date::today[]
379: add_native_method("today", Method::CT_DYNAMIC, _today, 0, 0);
380:
1.78 misha 381: // ^date::create(float days)
1.90 misha 382: // ^date::create[date]
383: // ^date::create(year;month;day[;hour[;minute[;sec]]])
384: // ^date::create[yyyy-mm-dd[ hh:mm:ss]]
385: // ^date::create[hh:mm:ss]
1.17 paf 386: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
1.28 paf 387: // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
1.17 paf 388: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);
1.1 parser 389:
1.78 misha 390: // ^date.sql-string[]
1.90 misha 391: add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 1);
1.1 parser 392:
1.80 misha 393: // ^date.gmt-string[]
394: add_native_method("gmt-string", Method::CT_DYNAMIC, _gmt_string, 0, 0);
395:
1.78 misha 396: // ^date:lastday(year;month)
397: // ^date.lastday[]
1.79 misha 398: add_native_method("last-day", Method::CT_ANY, _last_day, 0, 2);
1.78 misha 399:
1.90 misha 400: // ^date.roll[year|month|day](+/- 1)
1.94 ! moko 401: add_native_method("roll", Method::CT_ANY, _roll, 2, 2);
1.10 parser 402:
1.90 misha 403: // ^date:calendar[rus|eng](year;month) = table
404: // ^date:calendar[rus|eng](year;month;day) = table
1.10 parser 405: add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
1.1 parser 406:
1.78 misha 407: // ^date.unix-timestamp[]
1.90 misha 408: // ^date::unix-timestamp(timestamp)
1.49 paf 409: add_native_method("unix-timestamp", Method::CT_DYNAMIC, _unix_timestamp, 0, 1);
1.1 parser 410: }
E-mail: