Changeset f7ea5400 in mainline for uspace/lib/c/generic/time.c
- Timestamp:
- 2012-08-15T17:52:09Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 16639bb
- Parents:
- 9dec6d4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/time.c
r9dec6d4 rf7ea5400 841 841 } 842 842 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 */ 851 int utctime2tm(const time_t time, struct tm *restrict result) 852 { 853 assert(result != NULL); 848 854 849 855 /* 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 */ 878 int 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 864 890 865 891 /** … … 868 894 * 869 895 * @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 */ 899 void tm2str(const struct tm *restrict timeptr, char *restrict buf) 900 { 876 901 assert(timeptr != NULL); 902 assert(buf != NULL); 877 903 878 904 static const char *wday[] = { … … 890 916 timeptr->tm_min, timeptr->tm_sec, 891 917 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 */ 929 int localtime2tm(const time_t time, struct tm *restrict result) 904 930 { 905 931 // TODO: deal with timezone 906 932 // currently assumes system and all times are in GMT 907 933 908 static struct tm result;909 910 934 /* 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. 928 952 * 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 */ 959 int 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; 939 970 } 940 971
Note:
See TracChangeset
for help on using the changeset viewer.