From 8a4ac9c387c9b2cca61ea3b2384dda8b1f7d93e6 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 11 Jan 2022 23:21:36 -0500 Subject: [PATCH] LibTimeZone: Add an API to retrieve the system's current time zone This is a wrapper API around the POSIX tzset / tzname information. It is to help ensure that tzset is invoked before accessing tzname. --- Userland/Libraries/LibTimeZone/TimeZone.cpp | 12 ++++++++++++ Userland/Libraries/LibTimeZone/TimeZone.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/Userland/Libraries/LibTimeZone/TimeZone.cpp b/Userland/Libraries/LibTimeZone/TimeZone.cpp index b87f11fc3a..251f84c11c 100644 --- a/Userland/Libraries/LibTimeZone/TimeZone.cpp +++ b/Userland/Libraries/LibTimeZone/TimeZone.cpp @@ -5,6 +5,7 @@ */ #include +#include namespace TimeZone { @@ -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 __attribute__((weak)) time_zone_from_string([[maybe_unused]] StringView time_zone) { #if !ENABLE_TIME_ZONE_DATA diff --git a/Userland/Libraries/LibTimeZone/TimeZone.h b/Userland/Libraries/LibTimeZone/TimeZone.h index 2fe55583a8..d7c5ef4c13 100644 --- a/Userland/Libraries/LibTimeZone/TimeZone.h +++ b/Userland/Libraries/LibTimeZone/TimeZone.h @@ -14,6 +14,8 @@ namespace TimeZone { +StringView current_time_zone(); + Optional time_zone_from_string(StringView time_zone); StringView time_zone_to_string(TimeZone time_zone); Optional canonicalize_time_zone(StringView time_zone);