home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Time / TimeDate.pm < prev   
Encoding:
Perl POD Document  |  2006-08-14  |  8.0 KB  |  337 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3. # Time/Date Configuration handling
  4. #
  5. # Copyright (C) 2000-2001 Ximian, Inc.
  6. #
  7. # Authors: Hans Petter Jansson <hpj@ximian.com>
  8. #          Carlos Garnacho     <carlosg@gnome.org>
  9. #          Grzegorz Golawski <grzegol@pld-linux.org> (PLD Support)
  10. #          James Ogley <james@usr-local-bin.org> (SuSE 9.0 support)
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU Library General Public License as published
  14. # by the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU Library General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU Library General Public License
  23. # along with this program; if not, write to the Free Software
  24. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  25.  
  26. package Time::TimeDate;
  27.  
  28. use File::Copy;
  29.  
  30. sub get_utc_time
  31. {
  32.   my (%h, $trash);
  33.  
  34.   ($h{"second"}, $h{"minute"}, $h{"hour"}, $h{"monthday"}, $h{"month"}, $h{"year"},
  35.    $trash, $trash, $trash) = gmtime (time);
  36.  
  37.   $h{"year"} += 1900;
  38.  
  39.   return \%h;
  40. }
  41.  
  42. # This function will force date format when setting time
  43. sub change_timedate
  44. {
  45.   my ($time) = @_;
  46.   my ($command);
  47.  
  48.   my $system_table = {
  49.     "Linux"   => "date -u %02d%02d%02d%02d%04d.%02d",
  50.     "FreeBSD" => "date -u -f %%m%%d%%H%%M%%Y.%%S  %02d%02d%02d%02d%04d.%02d"
  51.   };
  52.  
  53.   $command = sprintf ($$system_table {$Utils::Backend::tool{"system"}},
  54.                       $$time{"month"} + 1, $$time{"monthday"},
  55.                       $$time{"hour"},  $$time{"minute"}, 
  56.                       $$time{"year"},  $$time{"second"});
  57.  
  58.   &Utils::Report::do_report ("time_localtime_set", $command);
  59.   return &Utils::File::run ($command);
  60. }
  61.  
  62. sub set_utc_time
  63. {
  64.   my ($time) = @_;
  65.   my ($res, $xscreensaver_owners);
  66.  
  67.   &Utils::Report::enter ();
  68.  
  69.   # FIXME: restore this, take into account other screensavers
  70.   # Kill screensaver, so it doesn't confuse the users.
  71. #  $xscreensaver_owners = &gst_service_proc_get_owners ("xscreensaver");
  72. #  &gst_service_proc_stop_all  ("xscreensaver");
  73.  
  74.   $res = &change_timedate ($time);
  75.  
  76.   # Restart screensaver.
  77. #  &gst_service_proc_start_all ("xscreensaver -no-splash", $xscreensaver_owners);
  78.  
  79.   &Utils::Report::leave ();
  80.   return -1 if $res;
  81.   return 0;
  82. }
  83.  
  84. sub time_sync_hw_from_sys
  85. {
  86.   &Utils::File::run ("hwclock --systohc");
  87.   return 0;
  88. }
  89.  
  90. sub get_timezone
  91. {
  92.   my ($local_time_file, $zoneinfo_dir) = @_;
  93.   local *TZLIST;
  94.   my $zone;
  95.   my $size_search;
  96.   my $size_test;
  97.  
  98.   *TZLIST = &Utils::File::open_read_from_names($zoneinfo_dir . "/zone.tab");
  99.   if (not *TZLIST) { return; }
  100.  
  101.   &Utils::Report::do_report ("time_timezone_scan");
  102.  
  103.   # Get the filesize for /etc/localtime so that we don't have to execute
  104.   # a diff for every file, only for file with the correct size. This speeds
  105.   # up loading 
  106.   $size_search = (stat ($local_time_file))[7];
  107.  
  108.   while (<TZLIST>)
  109.   {
  110.     if (/^\#/) { next; }                   # Skip comments.
  111.     ($d, $d, $zone) = split /[\t ]+/, $_;  # Get 3rd column.
  112.     chomp $zone;                           # Remove linefeeds.
  113.  
  114.  
  115.     # See if this zone file matches the installed one.
  116.     &Utils::Report::do_report ("time_timezone_cmp", $zone);
  117.     $size_test = (stat("$zoneinfo_dir/$zone"))[7];
  118.     if ($size_test eq $size_search)
  119.     {
  120.       if (!&Utils::File::run ("diff $zoneinfo_dir/$zone $local_time_file"))
  121.       {
  122.         # Found a match.
  123.         last;
  124.       }
  125.     }
  126.     
  127.     $zone = "";
  128.   }
  129.   
  130.   return $zone;
  131.   close (TZLIST);
  132. }
  133.  
  134. sub set_timezone
  135. {
  136.   my ($localtime, $zonebase, $timezone) = @_;
  137.  
  138.   &Utils::Report::enter ();
  139.   &Utils::Report::do_report ("time_timezone_set", $timezone);
  140.  
  141.   $tz = "$zonebase/$timezone";
  142.  
  143.   if (stat($tz) ne "")
  144.   {
  145.     unlink $localtime;  # Important, since it might be a symlink.
  146.     
  147.     &Utils::Report::enter ();
  148.     $res = copy ($tz, $localtime);
  149.     &Utils::Report::leave ();
  150.     return -1 unless $res;
  151.     return 0;
  152.   }
  153.  
  154.   &Utils::Report::leave ();
  155.   return -1;
  156. }
  157.  
  158. sub get_dist
  159. {
  160.   my %dist_map =
  161.   (
  162.    "redhat-6.2"      => "redhat-6.2",
  163.    "redhat-7.0"      => "redhat-6.2",
  164.    "redhat-7.1"      => "redhat-6.2",
  165.    "redhat-7.2"      => "redhat-6.2",
  166.    "redhat-7.3"      => "redhat-6.2",
  167.    "redhat-8.0"      => "redhat-6.2",
  168.    "mandrake-9.0"    => "redhat-6.2",
  169.    "debian-3.0"      => "debian-3.0",
  170.    "suse-9.0"        => "redhat-6.2",
  171.    "slackware-9.1.0" => "redhat-6.2",
  172.    "gentoo"          => "redhat-6.2",
  173.    "archlinux"       => "archlinux",
  174.    "pld-1.0"         => "redhat-6.2",
  175.    "vine-3.0"        => "redhat-6.2",
  176.    "freebsd-5"       => "redhat-6.2",
  177.    );
  178.  
  179.   return $dist_map{$Utils::Backend::tool{"platform"}};
  180. }
  181.  
  182. sub conf_get_parse_table
  183. {
  184.   my %dist_tables =
  185.   (
  186.    "redhat-6.2" =>
  187.    {
  188.      fn =>
  189.      {
  190.        ZONEINFO     => "/usr/share/zoneinfo",
  191.        LOCAL_TIME   => "/etc/localtime"
  192.      },
  193.      table =>
  194.      [
  195.       [ "local_time",   \&get_utc_time ],
  196.       [ "timezone",     \&get_timezone, [LOCAL_TIME, ZONEINFO] ],
  197.      ]
  198.    },
  199.  
  200.    "debian-3.0" =>
  201.    {
  202.      fn =>
  203.      {
  204.        ZONEINFO     => "/usr/share/zoneinfo",
  205.        LOCAL_TIME   => "/etc/localtime"
  206.      },
  207.      table =>
  208.      [
  209.       [ "local_time",   \&get_utc_time ],
  210.       [ "timezone",     \&get_timezone, [LOCAL_TIME, ZONEINFO] ],
  211.      ]
  212.    },
  213.  
  214.    "archlinux" =>
  215.    {
  216.      fn =>
  217.      {
  218.        RC_LOCAL     => "/etc/rc.local",
  219.        ZONEINFO     => "/usr/share/zoneinfo",
  220.        LOCAL_TIME   => "/etc/localtime"
  221.      },
  222.      table =>
  223.      [
  224.       [ "local_time",   \&get_utc_time ],
  225.       [ "timezone",     \&Utils::Parse::get_sh, RC_LOCAL, TIMEZONE ],
  226.      ]
  227.    },
  228.   );
  229.  
  230.   my $dist = &get_dist();
  231.   return %{$dist_tables{$dist}} if $dist;
  232.  
  233.   &Utils::Report::do_report ("platform_no_table", $Utils::backend::tool{"platform"});
  234.   return undef;
  235. }
  236.  
  237. sub conf_get_replace_table
  238. {
  239.   my %dist_tables =
  240.   (
  241.    "redhat-6.2" =>
  242.    {
  243.      fn =>
  244.      {
  245.        ZONEINFO     => "/usr/share/zoneinfo",
  246.        LOCAL_TIME    => "/etc/localtime"
  247.      },
  248.      table =>
  249.      [
  250.       [ "timezone",    \&set_timezone, [LOCAL_TIME, ZONEINFO] ],
  251.       [ "local_time",  \&set_utc_time ],
  252.      ]
  253.    },
  254.        
  255.    "debian-3.0" =>
  256.    {
  257.      fn =>
  258.      {
  259.        ZONEINFO     => "/usr/share/zoneinfo",
  260.        LOCAL_TIME   => "/etc/localtime",
  261.        TIMEZONE     => "/etc/timezone"
  262.      },
  263.      table =>
  264.      [
  265.       [ "timezone",    \&set_timezone, [LOCAL_TIME, ZONEINFO] ],
  266.       [ "timezone",    \&Utils::Replace::set_first_line, TIMEZONE ],
  267.       [ "local_time",  \&set_utc_time ],
  268.      ]
  269.    },
  270.  
  271.    "archlinux" =>
  272.    {
  273.      fn =>
  274.      {
  275.        RC_LOCAL     => "/etc/rc.local",
  276.        ZONEINFO     => "/usr/share/zoneinfo",
  277.        LOCAL_TIME   => "/etc/localtime",
  278.      },
  279.      table =>
  280.      [
  281.       [ "timezone",    \&Utils::Replace::set_sh, RC_LOCAL, TIMEZONE ],
  282.       [ "timezone",    \&set_timezone, [LOCAL_TIME, ZONEINFO] ],
  283.       [ "local_time",  \&set_utc_time ],
  284.      ]
  285.    },
  286.   );
  287.  
  288.   my $dist = &get_dist ();
  289.   return %{$dist_tables{$dist}} if $dist;
  290.  
  291.   &Utils::Report::do_report ("platform_no_table", $Utils::Backend::tool{"platform"});
  292.   return undef;
  293. }
  294.  
  295. sub get
  296. {
  297.   my %dist_attrib;
  298.   my $hash;
  299.  
  300.   %dist_attrib = &conf_get_parse_table ();
  301.  
  302.   $hash = &Utils::Parse::get_from_table ($dist_attrib{"fn"},
  303.                                          $dist_attrib{"table"});
  304.   $h = $$hash {"local_time"};
  305.  
  306.   return ($$h {"year"}, $$h {"month"},  $$h {"monthday"},
  307.           $$h {"hour"}, $$h {"minute"}, $$h {"second"},
  308.           $$hash{"timezone"});
  309. }
  310.  
  311. sub set
  312. {
  313.   my (@config) = @_;
  314.   my ($hash, %localtime);
  315.  
  316.   %localtime = (
  317.     "year"     => $config[0],
  318.     "month"    => $config[1],
  319.     "monthday" => $config[2],
  320.     "hour"     => $config[3],
  321.     "minute"   => $config[4],
  322.     "second"   => $config[5]
  323.   );
  324.  
  325.   $$hash{"local_time"} = \%localtime;
  326.   $$hash{"timezone"}   = $config[6];
  327.  
  328.   %dist_attrib = &conf_get_replace_table ();
  329.  
  330.   $res = &Utils::Replace::set_from_table ($dist_attrib{"fn"}, $dist_attrib{"table"}, $hash);
  331.   &time_sync_hw_from_sys ();
  332.  
  333.   return $res;
  334. }
  335.  
  336. 1;
  337.