Skip to content

Commit 07f192c

Browse files
authored
[contrib/timezone-gen] Fix timezone gen (signalwire#2215)
* [contrib/timezone-gen] Move timezone-gen.pl to own folder * [contrib/timezone-gen] Add fixTzstr * [contrib/timezone-gen] Add tests and zone data getter - tests.pl can be used to verify that the generated timezone conf will produce the correct datetimes by testing them against what the system's `date` says - build-zonedata.pl will download the latest tzdb data and build the posix timezone data files. It only builds what is needed rather than adding extraneous "right/" and "posix/" timezones. FreeSWITCH doesn't seem to be able to use the "right/" timezone files. - data/ is where the various files needed to generate the timezones gets stored
1 parent 0df47be commit 07f192c

File tree

5 files changed

+165
-3
lines changed

5 files changed

+165
-3
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
my $remote_version = `wget --quiet https://data.iana.org/time-zones/tzdb/version --output-document -` =~ s/\n//r;
7+
my $local_version;
8+
9+
if ( open my $in, "<data/version" ) {
10+
$local_version = do { local $/; <$in> };
11+
close $in;
12+
}
13+
14+
my $up_to_date = defined($local_version) && $local_version eq $remote_version;
15+
16+
if ( ! $up_to_date ) {
17+
open my $out, ">data/version";
18+
print $out $remote_version;
19+
close $out;
20+
}
21+
22+
$local_version = $remote_version;
23+
24+
`wget --quiet --timestamping --directory-prefix=data https://data.iana.org/time-zones/tzdb-latest.tar.lz`;
25+
`tar --extract --file=data/tzdb-latest.tar.lz --directory=data`;
26+
`make DESTDIR=../ TZDIR=zones-$local_version --directory=data/tzdb-$local_version posix_only`;
27+
28+
print("Yay. Now you can run\n ./timezone-gen.pl --base=data/zones-$local_version --output=timezones-$local_version.conf.xml")
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tzdb-*
2+
zones-*
3+
version
4+
tzdb-latest.tar.lz

scripts/perl/timezones/fix-tzstr.pl

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/perl
2+
3+
sub fixTzstr {
4+
# switch_time.c expects POSIX-style TZ rule, but it won't process quoted TZ
5+
# rules that look like this: <-04>4 or <-04>4<-03>
6+
# See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
7+
8+
# Instead it defaults to UTC for these values. Here we process the quoted
9+
# values and convert them into letters. If the zone name has "GMT", we use
10+
# that as the replacement prefix, otherwise a default "STD" is used. Zones
11+
# that have a quoted suffix have their suffix replaced with "DST".
12+
13+
my ($tzstr, $name) = @_;
14+
15+
if ( $tzstr =~ /(<(?<std>[^>]+)>)([^<]+)(?<dst><.+>)?(?<rest>.+)?/ ) {
16+
my ($tzprefix, $tzsuffix, $tzrest, $offset, $offsetprefix) = ("") x 5;
17+
18+
if ( defined($+{std}) ) {
19+
my $std = $+{std};
20+
21+
if ( lc($name) =~ m/gmt/) {
22+
$tzprefix = "GMT";
23+
} else {
24+
$tzprefix = "STD";
25+
}
26+
27+
if ( $std =~ m/\+/ ) {
28+
$offset = sprintf "%d", $std =~ s/\+//r;
29+
$offsetprefix = "-";
30+
} else {
31+
$offset = sprintf "%d", $std =~ s/\-//r;
32+
}
33+
34+
my @chars = split(//, $offset);
35+
if ( @chars > 2 ) {
36+
my $hours = $chars[-3];
37+
if ( defined( $chars[-4] ) ) {
38+
$hours = $chars[-4].$hours;
39+
}
40+
41+
$offset = $hours.":".$chars[-2].$chars[-1];
42+
}
43+
44+
$offset = $offsetprefix.$offset;
45+
}
46+
47+
if ( defined($+{dst}) ) {
48+
$tzsuffix = "DST";
49+
}
50+
51+
if ( defined($+{rest}) ) {
52+
$tzrest = $+{rest};
53+
}
54+
55+
return $tzprefix.$offset.$tzsuffix.$tzrest;
56+
}
57+
58+
return $tzstr;
59+
}
60+
61+
1;

scripts/perl/timezones/tests.pl

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/perl
2+
=pod
3+
Tests to verify that the provided modifications to timezone formats produce
4+
the correct results. The first set of tests verify the fixTzstr subroutine
5+
converts the quoted values to something that won't make FreeSWITCH default to
6+
UTC.
7+
8+
The second set of tests confirms that those timezone changes actually produce
9+
the correct timestamps.
10+
11+
Make sure FreeSWITCH already has already loaded the timezones.conf.xml that you
12+
want to test.
13+
14+
To run tests:
15+
16+
TIMEZONES_XML_PATH=path/to/timezones.conf.xml prove tests.pl
17+
=cut
18+
19+
use strict;
20+
use warnings;
21+
use Test::More;
22+
use ESL;
23+
use XML::LibXML::Reader;
24+
25+
require "./fix-tzstr.pl";
26+
27+
use Env qw(TIMEZONES_XML_PATH);
28+
die "The TIMEZONES_XML_PATH environment variable must be set to test timezones." unless ( defined($TIMEZONES_XML_PATH) );
29+
30+
ok( fixTzstr("<-02>2", "doesntmatterhere") eq "STD2" );
31+
ok( fixTzstr("EST5EDT,M3.2.0,M11.1.0", "US/Eastern") eq "EST5EDT,M3.2.0,M11.1.0" );
32+
ok( fixTzstr("<+11>-11", "GMT-11") eq "GMT-11" );
33+
ok( fixTzstr("<-02>2<-01>,M3.5.0/-1,M10.5.0/0", "America/Godthab") eq "STD2DST,M3.5.0/-1,M10.5.0/0" );
34+
35+
my $test_count = 4;
36+
37+
my $tz_fmt = "%Y-%m-%d %H:%M:%S";
38+
my $c = new ESL::ESLconnection("127.0.0.1", "8021", "ClueCon");
39+
$c->api("reloadxml")->getBody();
40+
my $epoch = $c->api("strepoch")->getBody();
41+
run_tests($epoch);
42+
run_tests("1699613236"); # testing DST, add more epochs as needed
43+
44+
sub run_tests {
45+
my $epoch = shift;
46+
my $reader = XML::LibXML::Reader->new(location => $TIMEZONES_XML_PATH);
47+
while ($reader->read) {
48+
my $tag = $reader;
49+
if ( $tag->name eq "zone" && $tag->hasAttributes() ) {
50+
my $zn = $tag->getAttribute("name");
51+
52+
my $cmd = `TZ='$zn' date +'$tz_fmt' --date='\@$epoch'`;
53+
my $sys_time = $cmd =~ s/\n//r;
54+
my $fs_time = $c->api("strftime_tz $zn $epoch|$tz_fmt")->getBody();
55+
56+
ok ( $sys_time eq $fs_time, $zn ) or diag(
57+
" (sys) $sys_time\t(fs) $fs_time"
58+
);
59+
60+
$test_count++;
61+
}
62+
}
63+
}
64+
65+
done_testing($test_count);

scripts/perl/timezone-gen.pl renamed to scripts/perl/timezones/timezone-gen.pl

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/perl
22

33
use strict;
4+
use warnings;
45
use Getopt::Long;
56
use XML::Entities;
67
use HTML::Entities;
78

9+
require "./fix-tzstr.pl";
810

911
my $base = "/usr/share/zoneinfo";
1012
my $output = "timezones.conf.xml";
@@ -18,7 +20,7 @@
1820
"base=s" => \$base,
1921
"debug+" => \$debug,
2022
"help" => \$help,
21-
"output" => \$output
23+
"output=s" => \$output
2224
);
2325
if ( !$res || $help ) {
2426
print "$0 [--base=/usr/share/zoneinfo] [--output=timezones.conf.xml] [--debug] [--help]\n";
@@ -64,7 +66,9 @@
6466
next;
6567
}
6668

67-
$zones{$name} = pop(@strings);
69+
my $tzstr = fixTzstr( pop(@strings), $name );
70+
71+
$zones{$name} = $tzstr;
6872
}
6973

7074
open( my $out, ">$output" );
@@ -83,7 +87,7 @@
8387
}
8488
$lastprefix = $newprefix;
8589

86-
print $out "\t<zone name=\"$zone\" value=\"$str\" />\n";
90+
print $out " " x 8, "<zone name=\"$zone\" value=\"$str\" />\n";
8791
}
8892
print $out " " x 4, "</timezones>\n";
8993
print $out "</configuration>\n";

0 commit comments

Comments
 (0)