diff options
Diffstat (limited to 'perl-install/timezone.pm')
| -rw-r--r-- | perl-install/timezone.pm | 390 | 
1 files changed, 250 insertions, 140 deletions
| diff --git a/perl-install/timezone.pm b/perl-install/timezone.pm index 6a3d86f99..cfbaf98a1 100644 --- a/perl-install/timezone.pm +++ b/perl-install/timezone.pm @@ -1,4 +1,4 @@ -package timezone; # $Id$ +package timezone;  use diagnostics;  use strict; @@ -6,172 +6,282 @@ use strict;  use common;  use log; +sub get_timezone_prefix { +    my ($b_use_system_prefix) = @_; -sub getTimeZones { -    my ($prefix) = @_; -    open(my $F, "cd $prefix/usr/share/zoneinfo && find [A-Z]* -type f |"); -    my @l = chomp_(<$F>); +    my $prefix = ($::testing || $b_use_system_prefix) ? '' : $::prefix; +    $prefix . "/usr/share/zoneinfo"; +} + +sub getTimeZones() { +    my $tz_prefix = get_timezone_prefix(); +    open(my $F, "cd $tz_prefix && find [A-Z]* -noleaf -type f |"); +    my @l = difference2([ chomp_(<$F>) ], [ 'ROC', 'PRC' ]);      close $F or die "cannot list the available zoneinfos";      sort @l;  } -sub read { +sub read() {      my %t = getVarsFromSh("$::prefix/etc/sysconfig/clock") or return {};      { timezone => $t{ZONE}, UTC => text2bool($t{UTC}) };  } -sub ntp_server { -    my ($prefix, $server) = @_; - -    my $f = "$prefix/etc/ntp.conf"; -    -e $f or return; - -    if (@_ > 1) { -	my $added = 0; -	substInFile { -	    if (/^#?\s*server\s+(\S*)/ && $1 ne '127.127.1.0') { -		$_ = $added ? "#server $1\n" : "server $server\n"; -		$added = 1; -	    } -	} $f; -	output("$prefix/etc/ntp/step-tickers", "$server\n"); -    } else { -	($server) = grep { $_ ne '127.127.1.0' } map { if_(/^\s*server\s+(\S*)/, $1) } cat_($f); +our $ntp = "ntp"; +my $servername_config_suffix = ""; +unless (-f $::prefix . "/etc/" . $ntp . ".conf") { +    $ntp = "chrony"; +    $servername_config_suffix = " iburst"; +} + +sub ntp_server() { +    find { $_ ne '127.127.1.0' } map { if_(/^\s*server\s+(\S*)/, $1) } cat_($::prefix . "/etc/" . $ntp . ".conf"); +} + +sub set_ntp_server { +    my ($server) = @_; +    my $f = $::prefix . "/etc/" . $ntp . ".conf"; +    -f $f or return; + +    my $pool_match = qr/\.pool\.ntp\.org$/; +    my @servers = $server =~ $pool_match  ? (map { "$_.$server" } 0 .. 2) : $server; + +    my $added = 0; +    substInFile { +        if (/^#?\s*server\s+(\S*)/ && $1 ne '127.127.1.0') { +            $_ = $added ? $_ =~ $pool_match ? undef : "#server $1\n" : join('', map { "server $_$servername_config_suffix\n" } @servers); +            $added = 1; +        } +    } $f; +    if ($ntp eq "ntp") { +	output_p("$::prefix/etc/ntp/step-tickers", join('', map { "$_\n" } @servers));      } -    $server; + +    require services; +    services::set_status($ntp . 'd', to_bool($server), $::isInstall);  }  sub write { -    my ($prefix, $t) = @_; +    my ($t) = @_; -    ntp_server($prefix, $t->{ntp}); +    set_ntp_server($t->{ntp}); -    eval { cp_af("$prefix/usr/share/zoneinfo/$t->{timezone}", "$prefix/etc/localtime") }; +    my $tz_prefix = get_timezone_prefix(1); +    eval { symlinkf($tz_prefix . '/' . $t->{timezone}, "$::prefix/etc/localtime") };      $@ and log::l("installing /etc/localtime failed"); -    setVarsInSh("$prefix/etc/sysconfig/clock", { +    setVarsInSh("$::prefix/etc/sysconfig/clock", {  	ZONE => $t->{timezone},  	UTC  => bool2text($t->{UTC}),  	ARC  => "false",      }); + +    my $adjtime_file = $::prefix . '/etc/adjtime'; +    my @adjtime = cat_($adjtime_file); +    @adjtime or @adjtime = ("0.0 0 0.0\n", "0\n"); +    $adjtime[2] = $t->{UTC} ? "UTC\n" : "LOCAL\n"; +    output_p($adjtime_file, @adjtime); +} + +sub reload_sys_clock { +    my ($t) = @_; +    require run_program; +    any::disable_x_screensaver(); +    run_program::run('hwclock', '--hctosys', ($t->{UTC} ? '--utc' : '--localtime')); +    any::enable_x_screensaver();  } -my %l2t = ( -'Afrikaans (South Africa)' => 'Africa/Johannesburg', -'Arabic' => 'Africa/Cairo', -'Armenian (Armenia)' => 'Asia/Yerevan', -'Azeri (Azerbaijan)' => 'Asia/Baku', -'Belarussian (Belarus)' => 'Europe/Minsk', -'Bosnian (Bosnia)' => 'Europe/Sarajevo', -'Brezhoneg (Brittany)' => 'Europe/Paris', -'Bulgarian (Bulgaria)' => 'Europe/Sofia', -'Catalan' => 'Europe/Madrid', -'Chinese Traditional (Taiwan)' => 'Asia/Taipei', -'Chinese Simplified (China)' => 'Asia/Beijing', -'Croatian (Bosnia)' => 'Europe/Sarajevo', -'Croatian (Croatia)' => 'Europe/Zagreb', -'Cymraeg (Welsh)' => 'Europe/London', -'Czech' => 'Europe/Prague', -'Danish (Denmark)' => 'Europe/Copenhagen', -'Dutch (Netherlands)' => 'Europe/Amsterdam', -'English (United States)' => 'America/New_York', -'English (United Kingdom)' => 'Europe/London', -'Esperanto' => 'Europe/Warsaw', -'Estonian (Estonia)' => 'Europe/Tallinn', -'Euskara (Basque)' => 'Europe/Madrid', -'Finnish (Finland)' => 'Europe/Helsinki', -'French (France)' => 'Europe/Paris', -'French (Belgium)' => 'Europe/Brussels', -'French (Canada)' => 'Canada/Atlantic', # or Newfoundland ? or Eastern ? -'Gaeilge (Ireland)' => 'Europe/Dublin', -'Galego' => 'Europe/Madrid', -'Georgian (Georgia)' => 'Asia/Yerevan', -'German (Austria)' => 'Europe/Vienna', -'German (Germany)' => 'Europe/Berlin', -'Greek (Greece)' => 'Europe/Athens', -'Greenlandic' => 'Arctic/Longyearbyen', -'Hebrew (Israel)' => 'Asia/Tel_Aviv', -'Hungarian (Hungary)' => 'Europe/Budapest', -'Icelandic (Iceland)' => 'Atlantic/Reykjavik', -'Indonesian (Indonesia)' => 'Asia/Jakarta', -'Iranian (Iran)' => 'Asia/Tehran', -'Italian (Italy)' => 'Europe/Rome', -#-'Italian (San Marino)' => 'Europe/San_Marino', -#-'Italian (Vatican)' => 'Europe/Vatican', -#-'Italian (Switzerland)' => 'Europe/Zurich', -'Japanese (Japon)' => 'Asia/Tokyo', -'Korean (Korea)' => 'Asia/Seoul', -'Latvian (Latvia)' => 'Europe/Riga', -'Lithuanian (Lithuania)' => 'Europe/Vilnius', -'Macedonian (Macedonia)' => 'Europe/Skopje', -'Maori (New Zealand)' => 'Australia/Sydney', -'Norwegian (Bokmaal)' => 'Europe/Oslo', -'Norwegian (Nynorsk)' => 'Europe/Oslo', -'Polish (Poland)' => 'Europe/Warsaw', -'Portuguese (Brazil)' => 'Brazil/East', # most people live on the east coast -'Portuguese (Portugal)' => 'Europe/Lisbon', -'Romanian (Rumania)' => 'Europe/Bucharest', -'Russian (Russia)' => 'Europe/Moscow', -'Serbian (Serbia)' => 'Europe/Belgrade', -'Slovak (Slovakia)' => 'Europe/Bratislava', -'Slovenian (Slovenia)' => 'Europe/Ljubljana', -'Spanish (Argentina)' => 'America/Buenos_Aires', -'Spanish (Mexico)' => 'America/Mexico_City', -'Spanish (Spain)' => 'Europe/Madrid', -'Swedish (Sweden)' => 'Europe/Stockholm', -'Tajik (Tajikistan)' => 'Asia/Dushanbe', -'Tamil (Sri Lanka)' => 'Asia/Colombo', -'Tatar' => 'Europe/Minsk', -'Thai (Thailand)' => 'Asia/Bangkok', -'Turkish (Turkey)' => 'Europe/Istanbul', -'Ukrainian (Ukraine)' => 'Europe/Kiev', -'Uzbek (Uzbekistan)' => 'Asia/Tashkent', -'Vietnamese (Vietnam)' => 'Asia/Saigon', -'Walon (Belgium)' => 'Europe/Brussels', +#- best guesses for a given country +my %c2t = ( +'AM' => 'Asia/Yerevan', +'AR' => 'America/Buenos_Aires', +'AT' => 'Europe/Vienna', +'AU' => 'Australia/Sydney', +'BA' => 'Europe/Sarajevo', +'BE' => 'Europe/Brussels', +'BG' => 'Europe/Sofia', +'BR' => 'America/Sao_Paulo', #- most brazilians live on this time zone +'BY' => 'Europe/Minsk', +'CA' => 'Canada/Eastern', +'CH' => 'Europe/Zurich', +'CN' => 'Asia/Beijing', +'CZ' => 'Europe/Prague', +'DE' => 'Europe/Berlin', +'DK' => 'Europe/Copenhagen', +'EE' => 'Europe/Tallinn', +'ES' => 'Europe/Madrid', +'FI' => 'Europe/Helsinki', +'FR' => 'Europe/Paris', +'GB' => 'Europe/London', +'GE' => 'Asia/Yerevan', +'GL' => 'Arctic/Longyearbyen', +'GR' => 'Europe/Athens', +'HR' => 'Europe/Zagreb', +'HU' => 'Europe/Budapest', +'ID' => 'Asia/Jakarta', +'IE' => 'Europe/Dublin', +'IL' => 'Asia/Tel_Aviv', +'IN' => 'Asia/Kolkata', +'IR' => 'Asia/Tehran', +'IS' => 'Atlantic/Reykjavik', +'IT' => 'Europe/Rome', +'JP' => 'Asia/Tokyo', +'KR' => 'Asia/Seoul', +'LT' => 'Europe/Vilnius', +'LV' => 'Europe/Riga', +'MK' => 'Europe/Skopje', +'MT' => 'Europe/Malta', +'MX' => 'America/Mexico_City', +'MY' => 'Asia/Kuala_Lumpur', +'NL' => 'Europe/Amsterdam', +'NO' => 'Europe/Oslo', +'NZ' => 'Pacific/Auckland', +'PL' => 'Europe/Warsaw', +'PT' => 'Europe/Lisbon', +'RO' => 'Europe/Bucharest', +'RU' => 'Europe/Moscow', +'SE' => 'Europe/Stockholm', +'SI' => 'Europe/Ljubljana', +'SK' => 'Europe/Bratislava', +'TH' => 'Asia/Bangkok', +'TJ' => 'Asia/Dushanbe', +'TR' => 'Europe/Istanbul', +'TW' => 'Asia/Taipei', +'UA' => 'Europe/Kiev', +'US' => 'America/New_York', +'UZ' => 'Asia/Tashkent', +'VN' => 'Asia/Saigon', +'YU' => 'Europe/Belgrade', +'ZA' => 'Africa/Johannesburg',  );  sub fuzzyChoice {  -    my ($b, $count) = bestMatchSentence($_[0], keys %l2t); +    my ($b, $count) = bestMatchSentence($_[0], keys %c2t);      $count ? $b : '';  } -sub bestTimezone { $l2t{fuzzyChoice($_[0])} || 'GMT' } - -sub ntp_servers {  -q(Australia (ntp.adelaide.edu.au) -Australia (ntp.saard.net) -Australia (time.esec.com.au) -Canada (ntp.cpsc.ucalgary.ca) -Canada (ntp1.cmc.ec.gc.ca) -Canada (ntp2.cmc.ec.gc.ca) -Canada (time.chu.nrc.ca) -Canada (time.nrc.ca) -Canada (timelord.uregina.ca) -Spain (slug.ctv.es) -France (ntp.univ-lyon1.fr) -Croatia (zg1.ntp.carnet.hr) -Croatia (zg2.ntp.carnet.hr) -Croatia (st.ntp.carnet.hr) -Croatia (ri.ntp.carnet.hr) -Croatia (os.ntp.carnet.hr) -Indonesia (ntp.incaf.net) -Italy (time.ien.it) -Korea, republic of (time.nuri.net) -Norway (fartein.ifi.uio.no) -Russia (ntp.landau.ac.ru) -Singapore (ntp.shim.org) -Slovenia (time.ijs.si) -United kingdom (ntp.cs.strath.ac.uk) -United kingdom (ntp2a.mcc.ac.uk) -United kingdom (ntp2b.mcc.ac.uk) -United kingdom (ntp2c.mcc.ac.uk) -United kingdom (ntp2d.mcc.ac.uk) -United states DE (louie.udel.edu) -United states IL (ntp-0.cso.uiuc.edu) -United states IL (ntp-1.cso.uiuc.edu) -United states IL (ntp-2.cso.uiuc.edu) -United states IN (gilbreth.ecn.purdue.edu) -United states IN (harbor.ecn.purdue.edu) -United states IN (molecule.ecn.purdue.edu) -); +sub bestTimezone { $c2t{fuzzyChoice($_[0])} || 'GMT' } + +our %ntp_servers; + +sub get_ntp_server_tree { +    my ($zone) = @_; +    map { +        $ntp_servers{$zone}{$_} => ( +            exists $ntp_servers{$_} ? +              $zone ? +                translate($_) . "|" . N("All servers") : +                N("All servers") : +              translate($zone) . "|" . translate($_) +        ), +        get_ntp_server_tree($_); +    } keys %{$ntp_servers{$zone}};  } +sub ntp_servers() { +    # FIXME: missing parameter: +    +{ get_ntp_server_tree() }; +} + +sub dump_ntp_zone { +    my ($zone) = @_; +    map { if_(/\[\d+\](.+) -- (.+\.ntp\.org)/, $1 => $2) } `lynx -dump http://www.pool.ntp.org/zone/$zone`; +} +sub print_ntp_zone { +    my ($zone, $name) = @_; +    # FIXME: missing parameter: +    my %servers = dump_ntp_zone($zone); +    print qq(\$ntp_servers{"$name"} = {\n); +    print join('', map { qq(    N_("$_") => "$servers{$_}",\n) } sort(keys %servers)); +    print "};\n"; +    \%servers; +} +sub print_ntp_servers() { +    print_ntp_zone(); +    my $servers = print_ntp_zone('@', "Global"); +    foreach my $name (sort(keys %$servers)) { +        my ($zone) = $servers->{$name} =~ /^(.*?)\./; +        print_ntp_zone($zone, $name); +    } +} + +# perl -Mtimezone -e 'timezone::print_ntp_servers()' +$ntp_servers{""} = { +    N_("Global") => "pool.ntp.org", +}; +$ntp_servers{Global} = { +    N_("Africa") => "africa.pool.ntp.org", +    N_("Asia") => "asia.pool.ntp.org", +    N_("Europe") => "europe.pool.ntp.org", +    N_("North America") => "north-america.pool.ntp.org", +    N_("Oceania") => "oceania.pool.ntp.org", +    N_("South America") => "south-america.pool.ntp.org", +}; +$ntp_servers{Africa} = { +    N_("South Africa") => "za.pool.ntp.org", +    N_("Tanzania") => "tz.pool.ntp.org", +}; +$ntp_servers{Asia} = { +    N_("Bangladesh") => "bd.pool.ntp.org", +    N_("China") => "cn.pool.ntp.org", +    N_("Hong Kong") => "hk.pool.ntp.org", +    N_("India") => "in.pool.ntp.org", +    N_("Indonesia") => "id.pool.ntp.org", +    N_("Iran") => "ir.pool.ntp.org", +    N_("Israel") => "il.pool.ntp.org", +    N_("Japan") => "jp.pool.ntp.org", +    N_("Korea") => "kr.pool.ntp.org", +    N_("Malaysia") => "my.pool.ntp.org", +    N_("Philippines") => "ph.pool.ntp.org", +    N_("Singapore") => "sg.pool.ntp.org", +    N_("Taiwan") => "tw.pool.ntp.org", +    N_("Thailand") => "th.pool.ntp.org", +    N_("Turkey") => "tr.pool.ntp.org", +    N_("United Arab Emirates") => "ae.pool.ntp.org", +}; +$ntp_servers{Europe} = { +    N_("Austria") => "at.pool.ntp.org", +    N_("Belarus") => "by.pool.ntp.org", +    N_("Belgium") => "be.pool.ntp.org", +    N_("Bulgaria") => "bg.pool.ntp.org", +    N_("Czech Republic") => "cz.pool.ntp.org", +    N_("Denmark") => "dk.pool.ntp.org", +    N_("Estonia") => "ee.pool.ntp.org", +    N_("Finland") => "fi.pool.ntp.org", +    N_("France") => "fr.pool.ntp.org", +    N_("Germany") => "de.pool.ntp.org", +    N_("Greece") => "gr.pool.ntp.org", +    N_("Hungary") => "hu.pool.ntp.org", +    N_("Ireland") => "ie.pool.ntp.org", +    N_("Italy") => "it.pool.ntp.org", +    N_("Lithuania") => "lt.pool.ntp.org", +    N_("Luxembourg") => "lu.pool.ntp.org", +    N_("Netherlands") => "nl.pool.ntp.org", +    N_("Norway") => "no.pool.ntp.org", +    N_("Poland") => "pl.pool.ntp.org", +    N_("Portugal") => "pt.pool.ntp.org", +    N_("Romania") => "ro.pool.ntp.org", +    N_("Russian Federation") => "ru.pool.ntp.org", +    N_("Slovakia") => "sk.pool.ntp.org", +    N_("Slovenia") => "si.pool.ntp.org", +    N_("Spain") => "es.pool.ntp.org", +    N_("Sweden") => "se.pool.ntp.org", +    N_("Switzerland") => "ch.pool.ntp.org", +    N_("Ukraine") => "ua.pool.ntp.org", +    N_("United Kingdom") => "uk.pool.ntp.org", +    N_("Yugoslavia") => "yu.pool.ntp.org", +}; +$ntp_servers{"North America"} = { +    N_("Canada") => "ca.pool.ntp.org", +    N_("Guatemala") => "gt.pool.ntp.org", +    N_("Mexico") => "mx.pool.ntp.org", +    N_("United States") => "us.pool.ntp.org", +}; +$ntp_servers{Oceania} = { +    N_("Australia") => "au.pool.ntp.org", +    N_("New Zealand") => "nz.pool.ntp.org", +}; +$ntp_servers{"South America"} = { +    N_("Argentina") => "ar.pool.ntp.org", +    N_("Brazil") => "br.pool.ntp.org", +    N_("Chile") => "cl.pool.ntp.org", +}; +  1; | 
