Usage of /etc/timezone is deprecated. The tzdata maintainers recommend
switching to timedatectl.

This adds timezone handling to the Systemd module and marks the
functions in the INotify module as deprecated.

Suggested-by: Fabian Grünbichler <[email protected]>
Suggested-by: Maximiliano Sandroval <[email protected]>
Tested-by: Stoiko Ivanov <[email protected]>
Reviewed-by: Stoiko Ivanov <[email protected]>
Signed-off-by: Stefan Mayr <[email protected]>
---
 src/PVE/INotify.pm |  2 ++
 src/PVE/Systemd.pm | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm
index 62b3ca8..fdec8f1 100644
--- a/src/PVE/INotify.pm
+++ b/src/PVE/INotify.pm
@@ -651,6 +651,7 @@ register_file(
     \&update_etc_resolv_conf,
 );
 
+# Deprecated: use PVE::Systemd::get_timezone() instead
 sub read_etc_timezone {
     my ($filename, $fd) = @_;
 
@@ -661,6 +662,7 @@ sub read_etc_timezone {
     return $timezone;
 }
 
+# Deprecated: use PVE::Systemd::set_timezone($timezone) instead
 sub write_etc_timezone {
     my ($filename, $fh, $timezone) = @_;
 
diff --git a/src/PVE/Systemd.pm b/src/PVE/Systemd.pm
index 6ff0dc8..aabb774 100644
--- a/src/PVE/Systemd.pm
+++ b/src/PVE/Systemd.pm
@@ -10,7 +10,8 @@ use Net::DBus::Reactor;
 use POSIX qw(EINTR);
 use Socket qw(SOCK_DGRAM);
 
-use PVE::Tools qw(file_set_contents file_get_contents trim);
+use PVE::Exception qw(raise_param_exc);
+use PVE::Tools qw(file_set_contents file_get_contents run_command trim);
 
 sub escape_unit {
     my ($val, $is_path) = @_;
@@ -285,6 +286,38 @@ sub write_ini {
     file_set_contents($filename, $content);
 }
 
+# Use systemds timedatectl for managing timezone settings
+sub get_timezone {
+    my $timezone;
+
+    PVE::Tools::run_command(
+        ['timedatectl', 'show', '--property=Timezone', '--value'],
+        outfunc => sub { $timezone //= shift },
+    );
+
+    return $timezone;
+}
+
+sub set_timezone {
+    my ($timezone) = @_;
+
+    raise_param_exc({ 'timezone' => "No such timezone" })
+        if (!grep { $_ eq $timezone } list_timezones());
+
+    PVE::Tools::run_command(['timedatectl', 'set-timezone', $timezone]);
+}
+
+sub list_timezones {
+    my @timezones = ();
+
+    PVE::Tools::run_command(
+        ['timedatectl', 'list-timezones'],
+        outfunc => sub { push(@timezones, shift); },
+    );
+
+    return @timezones;
+}
+
 =head3 notify()
 
 This is a pure Perl reimplementation of systemd's C<sd_notify()> mechanism as 
defined in
-- 
2.34.1



Reply via email to