Blame examples/dm_zdump

Packit 95306a
#!/usr/bin/perl -w
Packit 95306a
# Copyright (c) 2009-2017 Sullivan Beck.  All rights reserved.
Packit 95306a
# This program is free software; you can redistribute it and/or modify it
Packit 95306a
# under the same terms as Perl itself.
Packit 95306a
Packit 95306a
###############################################################################
Packit 95306a
###############################################################################
Packit 95306a
Packit 95306a
require 5.010000;
Packit 95306a
use Date::Manip::Date;
Packit 95306a
use IO::File;
Packit 95306a
use strict;
Packit 95306a
use warnings;
Packit 95306a
Packit 95306a
###############################################################################
Packit 95306a
# HELP
Packit 95306a
###############################################################################
Packit 95306a
Packit 95306a
our($usage);
Packit 95306a
my $COM = $0;
Packit 95306a
$COM =~ s/^.*\///;
Packit 95306a
Packit 95306a
$usage=
Packit 95306a
  "usage: $COM OPTIONS [ZONE ZONE ...]
Packit 95306a
      -h/--help        : Print help.
Packit 95306a
Packit 95306a
      -v/--vebose      : Prints a full description of each
Packit 95306a
                         timezone.
Packit 95306a
Packit 95306a
      -c/--cutoff YEAR : Cut off verbose output near the
Packit 95306a
                         start of YEAR.
Packit 95306a
Packit 95306a
";
Packit 95306a
Packit 95306a
=pod
Packit 95306a
Packit 95306a
=head1 NAME
Packit 95306a
Packit 95306a
dm_zdump - timezone dumper
Packit 95306a
Packit 95306a
=head1 SYNOPSIS
Packit 95306a
Packit 95306a
This performs the same operation as the unix 'zdump' command, but using
Packit 95306a
the Date::Manip module.
Packit 95306a
Packit 95306a
   dm_zdump [-v] [-c YEAR] [ZONE ZONE ...]
Packit 95306a
Packit 95306a
=head1 DESCRIPTION
Packit 95306a
Packit 95306a
This displays the current time in each ZONE named on the command line
Packit 95306a
unless the -v option is given.
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item -h, --help
Packit 95306a
Packit 95306a
Print online help.
Packit 95306a
Packit 95306a
=item -v, --verbose
Packit 95306a
Packit 95306a
This displays all critical dates (i.e. the times when a time change
Packit 95306a
occurs due to the timezone description) for each of the timezones
Packit 95306a
listed (or the local timezone if none are listed).
Packit 95306a
Packit 95306a
Each critical date is printed as two lines of output: the last second
Packit 95306a
before the change occurs, and the first second of the new time.
Packit 95306a
Packit 95306a
By default, all critical dates from Jan 1, 0001 until the year 20 years
Packit 95306a
in the future are printed, but this can be changed with the -c option.
Packit 95306a
Packit 95306a
=item -c, --cutoff YEAR
Packit 95306a
Packit 95306a
This specifies the cutoff year. All critical dates up to the start of
Packit 95306a
YEAR are given. The GMT time Jan 01, YEAR at 00:00:00 is the cutoff
Packit 95306a
time.
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
=head1 KNOWN BUGS
Packit 95306a
Packit 95306a
None known.
Packit 95306a
Packit 95306a
=head1 BUGS AND QUESTIONS
Packit 95306a
Packit 95306a
Please refer to the Date::Manip::Problems documentation for
Packit 95306a
information on submitting bug reports or questions to the author.
Packit 95306a
Packit 95306a
=head1 SEE ALSO
Packit 95306a
Packit 95306a
Date::Manip::Date
Packit 95306a
Packit 95306a
=head1 LICENSE
Packit 95306a
Packit 95306a
This script is free software; you can redistribute it and/or
Packit 95306a
modify it under the same terms as Perl itself.
Packit 95306a
Packit 95306a
=head1 AUTHOR
Packit 95306a
Packit 95306a
Sullivan Beck (sbeck@cpan.org)
Packit 95306a
Packit 95306a
=cut
Packit 95306a
Packit 95306a
###############################################################################
Packit 95306a
# PARSE ARGUMENTS
Packit 95306a
###############################################################################
Packit 95306a
Packit 95306a
our($date,$dmt,$dmb,$verbose,$cutoff,@zone);
Packit 95306a
Packit 95306a
$date    = new Date::Manip::Date "now";
Packit 95306a
$dmt     = $date->tz();
Packit 95306a
$dmb     = $date->base();
Packit 95306a
$cutoff  = $date->printf('%Y') + 21;
Packit 95306a
$verbose = 0;
Packit 95306a
Packit 95306a
while ($_ = shift) {
Packit 95306a
Packit 95306a
   (print $usage),   exit  if ($_ eq "-h"   ||  $_ eq "--help");
Packit 95306a
Packit 95306a
   $verbose = 1,     next  if ($_ eq "-v"   ||  $_ eq "--verbose");
Packit 95306a
   $cutoff = shift,  next  if ($_ eq "-c"   ||  $_ eq "--cutoff");
Packit 95306a
Packit 95306a
   @zone = ($_,@ARGV);
Packit 95306a
   last;
Packit 95306a
}
Packit 95306a
Packit 95306a
if (@zone) {
Packit 95306a
   foreach my $z (@zone) {
Packit 95306a
      my $tmp = $dmt->zone($z);
Packit 95306a
      if (! $tmp) {
Packit 95306a
         die "ERROR: invalid timezone: $z\n";
Packit 95306a
      }
Packit 95306a
      $z = $tmp;
Packit 95306a
   }
Packit 95306a
Packit 95306a
} else {
Packit 95306a
   @zone = $dmt->curr_zone();
Packit 95306a
}
Packit 95306a
Packit 95306a
############################################################################
Packit 95306a
# MAIN PROGRAM
Packit 95306a
############################################################################
Packit 95306a
Packit 95306a
if ($verbose) {
Packit 95306a
   foreach my $z (@zone) {
Packit 95306a
      my @per        = $dmt->periods($z,undef,$cutoff);
Packit 95306a
      foreach my $per (@per) {
Packit 95306a
         my($startUT,$startLT,$offsetstr,$offset,$abbrev,$isdst,$endUT,$endLT)
Packit 95306a
           = @$per;
Packit 95306a
Packit 95306a
         $startUT   = datestr($startUT);
Packit 95306a
         $startLT   = datestr($startLT);
Packit 95306a
         $endUT     = datestr($endUT);
Packit 95306a
         $endLT     = datestr($endLT);
Packit 95306a
         my $gmtoff = $$offset[0]*3600 + $$offset[1]*60 + $$offset[2];
Packit 95306a
Packit 95306a
         print "$z  $startUT UT = $startLT $abbrev isdst=$isdst gmtoff=$gmtoff\n";
Packit 95306a
         print "$z  $endUT UT = $endLT $abbrev isdst=$isdst gmtoff=$gmtoff\n";
Packit 95306a
      }
Packit 95306a
      print "\n"  if ($#zone != 0);
Packit 95306a
   }
Packit 95306a
Packit 95306a
} else {
Packit 95306a
   my $wid = 0;
Packit 95306a
   foreach my $z (@zone) {
Packit 95306a
      $wid = length($z)  if (length($z) > $wid);
Packit 95306a
   }
Packit 95306a
Packit 95306a
   foreach my $z (@zone) {
Packit 95306a
      $date->convert($z);
Packit 95306a
      print $z," "x($wid-length($z)),"  ",$date->printf('%a %b %e %H:%M:%S %Y %Z'),"\n";
Packit 95306a
   }
Packit 95306a
}
Packit 95306a
Packit 95306a
sub datestr {
Packit 95306a
   my($date) = @_;
Packit 95306a
Packit 95306a
   my %mon = qw(1 Jan 2 Feb 3 Mar 4 Apr 5 May 6 Jun
Packit 95306a
                7 Jul 8 Aug 9 Sep 10 Oct 11 Nov 12 Dec);
Packit 95306a
   my %dow = qw(1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 Sat 7 Sun);
Packit 95306a
Packit 95306a
   my($y,$m,$d,$h,$mn,$s) = @$date;
Packit 95306a
   my $dow = $dmb->day_of_week($date);
Packit 95306a
   $dow    = $dow{$dow};
Packit 95306a
   my $mon = $mon{$m+0};
Packit 95306a
   $y="0$y"    while (length($y) < 4);
Packit 95306a
   $d=" $d"    if (length($d) < 2);
Packit 95306a
   $h="0$h"    if (length($h) < 2);
Packit 95306a
   $mn="0$mn"  if (length($mn) < 2);
Packit 95306a
   $s="0$s"    if (length($s) < 2);
Packit 95306a
Packit 95306a
   return "$dow $mon $d $h:$mn:$s $y";
Packit 95306a
}
Packit 95306a
Packit 95306a
# Local Variables:
Packit 95306a
# mode: cperl
Packit 95306a
# indent-tabs-mode: nil
Packit 95306a
# cperl-indent-level: 3
Packit 95306a
# cperl-continued-statement-offset: 2
Packit 95306a
# cperl-continued-brace-offset: 0
Packit 95306a
# cperl-brace-offset: 0
Packit 95306a
# cperl-brace-imaginary-offset: 0
Packit 95306a
# cperl-label-offset: 0
Packit 95306a
# End: