Ignore:
File:
1 edited

Legend:

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

    r5a6cc679 ra35b458  
    8282{
    8383        year += 1900;
    84        
     84
    8585        if (year % 400 == 0)
    8686                return true;
    87        
     87
    8888        if (year % 100 == 0)
    8989                return false;
    90        
     90
    9191        if (year % 4 == 0)
    9292                return true;
    93        
     93
    9494        return false;
    9595}
     
    110110        assert(mon >= 0);
    111111        assert(mon <= 11);
    112        
     112
    113113        static int month_days[] = {
    114114                31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    115115        };
    116        
     116
    117117        if (mon == 1) {
    118118                /* February */
     
    120120                return is_leap_year(year) ? 29 : 28;
    121121        }
    122        
     122
    123123        return month_days[mon];
    124124}
     
    144144                0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
    145145        };
    146        
     146
    147147        static int leap_mdays[] = {
    148148                0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
    149149        };
    150        
     150
    151151        return (is_leap_year(year) ? leap_mdays[mon] : mdays[mon]) + mday - 1;
    152152}
     
    166166        if ((op1 >= 0) || (op1 % op2 == 0))
    167167                return op1 / op2;
    168        
     168
    169169        return op1 / op2 - 1;
    170170}
     
    183183{
    184184        time_t div = floor_div(op1, op2);
    185        
     185
    186186        /*
    187187         * (a / b) * b + a % b == a
    188188         * Thus: a % b == a - (a / b) * b
    189189         */
    190        
     190
    191191        time_t result = op1 - div * op2;
    192        
     192
    193193        /* Some paranoid checking to ensure there is mistake here. */
    194194        assert(result >= 0);
    195195        assert(result < op2);
    196196        assert(div * op2 + result == op1);
    197        
     197
    198198        return result;
    199199}
     
    261261{
    262262        // TODO: DST correction
    263        
     263
    264264        /* Set initial values. */
    265265        time_t usec = tm->tm_usec + tv->tv_usec;
     
    270270        time_t mon = tm->tm_mon;
    271271        time_t year = tm->tm_year;
    272        
     272
    273273        /* Adjust time. */
    274274        sec += floor_div(usec, USECS_PER_SEC);
     
    280280        day += floor_div(hour, HOURS_PER_DAY);
    281281        hour = floor_mod(hour, HOURS_PER_DAY);
    282        
     282
    283283        /* Adjust month. */
    284284        year += floor_div(mon, 12);
    285285        mon = floor_mod(mon, 12);
    286        
     286
    287287        /* Now the difficult part - days of month. */
    288        
     288
    289289        /* First, deal with whole cycles of 400 years = 146097 days. */
    290290        year += floor_div(day, 146097) * 400;
    291291        day = floor_mod(day, 146097);
    292        
     292
    293293        /* Then, go in one year steps. */
    294294        if (mon <= 1) {
     
    305305                }
    306306        }
    307        
     307
    308308        /* Finally, finish it off month per month. */
    309309        while (day >= days_in_month(year, mon)) {
    310310                day -= days_in_month(year, mon);
    311311                mon++;
    312                
     312
    313313                if (mon >= 12) {
    314314                        mon -= 12;
     
    316316                }
    317317        }
    318        
     318
    319319        /* Calculate the remaining two fields. */
    320320        tm->tm_yday = day_of_year(year, mon, day + 1);
    321321        tm->tm_wday = day_of_week(year, mon, day + 1);
    322        
     322
    323323        /* And put the values back to the struct. */
    324324        tm->tm_usec = (int) usec;
     
    328328        tm->tm_mday = (int) day + 1;
    329329        tm->tm_mon = (int) mon;
    330        
     330
    331331        /* Casts to work around POSIX brain-damage. */
    332332        if (year > ((int) INT_MAX) || year < ((int) INT_MIN)) {
     
    334334                return -1;
    335335        }
    336        
     336
    337337        tm->tm_year = (int) year;
    338338        return 0;
     
    363363{
    364364        int start_wday = day_of_week(year, 0, 1);
    365        
     365
    366366        return floor_mod(4 - start_wday, 7) - 3;
    367367}
     
    377377{
    378378        int day = tm->tm_yday - wbyear_offset(tm->tm_year);
    379        
     379
    380380        if (day < 0) {
    381381                /* Last week of previous year. */
    382382                return tm->tm_year - 1;
    383383        }
    384        
     384
    385385        if (day > 364 + is_leap_year(tm->tm_year)) {
    386386                /* First week of next year. */
    387387                return tm->tm_year + 1;
    388388        }
    389        
     389
    390390        /* All the other days are in the calendar year. */
    391391        return tm->tm_year;
     
    405405{
    406406        int first_day = (7 - day_of_week(tm->tm_year, 0, 1)) % 7;
    407        
     407
    408408        return (tm->tm_yday - first_day + 7) / 7;
    409409}
     
    425425{
    426426        int day = tm->tm_yday - wbyear_offset(tm->tm_year);
    427        
     427
    428428        if (day < 0) {
    429429                /* Last week of previous year. */
    430430                return 53;
    431431        }
    432        
     432
    433433        if (day > 364 + is_leap_year(tm->tm_year)) {
    434434                /* First week of next year. */
    435435                return 1;
    436436        }
    437        
     437
    438438        /* All the other days give correct answer. */
    439439        return (day / 7 + 1);
     
    453453{
    454454        int first_day = (1 - day_of_week(tm->tm_year, 0, 1)) % 7;
    455        
     455
    456456        return (tm->tm_yday - first_day + 7) / 7;
    457457}
     
    535535        if (tv1->tv_sec > tv2->tv_sec)
    536536                return true;
    537        
     537
    538538        if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec > tv2->tv_usec))
    539539                return true;
    540        
     540
    541541        return false;
    542542}
     
    555555        if (tv1->tv_sec > tv2->tv_sec)
    556556                return true;
    557        
     557
    558558        if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec >= tv2->tv_usec))
    559559                return true;
    560        
     560
    561561        return false;
    562562}
     
    582582                tz->tz_dsttime = DST_NONE;
    583583        }
    584        
     584
    585585        if (clock_conn == NULL) {
    586586                category_id_t cat_id;
     
    588588                if (rc != EOK)
    589589                        goto fallback;
    590                
     590
    591591                service_id_t *svc_ids;
    592592                size_t svc_cnt;
     
    594594                if (rc != EOK)
    595595                        goto fallback;
    596                
     596
    597597                if (svc_cnt == 0)
    598598                        goto fallback;
    599                
     599
    600600                char *svc_name;
    601601                rc = loc_service_get_name(svc_ids[0], &svc_name);
     
    603603                if (rc != EOK)
    604604                        goto fallback;
    605                
     605
    606606                service_id_t svc_id;
    607607                rc = loc_service_get_id(svc_name, &svc_id, 0);
     
    609609                if (rc != EOK)
    610610                        goto fallback;
    611                
     611
    612612                clock_conn = loc_service_connect(svc_id, INTERFACE_DDF,
    613613                    IPC_FLAG_BLOCKING);
     
    615615                        goto fallback;
    616616        }
    617        
     617
    618618        struct tm time;
    619619        errno_t rc = clock_dev_time_get(clock_conn, &time);
    620620        if (rc != EOK)
    621621                goto fallback;
    622        
     622
    623623        tv->tv_usec = time.tm_usec;
    624624        tv->tv_sec = mktime(&time);
    625        
     625
    626626        return;
    627        
     627
    628628fallback:
    629629        getuptime(tv);
     
    639639                        goto fallback;
    640640                }
    641                
     641
    642642                void *addr = AS_AREA_ANY;
    643643                rc = physmem_map(faddr, 1, AS_AREA_READ | AS_AREA_CACHEABLE,
     
    648648                        goto fallback;
    649649                }
    650                
     650
    651651                ktime = addr;
    652652        }
    653        
     653
    654654        sysarg_t s2 = ktime->seconds2;
    655        
     655
    656656        read_barrier();
    657657        tv->tv_usec = ktime->useconds;
    658        
     658
    659659        read_barrier();
    660660        sysarg_t s1 = ktime->seconds1;
    661        
     661
    662662        if (s1 != s2) {
    663663                tv->tv_sec = max(s1, s2);
     
    665665        } else
    666666                tv->tv_sec = s1;
    667        
     667
    668668        return;
    669        
     669
    670670fallback:
    671671        tv->tv_sec = 0;
     
    677677        struct timeval tv;
    678678        gettimeofday(&tv, NULL);
    679        
     679
    680680        if (tloc)
    681681                *tloc = tv.tv_sec;
    682        
     682
    683683        return tv.tv_sec;
    684684}
     
    706706        // TODO: take DST flag into account
    707707        // TODO: detect overflow
    708        
     708
    709709        normalize_tm_time(tm, 0);
    710710        return secs_since_epoch(tm);
     
    755755        assert(format != NULL);
    756756        assert(tm != NULL);
    757        
     757
    758758        // TODO: use locale
    759        
     759
    760760        static const char *wday_abbr[] = {
    761761                "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    762762        };
    763        
     763
    764764        static const char *wday[] = {
    765765                "Sunday", "Monday", "Tuesday", "Wednesday",
    766766                "Thursday", "Friday", "Saturday"
    767767        };
    768        
     768
    769769        static const char *mon_abbr[] = {
    770770                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    771771                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    772772        };
    773        
     773
    774774        static const char *mon[] = {
    775775                "January", "February", "March", "April", "May", "June", "July",
    776776                "August", "September", "October", "November", "December"
    777777        };
    778        
     778
    779779        if (maxsize < 1)
    780780                return 0;
    781        
     781
    782782        char *ptr = s;
    783783        size_t consumed;
    784784        size_t remaining = maxsize;
    785        
     785
    786786        while (*format != '\0') {
    787787                if (*format != '%') {
     
    790790                        continue;
    791791                }
    792                
     792
    793793                format++;
    794794                if ((*format == '0') || (*format == '+')) {
     
    796796                        format++;
    797797                }
    798                
     798
    799799                while (isdigit(*format)) {
    800800                        // TODO: padding
    801801                        format++;
    802802                }
    803                
     803
    804804                if ((*format == 'O') || (*format == 'E')) {
    805805                        // TODO: locale's alternative format
    806806                        format++;
    807807                }
    808                
     808
    809809                switch (*format) {
    810810                case 'a':
     
    938938                        while (*format != '%')
    939939                                format--;
    940                        
     940
    941941                        APPEND("%%");
    942942                        break;
    943943                }
    944                
     944
    945945                format++;
    946946        }
    947        
     947
    948948        return maxsize - remaining;
    949949}
     
    960960{
    961961        assert(result != NULL);
    962        
     962
    963963        /* Set result to epoch. */
    964964        result->tm_usec = 0;
     
    969969        result->tm_mon = 0;
    970970        result->tm_year = 70; /* 1970 */
    971        
     971
    972972        if (normalize_tm_time(result, time) == -1)
    973973                return EOVERFLOW;
    974        
     974
    975975        return EOK;
    976976}
     
    993993        if (ret != EOK)
    994994                return ret;
    995        
     995
    996996        time_tm2str(&tm, buf);
    997997        return EOK;
     
    10111011        assert(timeptr != NULL);
    10121012        assert(buf != NULL);
    1013        
     1013
    10141014        static const char *wday[] = {
    10151015                "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    10161016        };
    1017        
     1017
    10181018        static const char *mon[] = {
    10191019                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    10201020                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    10211021        };
    1022        
     1022
    10231023        snprintf(buf, ASCTIME_BUF_LEN, "%s %s %2d %02d:%02d:%02d %d\n",
    10241024            wday[timeptr->tm_wday],
     
    10431043        // TODO: Deal with timezones.
    10441044        //       Currently assumes system and all times are in UTC
    1045        
     1045
    10461046        /* Set result to epoch. */
    10471047        result->tm_usec = 0;
     
    10521052        result->tm_mon = 0;
    10531053        result->tm_year = 70; /* 1970 */
    1054        
     1054
    10551055        if (normalize_tm_tv(result, tv) == -1)
    10561056                return EOVERFLOW;
    1057        
     1057
    10581058        return EOK;
    10591059}
     
    10971097        if (ret != EOK)
    10981098                return ret;
    1099        
     1099
    11001100        time_tm2str(&loctime, buf);
    11011101        return EOK;
Note: See TracChangeset for help on using the changeset viewer.