1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
# vim: set et ts=4 sw=4:
package AdminPanel::Shared::TimeZone;
#============================================================= -*-perl-*-
=head1 NAME
AdminPanel::Shared::TimeZone - module to manage TimeZone settings
=head1 SYNOPSIS
my $tz = AdminPanel::Shared::TimeZone->new();
=head1 DESCRIPTION
This module allows to manage time zone settings.
=head1 SUPPORT
You can find documentation for this module with the perldoc command:
perldoc AdminPanel::Shared::TimeZone
=head1 AUTHOR
Angelo Naselli <anaselli@linux.it>
=head1 COPYRIGHT and LICENSE
Copyright (C) 2014, Angelo Naselli.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=head1 METHODS
=cut
use diagnostics;
use strict;
use Moose;
use DateTime::TimeZone;
use Config::Auto;
use AdminPanel::Shared::Locales;
#=============================================================
=head2 new - optional parameters
=head3 timezone_prefix
optional parameter to set the system timezone directory,
default value is /usr/share/zoneinfo
=cut
#=============================================================
has 'timezone_prefix' => (
is => 'rw',
isa => 'Str',
default => "/usr/share/zoneinfo",
);
#=============================================================
=head2 new - optional parameters
=head3 clock_configuration_file
optional parameter to set the clock system configuration file,
default value is /etc/sysconfig/clock
=cut
#=============================================================
has 'clock_configuration_file' => (
is => 'rw',
isa => 'Str',
default => "/etc/sysconfig/clock",
);
#=== global ===
has 'loc' => (
is => 'rw',
lazy => 1,
init_arg => undef,
builder => '_localeInitialize'
);
sub _localeInitialize {
my $self = shift;
# TODO fix domain binding for translation
$self->loc(AdminPanel::Shared::Locales->new(domain_name => 'libDrakX') );
# TODO if we want to give the opportunity to test locally add dir_name => 'path'
}
has 'ntp_servers' => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef',
lazy => 1,
handles => {
get_ntp_server => 'get',
ntp_server_pairs => 'kv',
},
init_arg => undef,
builder => '_buildNTPServers'
);
sub _buildNTPServers {
my $self = shift;
my %ntpServersHash;
$ntpServersHash{"-"} = {
$self->loc->N_("Global") => "pool.ntp.org",
};
$ntpServersHash{Global} = {
$self->loc->N_("Africa") => "africa.pool.ntp.org",
$self->loc->N_("Asia") => "asia.pool.ntp.org",
$self->loc->N_("Europe") => "europe.pool.ntp.org",
$self->loc->N_("North America") => "north-america.pool.ntp.org",
$self->loc->N_("Oceania") => "oceania.pool.ntp.org",
$self->loc->N_("South America") => "south-america.pool.ntp.org",
};
$ntpServersHash{Africa} = {
$self->loc->N_("South Africa") => "za.pool.ntp.org",
$self->loc->N_("Tanzania") => "tz.pool.ntp.org",
};
$ntpServersHash{Asia} = {
$self->loc->N_("Bangladesh") => "bd.pool.ntp.org",
$self->loc->N_("China") => "cn.pool.ntp.org",
$self->loc->N_("Hong Kong") => "hk.pool.ntp.org",
$self->loc->N_("India") => "in.pool.ntp.org",
$self->loc->N_("Indonesia") => "id.pool.ntp.org",
$self->loc->N_("Iran") => "ir.pool.ntp.org",
$self->loc->N_("Israel") => "il.pool.ntp.org",
$self->loc->N_("Japan") => "jp.pool.ntp.org",
$self->loc->N_("Korea") => "kr.pool.ntp.org",
$self->loc->N_("Malaysia") => "my.pool.ntp.org",
$self->loc->N_("Philippines") => "ph.pool.ntp.org",
$self->loc->N_("Singapore") => "sg.pool.ntp.org",
$self->loc->N_("Taiwan") => "tw.pool.ntp.org",
$self->loc->N_("Thailand") => "th.pool.ntp.org",
$self->loc->N_("Turkey") => "tr.pool.ntp.org",
$self->loc->N_("United Arab Emirates") => "ae.pool.ntp.org",
};
$ntpServersHash{Europe} = {
$self->loc->N_("Austria") => "at.pool.ntp.org",
$self->loc->N_("Belarus") => "by.pool.ntp.org",
$self->loc->N_("Belgium") => "be.pool.ntp.org",
$self->loc->N_("Bulgaria") => "bg.pool.ntp.org",
$self->loc->N_("Czech Republic") => "cz.pool.ntp.org",
$self->loc->N_("Denmark") => "dk.pool.ntp.org",
$self->loc->N_("Estonia") => "ee.pool.ntp.org",
$self->loc->N_("Finland") => "fi.pool.ntp.org",
$self->loc->N_("France") => "fr.pool.ntp.org",
$self->loc->N_("Germany") => "de.pool.ntp.org",
$self->loc->N_("Greece") => "gr.pool.ntp.org",
$self->loc->N_("Hungary") => "hu.pool.ntp.org",
$self->loc->N_("Ireland") => "ie.pool.ntp.org",
$self->loc->N_("Italy") => "it.pool.ntp.org",
$self->loc->N_("Lithuania") => "lt.pool.ntp.org",
$self->loc->N_("Luxembourg") => "lu.pool.ntp.org",
$self->loc->N_("Netherlands") => "nl.pool.ntp.org",
$self->loc->N_("Norway") => "no.pool.ntp.org",
$self->loc->N_("Poland") => "pl.pool.ntp.org",
$self->loc->N_("Portugal") => "pt.pool.ntp.org",
$self->loc->N_("Romania") => "ro.pool.ntp.org",
$self->loc->N_("Russian Federation") => "ru.pool.ntp.org",
$self->loc->N_("Slovakia") => "sk.pool.ntp.org",
$self->loc->N_("Slovenia") => "si.pool.ntp.org",
$self->loc->N_("Spain") => "es.pool.ntp.org",
$self->loc->N_("Sweden") => "se.pool.ntp.org",
$self->loc->N_("Switzerland") => "ch.pool.ntp.org",
$self->loc->N_("Ukraine") => "ua.pool.ntp.org",
$self->loc->N_("United Kingdom") => "uk.pool.ntp.org",
$self->loc->N_("Yugoslavia") => "yu.pool.ntp.org",
};
$ntpServersHash{"North America"} = {
$self->loc->N_("Canada") => "ca.pool.ntp.org",
$self->loc->N_("Guatemala") => "gt.pool.ntp.org",
$self->loc->N_("Mexico") => "mx.pool.ntp.org",
$self->loc->N_("United States") => "us.pool.ntp.org",
};
$ntpServersHash{Oceania} = {
$self->loc->N_("Australia") => "au.pool.ntp.org",
$self->loc->N_("New Zealand") => "nz.pool.ntp.org",
};
$ntpServersHash{"South America"} = {
$self->loc->N_("Argentina") => "ar.pool.ntp.org",
$self->loc->N_("Brazil") => "br.pool.ntp.org",
$self->loc->N_("Chile") => "cl.pool.ntp.org",
};
return \%ntpServersHash;
}
#=============================================================
=head2 get_timezone_prefix
=head3 OUTPUT
timezone_prefix: directory in which time zone files are
=head3 DESCRIPTION
Return the timezone directory (defualt: /usr/share/zoneinfo)
=cut
#=============================================================
sub get_timezone_prefix {
my $self = shift;
return $self->timezone_prefix;
}
#=============================================================
=head2 getTimeZones
=head3 INPUT
$from_system: if present and its value is not 0 checks into timezone_prefix
directory and gets the list from there
=head3 OUTPUT
@l: ARRAY containing sorted time zones
=head3 DESCRIPTION
This method returns the available timezones
=cut
#=============================================================
sub getTimeZones {
my ($self, $from_system) = @_;
if ($from_system and $from_system != 0) {
require MDK::Common::DataStructure;
require MDK::Common::Various;
my $tz_prefix = $self->get_timezone_prefix();
open(my $F, "cd $tz_prefix && find [A-Z]* -noleaf -type f |");
my @l = MDK::Common::DataStructure::difference2([ MDK::Common::Various::chomp_(<$F>) ], [ 'ROC', 'PRC' ]);
close $F or die "cannot list the available zoneinfos";
return sort @l;
}
return DateTime::TimeZone->all_names;
}
#=============================================================
=head2 readConfiguration
=head3 OUTPUT
hash containing:
UTC => HW clock is set as UTC
ZONE => Time Zone set
=head3 DESCRIPTION
This method returns the time zone system settings as hash
reference
=cut
#=============================================================
sub readConfiguration {
my $self = shift;
my $prefs = {};
if (-e $self->clock_configuration_file) {
$prefs = Config::Auto::parse($self->clock_configuration_file);
}
return $prefs;
}
sub _get_ntp_server_tree {
my ($self, $zone) = @_;
$zone = "-" if ! $zone;
my $ns = $self->get_ntp_server($zone);
return if !$ns;
map {
$ns->{$_} => (
$self->get_ntp_server($_) ?
$zone ?
$self->loc->N($_) . "|" . $self->loc->N("All servers") :
$self->loc->N("All servers") :
$self->loc->N($zone) . "|" . $self->loc->N($_)
),
$self->_get_ntp_server_tree($_)
} keys $ns;
}
#=============================================================
=head2 ntpServers
=head3 OUTPUT
HASHREF containing ntp_server => zone info
=head3 DESCRIPTION
This method returns an hash ref containing pairs ntp-server, zone
=cut
#=============================================================
sub ntpServers {
my ($self) = @_;
# FIXME: missing parameter:
+{$self->_get_ntp_server_tree()};
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
|