Changeset 531


Ignore:
Timestamp:
02/04/09 22:58:22 (3 years ago)
Author:
mooneer
Message:

New version of System.date.hires.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interpreter/trunk/modules/System/date/hires.c

    r527 r531  
    44#ifndef WIN32 
    55#include <unistd.h> 
    6 #include <sys/time.h> 
    7 #else 
    8 #include <winsock2.h> 
    9 #include <time.h> 
    10  
    11 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) 
    12   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64 
    13 #else 
    14   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL 
    15 #endif 
    16  
    176#endif /* WIN32 */ 
    187#include <errno.h> 
    198#define __USE_XOPEN 
     9#include <sys/time.h> 
    2010 
    2111#include "kite_object.h" 
     
    4535    } 
    4636 
    47 #ifdef WIN32 
    48 struct timezone  
    49 { 
    50   int  tz_minuteswest; /* minutes W of Greenwich */ 
    51   int  tz_dsttime;     /* type of dst correction */ 
    52 }; 
    53   
    54 int gettimeofday(struct timeval *tv, struct timezone *tz) 
    55 { 
    56   FILETIME ft; 
    57   unsigned __int64 tmpres = 0; 
    58   static int tzflag; 
    59   
    60   if (NULL != tv) 
    61   { 
    62     GetSystemTimeAsFileTime(&ft); 
    63   
    64     tmpres |= ft.dwHighDateTime; 
    65     tmpres <<= 32; 
    66     tmpres |= ft.dwLowDateTime; 
    67   
    68     /*converting file time to unix epoch*/ 
    69     tmpres /= 10;  /*convert into microseconds*/ 
    70     tmpres -= DELTA_EPOCH_IN_MICROSECS;  
    71     tv->tv_sec = (long)(tmpres / 1000000UL); 
    72     tv->tv_usec = (long)(tmpres % 1000000UL); 
    73   } 
    74   
    75   if (NULL != tz) 
    76   { 
    77     if (!tzflag) 
    78     { 
    79       _tzset(); 
    80       tzflag++; 
    81     } 
    82     tz->tz_minuteswest = _timezone / 60; 
    83     tz->tz_dsttime = _daylight; 
    84   } 
    85   
    86   return 0; 
    87 } 
    88 #endif /* WIN32 */ 
    8937 
    9038/* Subtract the `struct timeval' values X and Y, 
     
    12674} 
    12775 
    128 KITE_CLASS_METHOD(Hires__subtract) { 
     76KITE_CLASS_METHOD(hires_subtract) { 
    12977  struct timeval *me = malloc(sizeof(struct timeval)); 
    13078  struct timeval *you = malloc(sizeof(struct timeval)); 
     
    13280   
    13381  TYPECHECK_OPERANDS; 
     82     
    13483  me->tv_sec = KITE_GET_INTEGER(kite_get_property(this,"_sec")); 
    13584  me->tv_usec = KITE_GET_INTEGER(kite_get_property(this,"_usec")); 
    13685     
    137     you->tv_sec = KITE_GET_INTEGER(kite_get_property(rhs,"_sec")); 
     86     you->tv_sec = KITE_GET_INTEGER(kite_get_property(rhs,"_sec")); 
    13887    you->tv_usec = KITE_GET_INTEGER(kite_get_property(rhs,"_usec")); 
    13988     
     
    14493} 
    14594 
    146 KITE_CLASS_METHOD(Hires__nowLocal) 
     95KITE_CLASS_METHOD(hires_nowLocal) 
    14796{ 
    14897  kite_object_t *newObject; 
     
    151100  KITE_NO_ARGS; 
    152101  KITE_THIS_NOT_USED; 
    153    
     102     
    154103  newObject = kite_dereference_and_load(thd, "hires"); 
    155104  newObject = kite_new_instance_with_constructor( 
     
    168117} 
    169118 
    170 KITE_CLASS_METHOD(Hires__nowGmt) 
     119KITE_CLASS_METHOD(hires_nowGmt) 
    171120{ 
    172121  kite_object_t *newObject; 
     
    174123  struct timezone *zone; 
    175124   
     125  KITE_NO_ARGS; 
    176126  KITE_THIS_NOT_USED; 
    177   KITE_NO_ARGS; 
    178     
     127   
    179128  newObject = kite_dereference_and_load(thd, "hires"); 
    180129  newObject = kite_new_instance_with_constructor( 
     
    195144} 
    196145 
    197 KITE_CLASS_METHOD(Hires__str) 
     146KITE_CLASS_METHOD(hires_str) 
    198147{ 
    199148    time_t t = (time_t)KITE_GET_INTEGER(kite_get_property(this,"_sec")); 
     
    201150 
    202151    KITE_NO_ARGS; 
    203         
     152         
    204153    ret = kite_new_string(thd, ctime(&t)); 
    205154    kite_vm_push(thd, ret); 
    206155    kite_dereference_object(ret); 
    207156} 
    208 KITE_CLASS_METHOD(Hires__usec) 
     157KITE_CLASS_METHOD(hires_usec) 
    209158{ 
    210159  time_t usec = (time_t)KITE_GET_INTEGER(kite_get_property(this,"_usec"));  
     
    213162} 
    214163 
    215 KITE_CLASS_METHOD(Hires__sec) 
     164KITE_CLASS_METHOD(hires_sec) 
    216165{ 
    217166  GET_BREAKDOWN_FIELD(tm_sec); 
    218167} 
    219168 
    220 KITE_CLASS_METHOD(Hires__min) 
     169KITE_CLASS_METHOD(hires_min) 
    221170{ 
    222171  GET_BREAKDOWN_FIELD(tm_min); 
    223172} 
    224173 
    225 KITE_CLASS_METHOD(Hires__hour) 
     174KITE_CLASS_METHOD(hires_hour) 
    226175{ 
    227176  GET_BREAKDOWN_FIELD(tm_hour); 
    228177} 
    229178 
    230 KITE_CLASS_METHOD(Hires__day_of_month) 
     179KITE_CLASS_METHOD(hires_day_of_month) 
    231180{ 
    232181  GET_BREAKDOWN_FIELD(tm_mday); 
    233182} 
    234183 
    235 KITE_CLASS_METHOD(Hires__month) 
     184KITE_CLASS_METHOD(hires_month) 
    236185{ 
    237186  GET_BREAKDOWN_FIELD(tm_mon); 
    238187} 
    239188 
    240 KITE_CLASS_METHOD(Hires__year) 
     189KITE_CLASS_METHOD(hires_year) 
    241190{ 
    242191  GET_BREAKDOWN_FIELD(tm_year); 
    243192} 
    244193 
    245 KITE_CLASS_METHOD(Hires__timestamp) 
     194KITE_CLASS_METHOD(hires_timestamp) 
    246195{ 
    247196    kite_object_t *ret; 
     
    254203} 
    255204 
    256 KITE_CLASS_METHOD(Hires__construct) 
    257 { 
    258   struct tm date_values; 
    259   kite_object_t *arg; 
    260   int i; 
    261   int *cur; 
    262  
    263   memset(&date_values, '0', sizeof(struct tm)); 
    264    
    265   KITE_GET_METHOD_ARGUMENT(arg, 1); 
    266   if (arg == NULL) { 
    267       // empty constructor 
    268       Date_copyToProperties(thd, this, &date_values); 
    269       return; 
    270   } 
    271  
    272   // all time_t struct properties are ints. 
    273   cur = (int*)&date_values; 
    274   for (i = 1; i <= 9; i++) { 
    275       if (!KITE_IS_TYPE(arg, OBJ_INTEGER)) { 
    276           kite_object_t *exc; 
    277           exc = kite_new_exception(thd, "System.exceptions.TypeMismatch", 
    278                                   "Operand must be integer"); 
    279           kite_vm_call_method(thd, exc, "throw", kite_new_list(thd), TRUE); 
    280           return; 
    281       } 
    282       *cur = KITE_GET_INTEGER(arg); 
    283       cur++; 
    284   } 
    285   Date_copyToProperties(thd, this, &date_values); 
     205KITE_CLASS_METHOD(hires_construct) 
     206{ 
     207  KITE_NO_ARGS; 
     208  kite_set_property(thd, this, "_sec", kite_new_integer(thd, 0)); 
     209  kite_set_property(thd, this, "_usec", kite_new_integer(thd, 0)); 
    286210} 
    287211/***************************************************************************** 
    288212 * Format date using given format string 
    289213 ****************************************************************************/ 
    290 KITE_CLASS_METHOD(Hires__format) 
     214KITE_CLASS_METHOD(hires_format) 
    291215{ 
    292216    struct tm *thistime; 
     
    314238 * Parse date using given format string 
    315239 ****************************************************************************/ 
    316 KITE_CLASS_METHOD(Hires__parse) 
     240KITE_CLASS_METHOD(hires_parse) 
    317241{ 
    318242    kite_object_t *date, *format; 
     
    354278    
    355279   kite_add_operator(thread, newclass, OP_SUBTRACT,  
    356        kite_new_method_compiled_with_docs(thread, Hires__subtract,  
     280       kite_new_method_compiled_with_docs(thread, hires_subtract,  
    357281                                 "Subtracts two dates with fractional-second precision.", 1, 
    358282                                 "rhs", "The date to subtract from the current date.")); 
    359283    
    360284   kite_add_method(thread, newclass, "nowLocal", 
    361        kite_new_method_compiled_with_docs(thread, Hires__nowLocal, 
     285       kite_new_method_compiled_with_docs(thread, hires_nowLocal, 
    362286                                 "Returns the current local time.", 0)); 
    363287 
    364288   kite_add_method(thread, newclass, "nowGmt", 
    365        kite_new_method_compiled_with_docs(thread, Hires__nowGmt, 
     289       kite_new_method_compiled_with_docs(thread, hires_nowGmt, 
    366290                                 "Returns the current time, adjusted for GMT.", 0)); 
    367291    
    368292   kite_add_method(thread, newclass, "str", 
    369        kite_new_method_compiled_with_docs(thread, Hires__str, 
     293       kite_new_method_compiled_with_docs(thread, hires_str, 
    370294                                 "Provides the time in a readable format.", 0)); 
    371295                                  
    372296   kite_add_method(thread, newclass, "timestamp", 
    373        kite_new_method_compiled_with_docs(thread, Hires__timestamp, 
     297       kite_new_method_compiled_with_docs(thread, hires_timestamp, 
    374298                                 "Return given date as the number of seconds since 1970.", 0)); 
    375299   kite_add_method(thread, newclass, "parse",  
    376        kite_new_method_compiled_with_docs(thread, Hires__parse,  
     300       kite_new_method_compiled_with_docs(thread, hires_parse,  
    377301                                 "Parse given value into a date object.", 2, 
    378302                                 "val", "Value to parse.", 
    379303                                 "format", "Format to use (see man strptime for info).")); 
    380304   kite_add_method(thread, newclass, "format",  
    381        kite_new_method_compiled_with_docs(thread, Hires__format, 
     305       kite_new_method_compiled_with_docs(thread, hires_format, 
    382306                                 "Format date into a string, given format specifier.", 1, 
    383307                                 "format", "Format specifier (see man strftime for valid characters).")); 
    384308   kite_add_method(thread, newclass, "__construct__", 
    385        kite_new_method_compiled_with_docs(thread, Hires__construct,  
    386                                  "Constructs a new date object.", 0)); 
     309       kite_new_method_compiled_with_docs(thread, hires_construct,  
     310                                 "Constructs a new date object, set to epoch (1970 new year's day).", 0)); 
    387311                                  
    388312   kite_add_method(thread, newclass, "usec", 
    389        kite_new_method_compiled_with_docs(thread, Hires__usec,  
     313       kite_new_method_compiled_with_docs(thread, hires_usec,  
    390314                                 "Number of microseconds (0-999999).", 0));     
    391315                                                               
    392316   kite_add_method(thread, newclass, "sec", 
    393        kite_new_method_compiled_with_docs(thread, Hires__sec,  
     317       kite_new_method_compiled_with_docs(thread, hires_sec,  
    394318                                 "Number of seconds (0-59).", 0)); 
    395319   kite_add_method(thread, newclass, "min", 
    396        kite_new_method_compiled_with_docs(thread, Hires__min,  
     320       kite_new_method_compiled_with_docs(thread, hires_min,  
    397321                                 "Number of minutes (0-59).", 0)); 
    398322   kite_add_method(thread, newclass, "hour", 
    399        kite_new_method_compiled_with_docs(thread, Hires__hour,  
     323       kite_new_method_compiled_with_docs(thread, hires_hour,  
    400324                                 "Number of hours (0-23).", 0)); 
    401325   kite_add_method(thread, newclass, "day_of_month", 
    402        kite_new_method_compiled_with_docs(thread, Hires__day_of_month,  
     326       kite_new_method_compiled_with_docs(thread, hires_day_of_month,  
    403327                                 "Day of the month (1-31).", 0)); 
    404328   kite_add_method(thread, newclass, "month", 
    405        kite_new_method_compiled_with_docs(thread, Hires__month,  
     329       kite_new_method_compiled_with_docs(thread, hires_month,  
    406330                                 "Month of the year (1-12).", 0)); 
    407331   kite_add_method(thread, newclass, "year", 
    408        kite_new_method_compiled_with_docs(thread, Hires__year,  
     332       kite_new_method_compiled_with_docs(thread, hires_year,  
    409333                                 "Number of seconds (1970-∞).", 0)); 
    410  
    411    kite_add_method(thread, newclass, "__construct__", 
    412        kite_new_method_compiled_with_docs(thread, Hires__construct,  
    413                                  "Constructs a new date object.", 9, 
    414                                  "sec", "Number of seconds (0-59).", 
    415                                  "min", "Number of minutes (0-59).", 
    416                                  "hour", "Number of hours (0-23).", 
    417                                  "day_of_month", "Day of the month (1-31).", 
    418                                  "month", "Month (1-12).", 
    419                                  "year", "Year (yyyy).", 
    420                                  "day_of_week", "Day of the week (0-6).", 
    421                                  "day_of_year", "Day of the year (0-365).", 
    422                                  "dst", "Daylight Saving Time (true/false)."));                            
     334                          
    423335  kite_add_property(thread, newclass, "_sec", 0, "Number of seconds since epoch."); 
    424336  kite_add_property(thread, newclass, "_usec", 0, "Number of microseconds in the time since epoch."); 
Note: See TracChangeset for help on using the changeset viewer.