Changeset f7ea5400 in mainline for uspace/lib/c/generic/time.c


Ignore:
Timestamp:
2012-08-15T17:52:09Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
16639bb
Parents:
9dec6d4
Message:

Replace the gmtime(), asctime() localtime() and ctime() with a new set of reentrant functions.
The former will be kept in the posix library for compatibility reasons.

File:
1 edited

Legend:

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

    r9dec6d4 rf7ea5400  
    841841}
    842842
    843 struct tm *gmtime(const time_t *timer)
    844 {
    845         assert(timer != NULL);
    846 
    847         static struct tm result;
     843
     844/** Converts a time value to a broken-down UTC time
     845 *
     846 * @param time    Time to convert
     847 * @param result  Structure to store the result to
     848 *
     849 * @return        EOK or a negative error code
     850 */
     851int utctime2tm(const time_t time, struct tm *restrict result)
     852{
     853        assert(result != NULL);
    848854
    849855        /* Set result to epoch. */
    850         result.tm_sec = 0;
    851         result.tm_min = 0;
    852         result.tm_hour = 0;
    853         result.tm_mday = 1;
    854         result.tm_mon = 0;
    855         result.tm_year = 70; /* 1970 */
    856 
    857         if (_normalize_time(&result, *timer) == -1) {
    858                 errno = EOVERFLOW;
    859                 return NULL;
    860         }
    861 
    862         return &result;
    863 }
     856        result->tm_sec = 0;
     857        result->tm_min = 0;
     858        result->tm_hour = 0;
     859        result->tm_mday = 1;
     860        result->tm_mon = 0;
     861        result->tm_year = 70; /* 1970 */
     862
     863        if (_normalize_time(result, time) == -1)
     864                return EOVERFLOW;
     865
     866        return EOK;
     867}
     868
     869/** Converts a time value to a null terminated string of the form
     870 *  "Wed Jun 30 21:49:08 1993\n" expressed in UTC.
     871 *
     872 * @param time   Time to convert.
     873 * @param buf    Buffer to store the string to, must be at least
     874 *               ASCTIME_BUF_LEN bytes long.
     875 *
     876 * @return       EOK or a negative error code.
     877 */
     878int utctime2str(const time_t time, char *restrict buf)
     879{
     880        struct tm t;
     881        int r;
     882
     883        if ((r = utctime2tm(time, &t)) != EOK)
     884                return r;
     885
     886        tm2str(&t, buf);
     887        return EOK;
     888}
     889
    864890
    865891/**
     
    868894 *
    869895 * @param timeptr Broken-down time structure.
    870  * @return Pointer to a statically allocated string.
    871  */
    872 char *asctime(const struct tm *timeptr)
    873 {
    874         static char buf[ASCTIME_BUF_LEN];
    875 
     896 * @param buf     Buffer to store string to, must be at least ASCTIME_BUF_LEN
     897 *                bytes long.
     898 */
     899void tm2str(const struct tm *restrict timeptr, char *restrict buf)
     900{
    876901        assert(timeptr != NULL);
     902        assert(buf != NULL);
    877903
    878904        static const char *wday[] = {
     
    890916            timeptr->tm_min, timeptr->tm_sec,
    891917            1900 + timeptr->tm_year);
    892 
    893         return buf;
    894 
    895 }
    896 
    897 /**
    898  * Converts a time value to a broken-down local time.
    899  *
    900  * @param timer Time to convert.
    901  * @return Normalized broken-down time in local timezone, NULL on overflow.
    902  */
    903 struct tm *localtime(const time_t *timer)
     918}
     919
     920/**
     921 * Converts a time value to a broken-down local time, expressed relative
     922 * to the user's specified timezone.
     923 *
     924 * @param timer     Time to convert.
     925 * @param result    Structure to store the result to.
     926 *
     927 * @return          EOK on success or a negative error code.
     928 */
     929int localtime2tm(const time_t time, struct tm *restrict result)
    904930{
    905931        // TODO: deal with timezone
    906932        // currently assumes system and all times are in GMT
    907933
    908         static struct tm result;
    909 
    910934        /* Set result to epoch. */
    911         result.tm_sec = 0;
    912         result.tm_min = 0;
    913         result.tm_hour = 0;
    914         result.tm_mday = 1;
    915         result.tm_mon = 0;
    916         result.tm_year = 70; /* 1970 */
    917 
    918         if (_normalize_time(&result, *timer) == -1) {
    919                 errno = EOVERFLOW;
    920                 return NULL;
    921         }
    922 
    923         return &result;
    924 }
    925 
    926 /**
    927  * Equivalent to asctime(localtime(clock)).
     935        result->tm_sec = 0;
     936        result->tm_min = 0;
     937        result->tm_hour = 0;
     938        result->tm_mday = 1;
     939        result->tm_mon = 0;
     940        result->tm_year = 70; /* 1970 */
     941
     942        if (_normalize_time(result, time) == -1)
     943                return EOVERFLOW;
     944
     945        return EOK;
     946}
     947
     948/**
     949 * Converts the calendar time to a null terminated string
     950 * of the form "Wed Jun 30 21:49:08 1993\n" expressed relative to the
     951 * user's specified timezone.
    928952 *
    929  * @param timer Time to convert.
    930  * @return Pointer to a statically allocated string holding the date.
    931  */
    932 char *ctime(const time_t *timer)
    933 {
    934         struct tm *loctime = localtime(timer);
    935         if (loctime == NULL) {
    936                 return NULL;
    937         }
    938         return asctime(loctime);
     953 * @param timer  Time to convert.
     954 * @param buf    Buffer to store the string to. Must be at least
     955 *               ASCTIME_BUF_LEN bytes long.
     956 *
     957 * @return       EOK on success or a negative error code.
     958 */
     959int localtime2str(const time_t time, char *buf)
     960{
     961        struct tm loctime;
     962        int r;
     963
     964        if ((r = localtime2tm(time, &loctime)) != EOK)
     965                return r;
     966
     967        tm2str(&loctime, buf);
     968
     969        return EOK;
    939970}
    940971
Note: See TracChangeset for help on using the changeset viewer.