This perl script prints modification time of a file, in various format.
Examples :
datemodif y-m-d mystuff.log → 2003-30-06
datemodif d/M/Y-h.m.s mystuff.log → 06/12/03-21.30.02
datemodif X d O Y mystuff.log → Samedi 06 Décembre 03
PERL:
-
#!/usr/bin/perl
-
-
# datemodif <format> <fichier>
-
# ex : datemodif ymd tutu.txt
-
# ex : datemodif m-d tutu.txt
-
# ex : datemodif "D M Y" tutu.txt
-
#
-
# Heure de modif :
-
# s : 01-59
-
# m : 01-12
-
# h : 01-24
-
# i : 01-12 am/pm
-
#
-
# Jour de modif :
-
# d : 01-31
-
# D : Lun-Dim
-
# e : lun-dim
-
# F : Lundi - Dimanche
-
# g : lundi - dimanche
-
# M : 1-12
-
# N : Jan-Dec
-
# n ; jan-dec
-
# O : Janvier-Decembre
-
# o : janvier-decembre
-
# y : 02
-
# Y : 2002
-
-
%weekday=(
-
"0"=>"Dim",
-
"1"=>"Lun",
-
"2"=>"Mar",
-
"3"=>"Mer",
-
"4"=>"Jeu",
-
"5"=>"Ven",
-
"6"=>"Sam",
-
);
-
-
%lweekday=(
-
"0"=>"Dimanche",
-
"1"=>"Lundi",
-
"2"=>"Mardi",
-
"3"=>"Mercredi",
-
"4"=>"Jeudi",
-
"5"=>"Vendredi",
-
"6"=>"Samedi",
-
);
-
-
%month=(
-
"1"=>"Jan",
-
"2"=>"Fev",
-
"3"=>"Mar",
-
"4"=>"Avr",
-
"5"=>"Mai",
-
"6"=>"Juin",
-
"7"=>"Juil",
-
"8"=>"Aout",
-
"9"=>"Sept",
-
"10"=>"Oct",
-
"11"=>"Nov",
-
"12"=>"Dec",
-
);
-
-
-
%lmonth=(
-
"1"=>"Janvier",
-
"2"=>"Février",
-
"3"=>"Mars",
-
"4"=>"Avril",
-
"5"=>"Mai",
-
"6"=>"Juin",
-
"7"=>"Juillet",
-
"8"=>"Aout",
-
"9"=>"Septembre",
-
"10"=>"Octobre",
-
"11"=>"Novembre",
-
"12"=>"Décembre",
-
);
-
-
if (!@ARGV ) {
-
help();
-
exit;
-
}
-
-
$file=pop;
-
-
if (!-e $file) {
-
print "File $file not found, aborting.\n";
-
print "Calling script with no arguments prints help\n" ;
-
exit;
-
}
-
-
$arg = join " ",@ARGV;
-
-
$mtime = (stat($file))[9];
-
($sec,$min,$hour,$day,$mon,$year,$wday,$yday) = localtime($mtime);
-
$year+=1900;
-
$mon++;
-
if ($hour> 12) {
-
$hour_ampm=abs($hour - 12); $p="pm"
-
} else {
-
$hour_ampm="$hour"; $p="am"
-
}
-
$ssec=&paf($sec);
-
$mmin=&paf($min);
-
$hhour=&paf($hour);
-
$hhour_ampm=&paf($hour_ampm);
-
$dday=&paf($day);
-
$mmon=&paf($mon);
-
$yyear=substr($year,2,2);
-
-
$arg=" ".$arg;
-
$result = "";
-
$translated = 0;
-
-
foreach $letter (split //,$arg) {
-
-
$translated = 0;
-
-
# time
-
&pouet("s",$ssec);
-
&pouet("t",$sec);
-
&pouet("m",$mmin);
-
&pouet("u",$min);
-
&pouet("h",$hhour);
-
&pouet("g",$hour);
-
&pouet("i",$hhour_ampm);
-
&pouet("k",$hour_ampm);
-
&pouet("p",$p);
-
# day
-
&pouet("d",$dday);
-
&pouet("D",$day);
-
&pouet("W",($weekday{"$wday"}));
-
&pouet("w",lc($weekday{$wday}));
-
&pouet("X",$lweekday{$wday});
-
&pouet("x",lc($lweekday{$wday}));
-
# month
-
&pouet("M",$mmon);
-
&pouet("T",$mon);
-
&pouet("N",$month{$mon});
-
&pouet("n",lc($month{$mon}));
-
&pouet("O",$lmonth{$mon});
-
&pouet("o",lc($lmonth{$mon}));
-
# year
-
&pouet("y",$year);
-
&pouet("Y",$yyear);
-
-
$previousletter=$letter;
-
if (!$translated) { $result.=$letter }
-
}
-
-
$result=~s/^ //;
-
$result=~s/\^//;
-
-
print "$result";
-
-
-
sub pouet {
-
my ($in,$out) = @_ ;
-
if ($previousletter ne "^" && $letter eq $in) {
-
$letter =~ s/$in/$out/g ;
-
$result .= $letter;
-
$translated=1;
-
}
-
return
-
}
-
-
sub paf {
-
my ($in)=@_ ;
-
if ($in <10) {$in="0".$in}
-
return $in;
-
}
-
-
sub help {
-
print <<ENDHELP;
-
Output modification time and date in various format.
-
-
Usage :
-
$0 <optional format> [file]
-
-
Format arguments are :
-
arg value (output)
-
> Time
-
s : seconds (01-59)
-
t : seconds (1-59)
-
m : minutes (01-12)
-
u : minutes (1-12)
-
h : hour (01-24)
-
g : hour (1-24)
-
i : hour (01-12)
-
k : hour (1-12)
-
p : am or pm (am-pm)
-
> Day
-
d : monthday (01-31)
-
D : monthday (1-31)
-
W : weekday (Lun-Dim)
-
w : weekday (lun-dim)
-
X : weekday (Lundi-Dimanche)
-
x : weekday (lundi-dimanche)
-
> Month
-
M : month (01-12)
-
T : month (1-12)
-
N : month (Jan-Dec)
-
n : month (jan-dec)
-
O : month (Janvier-Decembre)
-
o : month (janvier-decembre)
-
> Year
-
y : year (02)
-
Y : year (2002)
-
-
To escape an argument, use ^
-
Edit the script ($0) to translate weekdays and months if you wish.
-
-
Examples :
-
$0 ymd tutu.txt
-
$0 m-d tutu.txt
-
$0 D M Y tutu.txt
-
$0 ^dd^mm^yy tutu.txt
-
-
ENDHELP
-
}
Related posts
Metastuff
No Comment yet
No comments yet. You could get "First Post!" which would bring you fame, luxury cars and chicks.
This entry "Datemodif perl script" was posted on 07/12/2003 at 11:59 am and is tagged with Code, Linux, Perl
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site