headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / time / localtime.cpp
blobcc4a2b49e2fcc781995ad4041332ae7a02c55ca3
1 /*
2 * Copyright 2010, Oliver Tappe, zooey@hirschkaefer.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
12 #include <syscalls.h>
14 #include <StorageDefs.h>
16 #include <errno_private.h>
17 #include "LocaleBackend.h"
20 using BPrivate::Libroot::gLocaleBackend;
21 using BPrivate::Libroot::LocaleBackend;
24 static char sStandardTZName[64] = { "GMT" };
25 static char sDaylightSavingTZName[64] = { "GMT" };
28 char* tzname[2] = {
29 sStandardTZName,
30 sDaylightSavingTZName
32 long timezone = 0;
33 int daylight = 0;
36 // These two functions are used as a fallback when the locale backend could not
37 // be loaded. They are implemented in localtime_fading_out.c.
38 extern "C" struct tm* __gmtime_r_fallback(const time_t* timep, struct tm* tmp);
39 extern "C" time_t __mktime_fallback(struct tm* tmp);
42 extern "C" void
43 tzset(void)
45 if (gLocaleBackend == NULL && LocaleBackend::LoadBackend() != B_OK)
46 return;
48 char timeZoneID[B_FILE_NAME_LENGTH] = { "GMT" };
49 _kern_get_timezone(NULL, timeZoneID, sizeof(timeZoneID));
51 gLocaleBackend->TZSet(timeZoneID, getenv("TZ"));
55 extern "C" struct tm*
56 localtime(const time_t* inTime)
58 static tm tm;
60 return localtime_r(inTime, &tm);
64 extern "C" struct tm*
65 localtime_r(const time_t* inTime, struct tm* tmOut)
67 if (inTime == NULL) {
68 __set_errno(EINVAL);
69 return NULL;
72 tzset();
73 if (gLocaleBackend != NULL) {
74 status_t status = gLocaleBackend->Localtime(inTime, tmOut);
76 if (status != B_OK)
77 __set_errno(EOVERFLOW);
79 return tmOut;
82 // without a locale backend, there are no timezones, so we fall back to
83 // using a basic gmtime_r implementation.
84 return __gmtime_r_fallback(inTime, tmOut);
88 extern "C" struct tm*
89 gmtime(const time_t* inTime)
91 static tm tm;
93 return gmtime_r(inTime, &tm);
97 extern "C" struct tm*
98 gmtime_r(const time_t* inTime, struct tm* tmOut)
100 if (inTime == NULL) {
101 __set_errno(EINVAL);
102 return NULL;
105 tzset();
106 if (gLocaleBackend != NULL) {
107 status_t status = gLocaleBackend->Gmtime(inTime, tmOut);
109 if (status != B_OK)
110 __set_errno(EOVERFLOW);
112 return tmOut;
115 // without a locale backend, we fall back to using a basic gmtime_r
116 // implementation.
117 return __gmtime_r_fallback(inTime, tmOut);
121 extern "C" time_t
122 mktime(struct tm* inTm)
124 if (inTm == NULL) {
125 __set_errno(EINVAL);
126 return -1;
129 tzset();
130 if (gLocaleBackend != NULL) {
131 time_t timeOut;
132 status_t status = gLocaleBackend->Mktime(inTm, timeOut);
134 if (status != B_OK) {
135 __set_errno(EOVERFLOW);
136 return -1;
139 return timeOut;
142 // without a locale backend, we fall back to using a basic gmtime_r
143 // implementation.
144 return __mktime_fallback(inTm);