Diff for /parser3/src/classes/date.C between versions 1.97 and 1.103

version 1.97, 2015/09/22 13:53:04 version 1.103, 2016/03/31 21:46:19
Line 1 Line 1
 /** @file  /** @file
         Parser: @b date parser class.          Parser: @b date parser class.
   
         Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)          Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
 */  */
   
Line 27  public: Line 27  public:
   
 // global variable  // global variable
   
 DECLARE_CLASS_VAR(date, new MDate, 0);  DECLARE_CLASS_VAR(date, new MDate);
   
 // helpers  // helpers
   
Line 50  Table date_calendar_table_template(new D Line 50  Table date_calendar_table_template(new D
 static void _now(Request& r, MethodParams& params) {  static void _now(Request& r, MethodParams& params) {
         VDate& vdate=GET_SELF(r, VDate);          VDate& vdate=GET_SELF(r, VDate);
   
         time_t t=time(0);          pa_time_t t=(pa_time_t)time(0);
         if(params.count()==1) // ^now(offset)          if(params.count()==1) // ^now(offset)
                 t+=(time_t)round(params.as_double(0, "offset must be double", r)*SECS_PER_DAY);                  t+=(pa_time_t)round(params.as_double(0, "offset must be double", r)*SECS_PER_DAY);
                   
         vdate.set_time(t);          vdate.set_time(t);
 }  }
Line 138  static char *skip_writespace(char* str) Line 138  static char *skip_writespace(char* str)
 }  }
   
 static char *numeric_tz(char prefix, char* tz) {  static char *numeric_tz(char prefix, char* tz) {
         char *cur=tz;          // preparing POSIX TZ format
           char *buf=new(PointerFreeGC) char[4+5+1/*zero-teminator*/];
           strcpy(buf, prefix=='+' ? "SUB-":"SUB+");
           char *cur=buf+4;
   
         // hours          // hours
         if(!isdigit(*(cur++)))          if(!isdigit(*(cur++)=*(tz++)))
                 return 0;                  return 0;
         if(isdigit(cur[0]))          if(isdigit(tz[0]))
                 cur++;                  *(cur++)=*(tz++);
         if(cur[0] == ':'){  
                 // optional minutes          if(tz[0] == ':'){
                 cur++;                  // HH:mm format
                 if(!isdigit(*(cur++)))                  *(cur++)=*(tz++);
                   if(!isdigit(*(cur++)=*(tz++)))
                           return 0;
                   if(isdigit(tz[0]))
                           *(cur++)=*(tz++);
           } else if(isdigit(tz[0])){
                   // HHmm format
                   *(cur++)=':';
                   if(!isdigit(*(cur++)=*(tz++)) || !isdigit(*(cur++)=*(tz++)))
                         return 0;                          return 0;
                 if(isdigit(cur[0]))  
                         cur++;  
         }          }
         // nothing more          // nothing more
         if(skip_writespace(cur))          if(skip_writespace(tz))
                 return 0;                  return 0;
         // returning POSIX TZ format          *cur=0;
         size_t size=4+(cur-tz)+1/*zero-teminator*/;  
         char *buf=new(PointerFreeGC) char[size];  
         strcpy(buf, prefix=='+' ? "SUB-":"SUB+");  
         strncpy(buf+4, tz, cur-tz);  
         buf[size-1]=0;  
         return buf;          return buf;
 }  }
   
Line 249  static void _create(Request& r, MethodPa Line 254  static void _create(Request& r, MethodPa
                                 vdate.set_tz(tz);                                  vdate.set_tz(tz);
                         vdate.set_tm(tmIn);                          vdate.set_tm(tmIn);
                 } else { // ^create(float days) or ^create[date object]                  } else { // ^create(float days) or ^create[date object]
                           if(Value* adate=params[0].as(VDATE_TYPE))
                                   vdate.set_tz(static_cast<VDate*>(adate)->get_tz());
                         vdate.set_time(round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY));                          vdate.set_time(round(params.as_double(0, "float days must be double", r)*SECS_PER_DAY));
                 }                  }
         } else { // ^create(y;m;d[;h[;m[;s]]])          } else { // ^create(y;m;d[;h[;m[;s[;TZ]]]])
                 assert(params.count()<=6);  
                 tm tmIn; memset(&tmIn, 0, sizeof(tmIn));                  tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
                 tmIn.tm_isdst=-1;                  tmIn.tm_isdst=-1;
                 tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));                  tmIn.tm_year=to_year(params.as_int(0, "year must be int", r));
Line 261  static void _create(Request& r, MethodPa Line 267  static void _create(Request& r, MethodPa
                 if(params.count()>3) tmIn.tm_hour=params.as_int(3, "hour must be int", r);                  if(params.count()>3) tmIn.tm_hour=params.as_int(3, "hour must be int", r);
                 if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r);                  if(params.count()>4) tmIn.tm_min=params.as_int(4, "minutes must be int", r);
                 if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r);                  if(params.count()>5) tmIn.tm_sec=params.as_int(5, "seconds must be int", r);
                   if(params.count()>6) vdate.set_tz(params.as_string(6, "TZ must be string").cstr());
                 vdate.set_tm(tmIn);                  vdate.set_tm(tmIn);
         };          };
 }  }
Line 290  static void _gmt_string(Request& r, Meth Line 297  static void _gmt_string(Request& r, Meth
         r.write_assign_lang(*vdate.get_gmt_string());          r.write_assign_lang(*vdate.get_gmt_string());
 }  }
   
 static void _iso_string(Request& r, MethodParams&) {  static void _iso_string(Request& r, MethodParams& params) {
         VDate& vdate=GET_SELF(r, VDate);          VDate& vdate=GET_SELF(r, VDate);
   
         r.write_assign_lang(*vdate.get_iso_string());          VDate::iso_string_type format=VDate::iso_string_default;
   
           if(params.count()>0)
                   if(HashStringValue* options=params.as_hash(0)){
                           int valid_options=0;
                           if(Value* vshow_ms=options->get("ms")){
                                   if(r.process_to_value(*vshow_ms).as_bool())
                                           format=VDate::iso_string_type(format|VDate::iso_string_ms);
                                   valid_options++;
                           }
                           if(Value* vshow_colon=options->get("colon")){
                                   if(!r.process_to_value(*vshow_colon).as_bool())
                                           format=VDate::iso_string_type(format|VDate::iso_string_no_colon);
                                   valid_options++;
                           }
                           if(Value* vshow_z=options->get("z")){
                                   if(!r.process_to_value(*vshow_z).as_bool())
                                           format=VDate::iso_string_type(format|VDate::iso_string_no_z);
                                   valid_options++;
                           }
                           if(valid_options != options->count())
                                   throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                   }
   
           r.write_assign_lang(*vdate.get_iso_string(format));
 }  }
   
 static void _roll(Request& r, MethodParams& params) {  static void _roll(Request& r, MethodParams& params) {
Line 506  MDate::MDate(): Methoded("date") { Line 537  MDate::MDate(): Methoded("date") {
   
         // ^date::create(float days)          // ^date::create(float days)
         // ^date::create[date]          // ^date::create[date]
         // ^date::create(year;month;day[;hour[;minute[;sec]]])          // ^date::create(year;month;day[;hour[;minute[;sec[;TZ]]]])
         // ^date::create[yyyy-mm-dd[ hh:mm:ss]]          // ^date::create[yyyy-mm-dd[ hh:mm:ss]]
         // ^date::create[hh:mm:ss]          // ^date::create[hh:mm:ss]
         add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);          add_native_method("create", Method::CT_DYNAMIC, _create, 1, 7);
         // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf          // old name for compatibility with <= v1.17 2002/2/18 12:13:42 paf
         add_native_method("set", Method::CT_DYNAMIC, _create, 1, 6);          add_native_method("set", Method::CT_DYNAMIC, _create, 1, 7);
   
         // ^date.sql-string[]          // ^date.sql-string[]
         add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 1);          add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 1);
Line 519  MDate::MDate(): Methoded("date") { Line 550  MDate::MDate(): Methoded("date") {
         // ^date.gmt-string[]          // ^date.gmt-string[]
         add_native_method("gmt-string", Method::CT_DYNAMIC, _gmt_string, 0, 0);          add_native_method("gmt-string", Method::CT_DYNAMIC, _gmt_string, 0, 0);
   
         // ^date.iso-string[]          // ^date.iso-string[$.colon(true) $.z(true) $.ms(false)]
         add_native_method("iso-string", Method::CT_DYNAMIC, _iso_string, 0, 0);          add_native_method("iso-string", Method::CT_DYNAMIC, _iso_string, 0, 1);
   
         // ^date:lastday(year;month)          // ^date:lastday(year;month)
         // ^date.lastday[]          // ^date.lastday[]

Removed from v.1.97  
changed lines
  Added in v.1.103


E-mail: