|
#!/bin/perl
use Time::Local;
my $a=time;
print "$a\n";
my @b = localtime($a);
print join(".",@b)."\n";
my $c = timelocal(@b);
print "$c\n";
#!/usr/bin/perl
#==============================================================================#
package main;
#==============================================================================#
#------------------------------------------------------------------------------#
sub time_chrs {
#------------------------------------------------------------------------------#
my ($atime) = @_;
if(! $atime) { $atime = time;}
my ($xs,$xm,$xh,$xdd,$xmm,$xyy,$xwd,$xyd,$xisdst) = localtime($atime);
$xmm ++;
$xyy += 1900;
$atime = sprintf("%4d%02d%02d%02d%02d%02d",$xyy,$xmm,$xdd,$xh,$xm,$xs);
my @olist = split(//,$atime);
@olist;
}
#------------------------------------------------------------------------------#
sub time_str {
#------------------------------------------------------------------------------#
my ($atime) = @_;
if(! $atime) { $atime = time;}
print "$atime\n";
my ($xs,$xm,$xh,$xdd,$xmm,$xyy,$xwd,$xyd,$xisdst) = localtime($atime);
$xmm ++;
if($xyy > 99) { $xyy -= 100;}
return sprintf("%02d.%d.%d %02d:%02d:%02d",$xyy,$xmm,$xdd,$xh,$xm,$xs);
}
#------------------------------------------------------------------------------#
sub time_num14 {
#------------------------------------------------------------------------------#
my ($atime) = @_;
if(! $atime) { $atime = time;}
my ($xs,$xm,$xh,$xdd,$xmm,$xyy,$xwd,$xyd,$xisdst) = localtime($atime);
$xmm ++;
$xyy += 1900;
return sprintf("%d%02d%02d%02d%02d%02d",$xyy,$xmm,$xdd,$xh,$xm,$xs);
}
#------------------------------------------------------------------------------#
sub str_time {
#------------------------------------------------------------------------------#
my ($str) = @_;
my ($xyy,$xmm,$xdd,$xh,$xm,$xs) = split(/[\.\:\s]/,$str);
if($xyy < 10) { $xyy += 2000;}
elsif($xyy < 1900) { $xyy += 1900;}
my $tstr = sprintf("%d%02d%02d%02d%02d%02d",$xyy,$xmm,$xdd,$xh,$xm,$xs);
my $pnum = time;
my $pstr = &time_num14($pnum);
my $diff = $pstr - $tstr;
do {
while($tstr < $pstr) {
$pnum -= $diff;
$pstr = &time_num14($pnum);
};
$diff = $diff>>1;
while($tstr > $pstr) {
$pnum += $diff;
$pstr = &time_num14($pnum);
}
$diff = $diff>>1;
} while($tstr != $pstr);
return $pnum;
}
my $astr = "08.3.2 12:00:24";
print "$astr\n";
print &str_time($astr);
%cat jtime.pl
#!/usr/bin/perl
#time,localtime,gmtime
require "$ENV{'JUTIL_HOME'}/jtime.pm";
my $atime = 0;
print "1.time:$atime\n";
my @timearray = localtime($atime);
print " localtime:",join(',',@timearray)."\n\n";
$atime = time;
print "2.time:$atime\n";
my @timearray = localtime($atime);
print " localtime:",join(',',@timearray)."\n\n";
@timearray = (6,5,4,3,2,6);
print "3.timearray:",join(',',@timearray)."\n";
$atime = timelocal(@timearray);
print " timelocal:$atime\n";
@timearray = localtime($atime);
print " localtime:",join(',',@timearray)."\n\n";
@timearray = (6,5,4,3,2,2006);
print "4.timearray:",join(',',@timearray)."\n";
$atime = timelocal(@timearray);
print " timelocal:$atime\n";
@timearray = localtime($atime);
print " localtime:",join(',',@timearray)."\n\n";
print "5.jtime:$atime\n";
@timearray = jlocaltime($atime);
print " jlocaltime:",join(',',@timearray)."\n";
$atime = jtimelocal(@timearray);
print " jtimelocal:$atime\n";
@timearray = jtimeread("2006.9.8","10:11:30");
print " jtimeread:",join(',',@timearray)."\n";
@timearray = btimeread("20060908101130");
print " btimeread:",join(',',@timearray)."\n";
%cat jtime.pm
#!/usr/bin/perl
#time,localtime,gmtime
require "timelocal.pl"; #for timelocal, timegm
#--------------------------------------------------------------------#
sub jlocaltime {
#--------------------------------------------------------------------#
my ($time) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($time);
$year+=1900;
$mon ++;
($year,$mon,$mday,$hour,$min,$sec);
}
#---------------------------------------------------------------------#
sub jtimelocal {
#---------------------------------------------------------------------#
my ($jyear,$jmonth,$jday,$jhour,$jmin,$jsec) = @_;
$jmonth --;
my $jtime = timelocal($jsec,$jmin,$jhour,$jday,$jmonth,$jyear);
return $jtime;
}
#---------------------------------------------------------------------#
sub jtimelocal1 {
#---------------------------------------------------------------------#
my ($jyear,$jmonth,$jday,$jhour,$jmin,$jsec) = @_;
if( $jyear > 1900) { $jyear -= 1900;}
elsif($jyear < 70) { $jyear += 100;}
$jmonth --;
$stime = sprintf("%d%02d%02d%02d%02d%02d",
$jyear,$jmonth,$jday,$jhour,$jmin,$jsec);
my $imin=1;
my $imax=2000000000;
do {
$imid = int(($imax + $imin)/2);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime($imid);
$itime = sprintf("%d%02d%02d%02d%02d%02d",
$year,$mon,$mday,$hour,$min,$sec);
if($stime < $itime) { $imax = $imid;}
elsif($stime > $itime) { $imin = $imid;}
else { $imax = $imid; $imin = $imid;}
} while(($imax - $imin)>1);
return $imid;
}
#--------------------------------------------------------------------#
sub jtimeread {
#--------------------------------------------------------------------#
my (@times) = @_;
my $stime = join(' ',@times);
my ($jyear,$jmonth,$jday,$jhour,$jmin,$jsec) =
split(/[-_\.\:\s]/,$stime);
return($jyear,$jmonth,$jday,$jhour,$jmin,$jsec);
}
#--------------------------------------------------------------------#
sub btimeread {
#--------------------------------------------------------------------#
my ($stime) = @_;
@times = split('',$stime);
$jyear = join('',@times[ 0.. 3]);
$jmonth= join('',@times[ 4.. 5]);
$jday = join('',@times[ 6.. 7]);
$jhour = join('',@times[ 8.. 9]);
$jmin = join('',@times[10..11]);
$jsec = join('',@times[12..13]);
return($jyear,$jmonth,$jday,$jhour,$jmin,$jsec);
}
1; |
|