Diff for /parser3/src/classes/date.C between versions 1.46 and 1.56

version 1.46, 2003/07/24 11:31:19 version 1.56, 2003/11/20 17:09:35
Line 5 Line 5
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
 */  */
   
 static const char* IDENT_DATE_C="$Date$";  static const char * const IDENT_DATE_C="$Date$";
   
 #include "classes.h"  #include "classes.h"
 #include "pa_vmethod_frame.h"  #include "pa_vmethod_frame.h"
Line 19  static const char* IDENT_DATE_C="$Date$" Line 19  static const char* IDENT_DATE_C="$Date$"
   
 class MDate: public Methoded {  class MDate: public Methoded {
 public: // VStateless_class  public: // VStateless_class
         Value* create_new_value() { return new VDate(0); }          Value* create_new_value(Pool&) { return new VDate(0); }
   
 public:  public:
         MDate();          MDate();
Line 71  static int NN_year_to_NNNN(int year) { Line 71  static int NN_year_to_NNNN(int year) {
 // 2002-04-25 18:14:00  // 2002-04-25 18:14:00
 // 18:14:00  // 18:14:00
 // 2002:04:25 [+maybe time]  // 2002:04:25 [+maybe time]
 time_t cstr_to_time_t(char *cstr, bool fail_on_error) { // used in image.C  /*not static, used in image.C*/ time_t cstr_to_time_t(char *cstr, bool fail_on_error) {
           if( !cstr || !*cstr ) {
                   if(fail_on_error)
                           throw Exception(0,
                                   0,
                                   "empty string is not valid datetime");
                   return 0;
           }
         char *cur=cstr;          char *cur=cstr;
         int date_delim=isdigit(cur[0])&&isdigit(cur[1])&&isdigit(cur[2])&&isdigit(cur[3])&&cur[4]==':'?':'          int date_delim=isdigit(cur[0])&&isdigit(cur[1])&&isdigit(cur[2])&&isdigit(cur[3])&&cur[4]==':'?':'
                 :'-';                  :'-';
Line 84  time_t cstr_to_time_t(char *cstr, bool f Line 91  time_t cstr_to_time_t(char *cstr, bool f
         const char* min=lsplit(&cur, ':');          const char* min=lsplit(&cur, ':');
         const char* sec=cur;          const char* sec=cur;
   
         tm tmIn={0};          tm tmIn;  memset(&tmIn, 0, sizeof(tmIn));
         tmIn.tm_isdst=-1;          tmIn.tm_isdst=-1;
         if(!month)          if(!month)
                 if(min) {                  if(min) {
Line 131  static void _create(Request& r, MethodPa Line 138  static void _create(Request& r, MethodPa
                                         "invalid datetime");                                          "invalid datetime");
                 }                  }
         } else if(params.count()>=2) { // ^create(y;m;d[;h[;m[;s]]])          } else if(params.count()>=2) { // ^create(y;m;d[;h[;m[;s]]])
                 tm tmIn={0};                  tm tmIn; memset(&tmIn, 0, sizeof(tmIn));
                 tmIn.tm_isdst=-1;                  tmIn.tm_isdst=-1;
                 tmIn.tm_year=NN_year_to_NNNN(params.as_int(0, "year must be int", r));                  tmIn.tm_year=NN_year_to_NNNN(params.as_int(0, "year must be int", r));
                 tmIn.tm_mon=params.as_int(1, "month must be int", r)-1;                  tmIn.tm_mon=params.as_int(1, "month must be int", r)-1;
Line 161  static void _sql_string(Request& r, Meth Line 168  static void _sql_string(Request& r, Meth
         r.write_assign_lang(String(buf, size));          r.write_assign_lang(String(buf, size));
 }  }
   
   
 static void _roll(Request& r, MethodParams& params) {  static void _roll(Request& r, MethodParams& params) {
         VDate& vdate=GET_SELF(r, VDate);          VDate& vdate=GET_SELF(r, VDate);
   
Line 173  static void _roll(Request& r, MethodPara Line 179  static void _roll(Request& r, MethodPara
         if(what=="year") offset=&oyear;          if(what=="year") offset=&oyear;
         else if(what=="month") offset=&omonth;          else if(what=="month") offset=&omonth;
         else if(what=="day") offset=&oday;          else if(what=="day") offset=&oday;
         else          else if(what=="TZ") {
                   const String& argument_tz=params.as_string(1, "'TZ' must be string");
                   vdate.set_tz(&argument_tz);
                   return;
           } else
                 throw Exception("parser.runtime",                  throw Exception("parser.runtime",
                         &what,                          &what,
                         "must be year|month|day");                          "must be year|month|day|TZ");
                   
         *offset=params.as_int(1, "offset must be int", r);          *offset=params.as_int(1, "offset must be int", r);
   
Line 247  static Table& fill_month_days(Request& r Line 257  static Table& fill_month_days(Request& r
         int year=params.as_int(1, "year must be int", r);          int year=params.as_int(1, "year must be int", r);
         int month=max(1, min(params.as_int(2, "month must be int", r), 12)) -1;          int month=max(1, min(params.as_int(2, "month must be int", r), 12)) -1;
                   
         tm tmIn={0, 0, 0, 1, month, year-1900};          tm tmIn;  
           memset(&tmIn, 0, sizeof(tmIn)); 
           tmIn.tm_mday=1;
           tmIn.tm_mon=month; 
           tmIn.tm_yday=year-1900;
   
         time_t t=mktime(&tmIn);          time_t t=mktime(&tmIn);
         if(t<0)          if(t<0)
                 throw Exception(0,                   throw Exception(0, 
Line 263  static Table& fill_month_days(Request& r Line 278  static Table& fill_month_days(Request& r
         for(int _day=1-weekDay1; _day<=monthDays;) {          for(int _day=1-weekDay1; _day<=monthDays;) {
                 Table::element_type row(new ArrayString(7));                  Table::element_type row(new ArrayString(7));
                 // calculating year week no [1..54]                  // calculating year week no [1..54]
                 char *weekno_buf;                  char *weekno_buf=0; // surely would be assigned to, but to calm down compiler
                 size_t weekno_size;                  size_t weekno_size=0; // same
                 int weekyear;                  int weekyear=0; // same
                 // 0..6 week days-cells fill with month days                  // 0..6 week days-cells fill with month days
                 for(int wday=0; wday<7; wday++, _day++) {                  for(int wday=0; wday<7; wday++, _day++) {
                         String* cell=new String;                          String* cell=new String;
Line 316  static Table& fill_week_days(Request& r, Line 331  static Table& fill_week_days(Request& r,
         int month=max(1, min(params.as_int(2, "month must be int", r), 12)) -1;          int month=max(1, min(params.as_int(2, "month must be int", r), 12)) -1;
         int day=params.as_int(3, "day must be int", r);          int day=params.as_int(3, "day must be int", r);
                   
         tm tmIn={0, 0, 18, day, month, year-1900};          tm tmIn;
           memset(&tmIn, 0, sizeof(tmIn)); 
           tmIn.tm_hour=18;
           tmIn.tm_mday=day;
           tmIn.tm_mon=month; 
           tmIn.tm_yday=year-1900;
                   
         time_t t=mktime(&tmIn);          time_t t=mktime(&tmIn);
         if(t<0)          if(t<0)
                 throw Exception(0,                   throw Exception(0, 
Line 368  static void _calendar(Request& r, Method Line 389  static void _calendar(Request& r, Method
         r.write_no_lang(*new VTable(table));          r.write_no_lang(*new VTable(table));
 }  }
   
   static void _unix_timestamp(Request& r, MethodParams& params) {
           VDate& vdate=GET_SELF(r, VDate);
   
           if(params.count()==0) { 
                   // ^date.unix-timestamp[]
                   r.write_no_lang(*new VInt((int)vdate.get_time()));
           } else {
                   if(vdate.get_time())
                           throw Exception(0,
                                   0,
                                   "date object already constructed");
   
                   // ^unix-timestamp(time_t)
                   time_t t=(time_t)params.as_int(0, "Unix timestamp must be integer", r);
   
                   vdate.set_time(t);
           }
   }
   
   
 // constructor  // constructor
   
 MDate::MDate(): Methoded("date") {  MDate::MDate(): Methoded("date") {
Line 389  MDate::MDate(): Methoded("date") { Line 430  MDate::MDate(): Methoded("date") {
         // ^date:calendar[week|weekeng;year;month;day] = table          // ^date:calendar[week|weekeng;year;month;day] = table
         add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);          add_native_method("calendar", Method::CT_STATIC, _calendar, 3, 4);
   
   
           // ^unix-timestamp[]
           add_native_method("unix-timestamp", Method::CT_DYNAMIC, _unix_timestamp, 0, 1);
 }  }

Removed from v.1.46  
changed lines
  Added in v.1.56


E-mail: