Diff for /parser3/src/classes/date.C between versions 1.26 and 1.32

version 1.26, 2002/04/16 08:59:25 version 1.32, 2002/08/01 11:26:43
Line 3 Line 3
   
         Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
   
         $Id$  
 */  */
   
   static const char* IDENT_DATE_C="$Id$";
   
 #include "classes.h"  #include "classes.h"
 #include "pa_request.h"  #include "pa_request.h"
 #include "pa_vdouble.h"  #include "pa_vdouble.h"
 #include "pa_vdate.h"  #include "pa_vdate.h"
 #include "pa_vtable.h"  #include "pa_vtable.h"
   
 // defines  
   
 #define DATE_CLASS_NAME "date"  
   
 // class  // class
   
 class MDate : public Methoded {  class MDate : public Methoded {
Line 42  static void _now(Request& r, const Strin Line 38  static void _now(Request& r, const Strin
         vdate->set_time(t);          vdate->set_time(t);
 }  }
   
   static int NN_year_to_NNNN(int year) {
           if(year<70) // 0..69 -> 100..169 [2000..2069]
                   year+=100;
           if(year>=1900)
                   year-=1900;
           return year;
   }
   
   /// @test 09 ok? [octal maybe]
 static void _create(Request& r, const String& method_name, MethodParams *params) {  static void _create(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VDate *vdate=static_cast<VDate *>(r.self);          VDate *vdate=static_cast<VDate *>(r.self);
   
         time_t t;          time_t t;
         if(params->size()==1) { // ^set(float days)          if(params->size()==1) { 
                 t=(time_t)(params->as_double(0, "float days must be double", r)*SECS_PER_DAY);                  // ^create[2002-04-25 18:14:00]
                 if(t<0 || !localtime(&t))                  // ^create[18:14:00]
                         throw Exception(0,                  if(const String *sdate=params->get(0).get_string()) {
                                 &method_name,                          char *cstr=sdate->cstr();
                                 "invalid datetime");                          char *cur=cstr;
                           const char *year=lsplit(&cur, '-');
                           const char *month=lsplit(&cur, '-');
                           const char *mday=lsplit(&cur, ' ');
                           if(!month)
                                   cur=cstr;
                           const char *hour=lsplit(&cur, ':');
                           const char *min=lsplit(&cur, ':');
                           const char *sec=cur;
   
                           tm tmIn={0};
                           tmIn.tm_isdst=-1;
                           if(!month)
                                   if(min) {
                                           year=mday=0; // HH:MM
                                           time_t t=time(0);
                                           tm *tmNow=localtime(&t);
                                           tmIn.tm_year=tmNow->tm_year;
                                           tmIn.tm_mon=tmNow->tm_mon;
                                           tmIn.tm_mday=tmNow->tm_mday;
                                           goto date_part_set;
                                   } else
                                           hour=min=sec=0; // not YYYY- & not HH: = just YYYY                                      
                           tmIn.tm_year=NN_year_to_NNNN(atoi(year));
                           tmIn.tm_mon=month?atoi(month)-1:0;
                           tmIn.tm_mday=mday?atoi(mday):1;
   date_part_set:
                           tmIn.tm_hour=hour?atoi(hour):0;
                           tmIn.tm_min=min?atoi(min):0;
                           tmIn.tm_sec=sec?atoi(sec):0;
                           t=mktime(&tmIn);
                           if(t<0)
                                   throw Exception(0,
                                           sdate,
                                           "invalid datetime");
                   } else { // ^create(float days)
                           t=(time_t)(params->as_double(0, "float days must be double", r)*SECS_PER_DAY);
                           if(t<0 || !localtime(&t))
                                   throw Exception(0,
                                           &method_name,
                                           "invalid datetime");
                   }
         } else if(params->size()>=2) { // ^set(y;m;d[;h[;m[;s]]])          } else if(params->size()>=2) { // ^set(y;m;d[;h[;m[;s]]])
                 tm tmIn={0};                  tm tmIn={0};
                 tmIn.tm_isdst=-1;                  tmIn.tm_isdst=-1;
                 int year=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));
                 if(year<70) // 0..69 -> 100..169 [2000..2069]  
                         year+=100;  
                 if(year>=1900)  
                         year-=1900;  
                 tmIn.tm_year=year;  
                 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;
                 tmIn.tm_mday=params->size()>2?params->as_int(2, "mday must be int", r):1;                  tmIn.tm_mday=params->size()>2?params->as_int(2, "mday must be int", r):1;
                 if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r);                  if(params->size()>3) tmIn.tm_hour=params->as_int(3, "hour must be int", r);
Line 270  static void _calendar(Request& r, const Line 311  static void _calendar(Request& r, const
                 table=fill_week_days(r, method_name, params, rus);                  table=fill_week_days(r, method_name, params, rus);
   
         VTable& result=*new(pool) VTable(pool, table);          VTable& result=*new(pool) VTable(pool, table);
         result.set_name(method_name);  
         r.write_no_lang(result);          r.write_no_lang(result);
 }  }
   
 // constructor  // constructor
   
 MDate::MDate(Pool& apool) : Methoded(apool) {  MDate::MDate(Pool& apool) : Methoded(apool, "date") {
         set_name(*NEW String(pool(), DATE_CLASS_NAME));  
   
   
         // ^now[]          // ^now[]
         add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);          add_native_method("now", Method::CT_DYNAMIC, _now, 0, 1);
   
         // ^set(float days)          // ^create(float days)
         add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);          add_native_method("create", Method::CT_DYNAMIC, _create, 1, 6);
           // 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, 6);
   
         // ^sql-string[]          // ^sql-string[]

Removed from v.1.26  
changed lines
  Added in v.1.32


E-mail: