"Fossies" - the Fresh Open Source Software Archive

Member "squid-graph/timeconv" (2 Oct 2006, 3832 Bytes) of package /linux/privat/old/squid-graph-3.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl
    2 
    3 ######################################################################
    4 #
    5 # Squid Graph Time Converter
    6 #
    7 # Supplementary Tool for Squid Graph v3
    8 #
    9 # http://squid-graph.sourceforge.net/
   10 #
   11 # Please refer to the Squid Graph v3 documentation for more info
   12 #
   13 #####################################################################
   14 
   15 use strict;
   16 
   17 if ($#ARGV < 1) { usage(); }
   18 elsif (($ARGV[0] eq "-n") && ($#ARGV >= 2)) {
   19     if (not exists $ARGV[3]) {
   20         warn "GMT offset not given. Assuming time is in GMT.\n";
   21         $ARGV[3] = 0;
   22     }
   23     if (length((split(/\//,$ARGV[2]))[2]) == 2) {
   24         my ($DAY, $MONTH, $YEAR) = split(/\//, $ARGV[2]);
   25         if ($YEAR >= 70) {
   26             $YEAR += 1900;
   27         }
   28         else {
   29             $YEAR += 2000;
   30         }
   31         warn "2 digit year provided. Assuming year provided is $YEAR.\n";
   32         $ARGV[2] = "$DAY/$MONTH/$YEAR";
   33     }
   34     my $RESULT = convert($ARGV[1], $ARGV[2], $ARGV[3]);
   35     print "$RESULT\n";
   36 }
   37 elsif (($ARGV[0] eq "-g") && ($#ARGV == 1)) {
   38     my ($SEC, $MIN, $HOUR, $DAY, $MONTH, $YEAR) = gmtime($ARGV[1]);
   39     $HOUR = zeropad($HOUR,2);
   40     $MIN = zeropad($MIN,2);
   41     $SEC = zeropad($SEC,2);
   42     $DAY = zeropad($DAY,2);
   43     $MONTH = zeropad($MONTH + 1,2);
   44     $YEAR = $YEAR + 1900;
   45     print "$HOUR:$MIN:$SEC $DAY/$MONTH/$YEAR GMT\n";
   46 }
   47 elsif (($ARGV[0] eq "-l") && ($#ARGV == 1)) {
   48     my ($SEC, $MIN, $HOUR, $DAY, $MONTH, $YEAR) = localtime($ARGV[1]);
   49     $HOUR = zeropad($HOUR,2);
   50     $MIN = zeropad($MIN,2);
   51     $SEC = zeropad($SEC,2);
   52     $DAY = zeropad($DAY,2);
   53     $MONTH = zeropad($MONTH + 1,2);
   54     $YEAR = $YEAR + 1900;
   55     print "$HOUR:$MIN:$SEC $DAY/$MONTH/$YEAR\n";
   56 }
   57 else { usage(); }
   58 
   59 sub usage() {
   60     print "\n";
   61     print "Squid Graph Time Converter\n";
   62     print "\n";
   63     print "Supplementary Tool for Squid Graph v3\n";
   64     print "\n";
   65     print "Utility to convert time from normal hh:mm:ss dd/mm/yyyy format to\n";
   66     print "seconds since 1970, and vice-versa.\n";
   67     print "\n";
   68     print "Usage:\n";
   69     print " $0 -n hh:mm:ss dd/mm/[yy]yy [offset]\n";
   70     print " $0 -g seconds-since-1970 (Outputs time is GMT)\n";
   71     print " $0 -l seconds-since-1970 (Outputs time is local time)\n";
   72     print "\n";
   73     print "E.g.:\n";
   74     print " $0 -n 12:04:22 12/10/2001 +8\n";
   75     print " $0 -n 12:04:22 12/10/01 +8\n";
   76     print " $0 -n 12:04:22 12/Oct/01 +8\n";
   77     print " $0 -g 9912334123\n";
   78     print " $0 -l 9912334123\n";
   79     print "\n";
   80     print "Please refer to the Squid Graph v3 documentation for more info.\n";
   81     print "http://squid-graph.sourceforge.net/\n";
   82     print "\n";
   83 }
   84 
   85 sub zeropad($$) {
   86     my $NUM = shift;
   87     my $LEN = shift;
   88     return $NUM if ($LEN < length($NUM));
   89     my $PAD = $LEN - length($NUM);
   90     return $NUM if ($PAD == 0);
   91     my $i;
   92     my $OUT = '';
   93     for ($i = 1; $i <= $PAD; $i++) {
   94         $OUT = "0$OUT";
   95     }
   96     undef $i;
   97     $OUT = "$OUT$NUM";
   98     return $OUT;
   99 }
  100 
  101 sub convert($$$) {
  102     my $TIME = shift;
  103     my $DATE = shift;
  104     my $OFFSET = shift;
  105 
  106     $DATE =~ s/Jan/01/;
  107     $DATE =~ s/Feb/02/;
  108     $DATE =~ s/Mar/03/;
  109     $DATE =~ s/Apr/04/;
  110     $DATE =~ s/May/05/;
  111     $DATE =~ s/Jun/06/;
  112     $DATE =~ s/Jul/07/;
  113     $DATE =~ s/Aug/08/;
  114     $DATE =~ s/Sep/09/;
  115     $DATE =~ s/Oct/10/;
  116     $DATE =~ s/Nov/11/;
  117     $DATE =~ s/Dec/12/;
  118 
  119     my ($HOUR, $MIN, $SEC) = split(/:/, $TIME);
  120     my ($DAY, $MONTH, $YEAR) = split(/\//, $DATE);
  121 
  122     my $TOTAL = 0;
  123 
  124     my $i = 0;
  125     for ($i = 1; $i <= ($YEAR - 1970); $i++) {
  126         $TOTAL += 31536000;
  127         $TOTAL += 86400 if (isleap($i + 1970));
  128     }
  129     for ($i = 1; $i < $MONTH; $i++) {
  130         $TOTAL += daysinmonth($i) * 86400;
  131         $TOTAL += 86400 if ((isleap($YEAR)) && ($i == 2));
  132     }
  133     undef $i;
  134 
  135     $TOTAL += ($DAY - 1) * 86400;
  136     $TOTAL += $HOUR * 3600;
  137     $TOTAL += $MIN * 60;
  138     $TOTAL += $SEC;
  139     $TOTAL -= $OFFSET * 3600;
  140     
  141     sub isleap($) {
  142         my $YEAR = shift;
  143         if ($YEAR%4 != 0) { return 0; }
  144         elsif ($YEAR%400 == 0) { return 1; }
  145         elsif ($YEAR%100 == 0) { return 0; }
  146         else { return 1; }
  147     }
  148 
  149     sub daysinmonth($$) {
  150         my $MONTH = shift;
  151         my @DAYS = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  152         return $DAYS[$MONTH];
  153     }
  154 
  155     return $TOTAL;
  156 }