Skip to content

Commit

Permalink
LibTimeZone: Add an API to retrieve the system's current time zone
Browse files Browse the repository at this point in the history
This is a wrapper API around the POSIX tzset / tzname information. It
is to help ensure that tzset is invoked before accessing tzname.
  • Loading branch information
trflynn89 authored and linusg committed Jan 12, 2022
1 parent 9a7cd8f commit 8a4ac9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Userland/Libraries/LibTimeZone/TimeZone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <LibTimeZone/TimeZone.h>
#include <time.h>

namespace TimeZone {

Expand All @@ -19,6 +20,17 @@ enum class TimeZone : u16 {
};
#endif

StringView current_time_zone()
{
static bool initialized_time_zone = false;
if (!initialized_time_zone) {
initialized_time_zone = true;
tzset();
}

return tzname[0];
}

Optional<TimeZone> __attribute__((weak)) time_zone_from_string([[maybe_unused]] StringView time_zone)
{
#if !ENABLE_TIME_ZONE_DATA
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibTimeZone/TimeZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace TimeZone {

StringView current_time_zone();

Optional<TimeZone> time_zone_from_string(StringView time_zone);
StringView time_zone_to_string(TimeZone time_zone);
Optional<StringView> canonicalize_time_zone(StringView time_zone);
Expand Down

0 comments on commit 8a4ac9c

Please sign in to comment.