In: , ,
On: 2003 / 12 / 07 Viewed: 28567 times

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:
  1. #!/usr/bin/perl
  2.  
  3. # datemodif <format> <fichier>
  4. # ex : datemodif ymd tutu.txt
  5. # ex : datemodif m-d tutu.txt
  6. # ex : datemodif "D M Y" tutu.txt
  7. #
  8. # Heure de modif :
  9. # s : 01-59
  10. # m : 01-12
  11. # h : 01-24
  12. # i : 01-12 am/pm
  13. #
  14. # Jour de modif :
  15. # d : 01-31
  16. # D : Lun-Dim
  17. # e : lun-dim
  18. # F : Lundi - Dimanche
  19. # g : lundi - dimanche
  20. # M : 1-12
  21. # N : Jan-Dec
  22. # n ; jan-dec
  23. # O : Janvier-Decembre
  24. # o : janvier-decembre
  25. # y : 02
  26. # Y : 2002
  27.  
  28. %weekday=(
  29.     "0"=>"Dim",
  30.     "1"=>"Lun",
  31.     "2"=>"Mar",
  32.     "3"=>"Mer",
  33.     "4"=>"Jeu",
  34.     "5"=>"Ven",
  35.     "6"=>"Sam",
  36. );
  37.  
  38. %lweekday=(
  39.     "0"=>"Dimanche",
  40.     "1"=>"Lundi",
  41.     "2"=>"Mardi",
  42.     "3"=>"Mercredi",
  43.     "4"=>"Jeudi",
  44.     "5"=>"Vendredi",
  45.     "6"=>"Samedi",
  46. );
  47.  
  48. %month=(
  49.     "1"=>"Jan",
  50.     "2"=>"Fev",
  51.     "3"=>"Mar",
  52.     "4"=>"Avr",
  53.     "5"=>"Mai",
  54.     "6"=>"Juin",
  55.     "7"=>"Juil",
  56.     "8"=>"Aout",
  57.     "9"=>"Sept",
  58.     "10"=>"Oct",
  59.     "11"=>"Nov",
  60.     "12"=>"Dec",
  61. );
  62.  
  63.  
  64. %lmonth=(
  65.     "1"=>"Janvier",
  66.     "2"=>"Février",
  67.     "3"=>"Mars",
  68.     "4"=>"Avril",
  69.     "5"=>"Mai",
  70.     "6"=>"Juin",
  71.     "7"=>"Juillet",
  72.     "8"=>"Aout",
  73.     "9"=>"Septembre",
  74.     "10"=>"Octobre",
  75.     "11"=>"Novembre",
  76.     "12"=>"Décembre",
  77. );
  78.  
  79. if (!@ARGV ) {
  80.     help();
  81.     exit;
  82. }
  83.  
  84. $file=pop;
  85.  
  86. if (!-e $file) {
  87.    print "File $file not found, aborting.\n";
  88.    print "Calling script with no arguments prints help\n" ;
  89.    exit;
  90. }
  91.  
  92. $arg = join " ",@ARGV;
  93.  
  94. $mtime = (stat($file))[9];
  95. ($sec,$min,$hour,$day,$mon,$year,$wday,$yday) = localtime($mtime);
  96. $year+=1900;
  97. $mon++;
  98. if ($hour> 12) {
  99.    $hour_ampm=abs($hour - 12); $p="pm"
  100. } else {
  101.    $hour_ampm="$hour"; $p="am"
  102. }
  103. $ssec=&paf($sec);
  104. $mmin=&paf($min);
  105. $hhour=&paf($hour);
  106. $hhour_ampm=&paf($hour_ampm);
  107. $dday=&paf($day);
  108. $mmon=&paf($mon);
  109. $yyear=substr($year,2,2);
  110.  
  111. $arg=" ".$arg;
  112. $result = "";
  113. $translated = 0;
  114.  
  115. foreach $letter (split //,$arg) {
  116.  
  117. $translated = 0;
  118.  
  119. # time
  120. &pouet("s",$ssec);
  121. &pouet("t",$sec);
  122. &pouet("m",$mmin);
  123. &pouet("u",$min);
  124. &pouet("h",$hhour);
  125. &pouet("g",$hour);
  126. &pouet("i",$hhour_ampm);
  127. &pouet("k",$hour_ampm);
  128. &pouet("p",$p);
  129. # day
  130. &pouet("d",$dday);
  131. &pouet("D",$day);
  132. &pouet("W",($weekday{"$wday"}));
  133. &pouet("w",lc($weekday{$wday}));
  134. &pouet("X",$lweekday{$wday});
  135. &pouet("x",lc($lweekday{$wday}));
  136. # month
  137. &pouet("M",$mmon);
  138. &pouet("T",$mon);
  139. &pouet("N",$month{$mon});
  140. &pouet("n",lc($month{$mon}));
  141. &pouet("O",$lmonth{$mon});
  142. &pouet("o",lc($lmonth{$mon}));
  143. # year
  144. &pouet("y",$year);
  145. &pouet("Y",$yyear);
  146.  
  147. $previousletter=$letter;
  148. if (!$translated) { $result.=$letter }
  149. }
  150.  
  151. $result=~s/^ //;
  152. $result=~s/\^//;
  153.  
  154. print "$result";
  155.  
  156.  
  157. sub pouet {
  158. my ($in,$out) = @_ ;
  159. if ($previousletter ne "^" && $letter eq $in) {
  160.     $letter =~ s/$in/$out/g ;
  161.     $result .= $letter;
  162.     $translated=1;
  163. }
  164. return
  165. }
  166.  
  167. sub paf {
  168. my ($in)=@_ ;
  169. if ($in <10) {$in="0".$in}
  170. return $in;
  171. }
  172.  
  173. sub help {
  174. print <<ENDHELP;
  175. Output modification time and date in various format.
  176.  
  177. Usage :
  178. $0 <optional format> [file]
  179.  
  180. Format arguments are :
  181.      arg   value   (output)
  182. > Time
  183.       s : seconds  (01-59)
  184.       t : seconds  (1-59)
  185.       m : minutes  (01-12)
  186.       u : minutes  (1-12)
  187.       h : hour     (01-24)
  188.       g : hour     (1-24)
  189.       i : hour     (01-12)
  190.       k : hour     (1-12)
  191.       p : am or pm (am-pm)
  192. > Day
  193.       d : monthday (01-31)
  194.       D : monthday (1-31)
  195.       W : weekday  (Lun-Dim)
  196.       w : weekday  (lun-dim)
  197.       X : weekday  (Lundi-Dimanche)
  198.       x : weekday  (lundi-dimanche)
  199. > Month
  200.       M : month    (01-12)
  201.       T : month    (1-12)
  202.       N : month    (Jan-Dec)
  203.       n : month    (jan-dec)
  204.       O : month    (Janvier-Decembre)
  205.       o : month    (janvier-decembre)
  206. > Year
  207.       y : year     (02)
  208.       Y : year     (2002)
  209.  
  210. To escape an argument, use ^
  211. Edit the script ($0) to translate weekdays and months if you wish.
  212.  
  213. Examples :
  214.  $0 ymd tutu.txt
  215.  $0 m-d tutu.txt
  216.  $0 D M Y tutu.txt
  217.  $0 ^dd^mm^yy tutu.txt
  218.  
  219. ENDHELP
  220. }

Related posts

Metastuff

This entry "Datemodif perl script" was posted on 07/12/2003 at 11:59 am and is tagged with , ,
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

No Comment yet

No comments yet. You could get "First Post!" which would bring you fame, luxury cars and chicks.

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

 Dont ban me. »