summaryrefslogtreecommitdiffstats
path: root/timezone/generate_timeZone.pl
blob: 8c4c7427f7d2d8f0a4d84ec282ac6086032fcefa (plain)
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
#!/usr/bin/perl
use strict;
use warnings;
use autodie;

use Text::CSV_XS ();

# Obtain timezone.txt from http://www.maxmind.com/timezone.txt

# Used to generate timeZone.c
# usage: ./generate_timeZone.pl > ../libGeoIP/timeZone.c

my $tz;

open my $fh, '<:encoding(latin1)', 'timezone.txt';

my $csv = Text::CSV_XS->new( { binary => 1, auto_diag => 1 } );

print "#include <string.h> \n";
print
    "const char* GeoIP_time_zone_by_country_and_region(const char * country,const char * region) {\n";
print "    const char* timezone = NULL;\n";
print "    if (country == NULL) {\n";
print "      return NULL;\n";
print "    }\n";
print "    if (region == NULL) {\n";
print '        region = "";', "\n";
print "    }\n";

<$fh>;    # skip first line
while ( my $row = $csv->getline($fh) ) {
    my ( $country, $region, $timezone ) = @{$row};
    die "$_ $.\n" unless $timezone;
    $tz->{$country}->{ $region || q{} } = $timezone;
}

my $first_country;

$first_country = 0;
for my $c ( sort keys %$tz ) {
    print('    if');
    $first_country ||= $c;
    my $def = delete $tz->{$c}->{q{}};
    if ( my @reg = sort keys %{ $tz->{$c} } ) {
        my @tz = map { $tz->{$c}->{$_} } @reg;

        printf( qq! ( strcmp (country, "%s") == 0 ) {\n!, $c );
        for ( 0 .. $#reg ) {

            # have regions

            print( ( $_ == 0 ) ? '        if' : '        else if' );
            printf( qq! ( strcmp (region, "%s") == 0 ) {\n!, $reg[$_] );
            printf( qq!            return "%s";\n!,                $tz[$_] );
            printf(qq!        }\n!);
        }
        if ( defined $def ) {
            printf( qq!    else { return "%s"; }\n!, $def );
        }
        else {
            print "        else {\n             return NULL;\n        }\n";
        }
        print qq[    }\n];
    }
    else {

        # only default tz
        printf( qq! ( strcmp (country, "%s") == 0 ) {\n!, $c );
        printf( qq!        return "%s";\n!,                   $def );
        printf(qq!    }\n!);

    }
}

print qq[    return timezone;\n}\n];