Changes in uspace/lib/c/generic/time.c [7f9d97f3:1ab8539] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/time.c
r7f9d97f3 r1ab8539 59 59 #define MINS_PER_HOUR 60 60 60 #define SECS_PER_MIN 60 61 #define USECS_PER_SEC 100000062 61 #define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY) 63 62 #define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR) … … 253 252 * Optionally add specified amount of seconds. 254 253 * 255 * @param tm Broken-down time to normalize.256 * @param tv Timevalto add.254 * @param tm Broken-down time to normalize. 255 * @param sec_add Seconds to add. 257 256 * 258 257 * @return 0 on success, -1 on overflow 259 258 * 260 259 */ 261 static int normalize_t m_tv(struct tm *tm, const struct timeval *tv)260 static int normalize_time(struct tm *tm, time_t sec_add) 262 261 { 263 262 // TODO: DST correction 264 263 265 264 /* Set initial values. */ 266 time_t usec = tm->tm_usec + tv->tv_usec; 267 time_t sec = tm->tm_sec + tv->tv_sec; 265 time_t sec = tm->tm_sec + sec_add; 268 266 time_t min = tm->tm_min; 269 267 time_t hour = tm->tm_hour; … … 273 271 274 272 /* Adjust time. */ 275 sec += floor_div(usec, USECS_PER_SEC);276 usec = floor_mod(usec, USECS_PER_SEC);277 273 min += floor_div(sec, SECS_PER_MIN); 278 274 sec = floor_mod(sec, SECS_PER_MIN); … … 323 319 324 320 /* And put the values back to the struct. */ 325 tm->tm_usec = (int) usec;326 321 tm->tm_sec = (int) sec; 327 322 tm->tm_min = (int) min; … … 340 335 } 341 336 342 static int normalize_tm_time(struct tm *tm, time_t time)343 {344 struct timeval tv = {345 .tv_sec = time,346 .tv_usec = 0347 };348 349 return normalize_tm_tv(tm, &tv);350 }351 352 353 337 /** Which day the week-based year starts on. 354 338 * … … 458 442 } 459 443 460 static void tv_normalize(struct timeval *tv)461 {462 while (tv->tv_usec > USECS_PER_SEC) {463 tv->tv_sec++;464 tv->tv_usec -= USECS_PER_SEC;465 }466 while (tv->tv_usec < 0) {467 tv->tv_sec--;468 tv->tv_usec += USECS_PER_SEC;469 }470 }471 472 444 /** Add microseconds to given timeval. 473 445 * … … 476 448 * 477 449 */ 478 void tv_add_diff(struct timeval *tv, suseconds_t usecs) 479 { 480 tv->tv_sec += usecs / USECS_PER_SEC; 481 tv->tv_usec += usecs % USECS_PER_SEC; 482 tv_normalize(tv); 483 } 484 485 /** Add two timevals. 450 void tv_add(struct timeval *tv, suseconds_t usecs) 451 { 452 tv->tv_sec += usecs / 1000000; 453 tv->tv_usec += usecs % 1000000; 454 455 if (tv->tv_usec > 1000000) { 456 tv->tv_sec++; 457 tv->tv_usec -= 1000000; 458 } 459 } 460 461 /** Subtract two timevals. 486 462 * 487 463 * @param tv1 First timeval. 488 464 * @param tv2 Second timeval. 489 */490 void tv_add(struct timeval *tv1, struct timeval *tv2)491 {492 tv1->tv_sec += tv2->tv_sec;493 tv1->tv_usec += tv2->tv_usec;494 tv_normalize(tv1);495 }496 497 /** Subtract two timevals.498 *499 * @param tv1 First timeval.500 * @param tv2 Second timeval.501 465 * 502 466 * @return Difference between tv1 and tv2 (tv1 - tv2) in … … 504 468 * 505 469 */ 506 suseconds_t tv_sub _diff(struct timeval *tv1, struct timeval *tv2)470 suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2) 507 471 { 508 472 return (tv1->tv_usec - tv2->tv_usec) + 509 ((tv1->tv_sec - tv2->tv_sec) * USECS_PER_SEC); 510 } 511 512 /** Subtract two timevals. 513 * 514 * @param tv1 First timeval. 515 * @param tv2 Second timeval. 516 * 517 */ 518 void tv_sub(struct timeval *tv1, struct timeval *tv2) 519 { 520 tv1->tv_sec -= tv2->tv_sec; 521 tv1->tv_usec -= tv2->tv_usec; 522 tv_normalize(tv1); 473 ((tv1->tv_sec - tv2->tv_sec) * 1000000); 523 474 } 524 475 … … 622 573 goto fallback; 623 574 624 tv->tv_usec = time.tm_usec;575 tv->tv_usec = 0; 625 576 tv->tv_sec = mktime(&time); 626 577 … … 738 689 // TODO: detect overflow 739 690 740 normalize_t m_time(tm, 0);691 normalize_time(tm, 0); 741 692 return secs_since_epoch(tm); 742 693 } … … 993 944 994 945 /* Set result to epoch. */ 995 result->tm_usec = 0;996 946 result->tm_sec = 0; 997 947 result->tm_min = 0; … … 1001 951 result->tm_year = 70; /* 1970 */ 1002 952 1003 if (normalize_t m_time(result, time) == -1)953 if (normalize_time(result, time) == -1) 1004 954 return EOVERFLOW; 1005 955 … … 1064 1014 * Time is expressed relative to the user's specified timezone. 1065 1015 * 1066 * @param t v Timevalto convert.1016 * @param timer Time to convert. 1067 1017 * @param result Structure to store the result to. 1068 1018 * … … 1070 1020 * 1071 1021 */ 1072 int time_ tv2tm(const struct timeval *tv, struct tm *restrict result)1022 int time_local2tm(const time_t time, struct tm *restrict result) 1073 1023 { 1074 1024 // TODO: Deal with timezones. … … 1076 1026 1077 1027 /* Set result to epoch. */ 1078 result->tm_usec = 0;1079 1028 result->tm_sec = 0; 1080 1029 result->tm_min = 0; … … 1084 1033 result->tm_year = 70; /* 1970 */ 1085 1034 1086 if (normalize_t m_tv(result, tv) == -1)1035 if (normalize_time(result, time) == -1) 1087 1036 return EOVERFLOW; 1088 1037 1089 1038 return EOK; 1090 }1091 1092 /** Converts a time value to a broken-down local time.1093 *1094 * Time is expressed relative to the user's specified timezone.1095 *1096 * @param timer Time to convert.1097 * @param result Structure to store the result to.1098 *1099 * @return EOK on success or a negative error code.1100 *1101 */1102 int time_local2tm(const time_t time, struct tm *restrict result)1103 {1104 struct timeval tv = {1105 .tv_sec = time,1106 .tv_usec = 01107 };1108 1109 return time_tv2tm(&tv, result);1110 1039 } 1111 1040
Note:
See TracChangeset
for help on using the changeset viewer.