Changeset 531
- Timestamp:
- 02/04/09 22:58:22 (3 years ago)
- File:
-
- 1 edited
-
interpreter/trunk/modules/System/date/hires.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
interpreter/trunk/modules/System/date/hires.c
r527 r531 4 4 #ifndef WIN32 5 5 #include <unistd.h> 6 #include <sys/time.h>7 #else8 #include <winsock2.h>9 #include <time.h>10 11 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)12 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui6413 #else14 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL15 #endif16 17 6 #endif /* WIN32 */ 18 7 #include <errno.h> 19 8 #define __USE_XOPEN 9 #include <sys/time.h> 20 10 21 11 #include "kite_object.h" … … 45 35 } 46 36 47 #ifdef WIN3248 struct timezone49 {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 */89 37 90 38 /* Subtract the `struct timeval' values X and Y, … … 126 74 } 127 75 128 KITE_CLASS_METHOD( Hires__subtract) {76 KITE_CLASS_METHOD(hires_subtract) { 129 77 struct timeval *me = malloc(sizeof(struct timeval)); 130 78 struct timeval *you = malloc(sizeof(struct timeval)); … … 132 80 133 81 TYPECHECK_OPERANDS; 82 134 83 me->tv_sec = KITE_GET_INTEGER(kite_get_property(this,"_sec")); 135 84 me->tv_usec = KITE_GET_INTEGER(kite_get_property(this,"_usec")); 136 85 137 you->tv_sec = KITE_GET_INTEGER(kite_get_property(rhs,"_sec"));86 you->tv_sec = KITE_GET_INTEGER(kite_get_property(rhs,"_sec")); 138 87 you->tv_usec = KITE_GET_INTEGER(kite_get_property(rhs,"_usec")); 139 88 … … 144 93 } 145 94 146 KITE_CLASS_METHOD( Hires__nowLocal)95 KITE_CLASS_METHOD(hires_nowLocal) 147 96 { 148 97 kite_object_t *newObject; … … 151 100 KITE_NO_ARGS; 152 101 KITE_THIS_NOT_USED; 153 102 154 103 newObject = kite_dereference_and_load(thd, "hires"); 155 104 newObject = kite_new_instance_with_constructor( … … 168 117 } 169 118 170 KITE_CLASS_METHOD( Hires__nowGmt)119 KITE_CLASS_METHOD(hires_nowGmt) 171 120 { 172 121 kite_object_t *newObject; … … 174 123 struct timezone *zone; 175 124 125 KITE_NO_ARGS; 176 126 KITE_THIS_NOT_USED; 177 KITE_NO_ARGS; 178 127 179 128 newObject = kite_dereference_and_load(thd, "hires"); 180 129 newObject = kite_new_instance_with_constructor( … … 195 144 } 196 145 197 KITE_CLASS_METHOD( Hires__str)146 KITE_CLASS_METHOD(hires_str) 198 147 { 199 148 time_t t = (time_t)KITE_GET_INTEGER(kite_get_property(this,"_sec")); … … 201 150 202 151 KITE_NO_ARGS; 203 152 204 153 ret = kite_new_string(thd, ctime(&t)); 205 154 kite_vm_push(thd, ret); 206 155 kite_dereference_object(ret); 207 156 } 208 KITE_CLASS_METHOD( Hires__usec)157 KITE_CLASS_METHOD(hires_usec) 209 158 { 210 159 time_t usec = (time_t)KITE_GET_INTEGER(kite_get_property(this,"_usec")); … … 213 162 } 214 163 215 KITE_CLASS_METHOD( Hires__sec)164 KITE_CLASS_METHOD(hires_sec) 216 165 { 217 166 GET_BREAKDOWN_FIELD(tm_sec); 218 167 } 219 168 220 KITE_CLASS_METHOD( Hires__min)169 KITE_CLASS_METHOD(hires_min) 221 170 { 222 171 GET_BREAKDOWN_FIELD(tm_min); 223 172 } 224 173 225 KITE_CLASS_METHOD( Hires__hour)174 KITE_CLASS_METHOD(hires_hour) 226 175 { 227 176 GET_BREAKDOWN_FIELD(tm_hour); 228 177 } 229 178 230 KITE_CLASS_METHOD( Hires__day_of_month)179 KITE_CLASS_METHOD(hires_day_of_month) 231 180 { 232 181 GET_BREAKDOWN_FIELD(tm_mday); 233 182 } 234 183 235 KITE_CLASS_METHOD( Hires__month)184 KITE_CLASS_METHOD(hires_month) 236 185 { 237 186 GET_BREAKDOWN_FIELD(tm_mon); 238 187 } 239 188 240 KITE_CLASS_METHOD( Hires__year)189 KITE_CLASS_METHOD(hires_year) 241 190 { 242 191 GET_BREAKDOWN_FIELD(tm_year); 243 192 } 244 193 245 KITE_CLASS_METHOD( Hires__timestamp)194 KITE_CLASS_METHOD(hires_timestamp) 246 195 { 247 196 kite_object_t *ret; … … 254 203 } 255 204 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); 205 KITE_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)); 286 210 } 287 211 /***************************************************************************** 288 212 * Format date using given format string 289 213 ****************************************************************************/ 290 KITE_CLASS_METHOD( Hires__format)214 KITE_CLASS_METHOD(hires_format) 291 215 { 292 216 struct tm *thistime; … … 314 238 * Parse date using given format string 315 239 ****************************************************************************/ 316 KITE_CLASS_METHOD( Hires__parse)240 KITE_CLASS_METHOD(hires_parse) 317 241 { 318 242 kite_object_t *date, *format; … … 354 278 355 279 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, 357 281 "Subtracts two dates with fractional-second precision.", 1, 358 282 "rhs", "The date to subtract from the current date.")); 359 283 360 284 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, 362 286 "Returns the current local time.", 0)); 363 287 364 288 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, 366 290 "Returns the current time, adjusted for GMT.", 0)); 367 291 368 292 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, 370 294 "Provides the time in a readable format.", 0)); 371 295 372 296 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, 374 298 "Return given date as the number of seconds since 1970.", 0)); 375 299 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, 377 301 "Parse given value into a date object.", 2, 378 302 "val", "Value to parse.", 379 303 "format", "Format to use (see man strptime for info).")); 380 304 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, 382 306 "Format date into a string, given format specifier.", 1, 383 307 "format", "Format specifier (see man strftime for valid characters).")); 384 308 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)); 387 311 388 312 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, 390 314 "Number of microseconds (0-999999).", 0)); 391 315 392 316 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, 394 318 "Number of seconds (0-59).", 0)); 395 319 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, 397 321 "Number of minutes (0-59).", 0)); 398 322 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, 400 324 "Number of hours (0-23).", 0)); 401 325 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, 403 327 "Day of the month (1-31).", 0)); 404 328 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, 406 330 "Month of the year (1-12).", 0)); 407 331 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, 409 333 "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 423 335 kite_add_property(thread, newclass, "_sec", 0, "Number of seconds since epoch."); 424 336 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.
