Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/time.c

    r7f9d97f3 r1ab8539  
    5959#define MINS_PER_HOUR  60
    6060#define SECS_PER_MIN   60
    61 #define USECS_PER_SEC  1000000
    6261#define MINS_PER_DAY   (MINS_PER_HOUR * HOURS_PER_DAY)
    6362#define SECS_PER_HOUR  (SECS_PER_MIN * MINS_PER_HOUR)
     
    253252 * Optionally add specified amount of seconds.
    254253 *
    255  * @param tm Broken-down time to normalize.
    256  * @param tv Timeval to add.
     254 * @param tm      Broken-down time to normalize.
     255 * @param sec_add Seconds to add.
    257256 *
    258257 * @return 0 on success, -1 on overflow
    259258 *
    260259 */
    261 static int normalize_tm_tv(struct tm *tm, const struct timeval *tv)
     260static int normalize_time(struct tm *tm, time_t sec_add)
    262261{
    263262        // TODO: DST correction
    264263       
    265264        /* 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;
    268266        time_t min = tm->tm_min;
    269267        time_t hour = tm->tm_hour;
     
    273271       
    274272        /* Adjust time. */
    275         sec += floor_div(usec, USECS_PER_SEC);
    276         usec = floor_mod(usec, USECS_PER_SEC);
    277273        min += floor_div(sec, SECS_PER_MIN);
    278274        sec = floor_mod(sec, SECS_PER_MIN);
     
    323319       
    324320        /* And put the values back to the struct. */
    325         tm->tm_usec = (int) usec;
    326321        tm->tm_sec = (int) sec;
    327322        tm->tm_min = (int) min;
     
    340335}
    341336
    342 static int normalize_tm_time(struct tm *tm, time_t time)
    343 {
    344         struct timeval tv = {
    345                 .tv_sec = time,
    346                 .tv_usec = 0
    347         };
    348 
    349         return normalize_tm_tv(tm, &tv);
    350 }
    351 
    352 
    353337/** Which day the week-based year starts on.
    354338 *
     
    458442}
    459443
    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 
    472444/** Add microseconds to given timeval.
    473445 *
     
    476448 *
    477449 */
    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.
     450void 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.
    486462 *
    487463 * @param tv1 First timeval.
    488464 * @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.
    501465 *
    502466 * @return Difference between tv1 and tv2 (tv1 - tv2) in
     
    504468 *
    505469 */
    506 suseconds_t tv_sub_diff(struct timeval *tv1, struct timeval *tv2)
     470suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
    507471{
    508472        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);
    523474}
    524475
     
    622573                goto fallback;
    623574       
    624         tv->tv_usec = time.tm_usec;
     575        tv->tv_usec = 0;
    625576        tv->tv_sec = mktime(&time);
    626577       
     
    738689        // TODO: detect overflow
    739690       
    740         normalize_tm_time(tm, 0);
     691        normalize_time(tm, 0);
    741692        return secs_since_epoch(tm);
    742693}
     
    993944       
    994945        /* Set result to epoch. */
    995         result->tm_usec = 0;
    996946        result->tm_sec = 0;
    997947        result->tm_min = 0;
     
    1001951        result->tm_year = 70; /* 1970 */
    1002952       
    1003         if (normalize_tm_time(result, time) == -1)
     953        if (normalize_time(result, time) == -1)
    1004954                return EOVERFLOW;
    1005955       
     
    10641014 * Time is expressed relative to the user's specified timezone.
    10651015 *
    1066  * @param tv     Timeval to convert.
     1016 * @param timer  Time to convert.
    10671017 * @param result Structure to store the result to.
    10681018 *
     
    10701020 *
    10711021 */
    1072 int time_tv2tm(const struct timeval *tv, struct tm *restrict result)
     1022int time_local2tm(const time_t time, struct tm *restrict result)
    10731023{
    10741024        // TODO: Deal with timezones.
     
    10761026       
    10771027        /* Set result to epoch. */
    1078         result->tm_usec = 0;
    10791028        result->tm_sec = 0;
    10801029        result->tm_min = 0;
     
    10841033        result->tm_year = 70; /* 1970 */
    10851034       
    1086         if (normalize_tm_tv(result, tv) == -1)
     1035        if (normalize_time(result, time) == -1)
    10871036                return EOVERFLOW;
    10881037       
    10891038        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 = 0
    1107         };
    1108 
    1109         return time_tv2tm(&tv, result);
    11101039}
    11111040
Note: See TracChangeset for help on using the changeset viewer.