{"id":21,"date":"2003-12-07T11:59:51","date_gmt":"2003-12-07T09:59:51","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2003\/12\/07\/datemodif-perl-script\/"},"modified":"2007-04-22T11:30:20","modified_gmt":"2007-04-22T09:30:20","slug":"datemodif-perl-script","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2003\/12\/datemodif-perl-script\/","title":{"rendered":"Datemodif perl script"},"content":{"rendered":"<p>This perl script prints modification time of a file, in various format.<br \/>\n<strong>Examples <\/strong>: <\/p>\n<p>datemodif y-m-d mystuff.log &rarr; 2003-30-06<br \/>\ndatemodif d\/M\/Y-h.m.s mystuff.log &rarr; 06\/12\/03-21.30.02<br \/>\ndatemodif X d O Y mystuff.log &rarr; Samedi 06 D\u00e9cembre 03 <\/p>\n<p><!--more--><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-perl line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-perl\">#!\/usr\/bin\/perl\r\n\r\n# datemodif &lt;format&gt; &lt;fichier&gt;\r\n# ex : datemodif ymd tutu.txt\r\n# ex : datemodif m-d tutu.txt\r\n# ex : datemodif &quot;D M Y&quot; tutu.txt\r\n#\r\n# Heure de modif :\r\n# s : 01-59\r\n# m : 01-12\r\n# h : 01-24\r\n# i : 01-12 am\/pm\r\n#\r\n# Jour de modif :\r\n# d : 01-31\r\n# D : Lun-Dim\r\n# e : lun-dim\r\n# F : Lundi - Dimanche\r\n# g : lundi - dimanche\r\n# M : 1-12\r\n# N : Jan-Dec\r\n# n ; jan-dec\r\n# O : Janvier-Decembre\r\n# o : janvier-decembre\r\n# y : 02\r\n# Y : 2002\r\n\r\n%weekday=(\r\n    &quot;0&quot;=&gt;&quot;Dim&quot;,\r\n    &quot;1&quot;=&gt;&quot;Lun&quot;,\r\n    &quot;2&quot;=&gt;&quot;Mar&quot;,\r\n    &quot;3&quot;=&gt;&quot;Mer&quot;,\r\n    &quot;4&quot;=&gt;&quot;Jeu&quot;,\r\n    &quot;5&quot;=&gt;&quot;Ven&quot;,\r\n    &quot;6&quot;=&gt;&quot;Sam&quot;,\r\n);\r\n\r\n%lweekday=(\r\n    &quot;0&quot;=&gt;&quot;Dimanche&quot;,\r\n    &quot;1&quot;=&gt;&quot;Lundi&quot;,\r\n    &quot;2&quot;=&gt;&quot;Mardi&quot;,\r\n    &quot;3&quot;=&gt;&quot;Mercredi&quot;,\r\n    &quot;4&quot;=&gt;&quot;Jeudi&quot;,\r\n    &quot;5&quot;=&gt;&quot;Vendredi&quot;,\r\n    &quot;6&quot;=&gt;&quot;Samedi&quot;,\r\n);\r\n\r\n%month=(\r\n    &quot;1&quot;=&gt;&quot;Jan&quot;,\r\n    &quot;2&quot;=&gt;&quot;Fev&quot;,\r\n    &quot;3&quot;=&gt;&quot;Mar&quot;,\r\n    &quot;4&quot;=&gt;&quot;Avr&quot;,\r\n    &quot;5&quot;=&gt;&quot;Mai&quot;,\r\n    &quot;6&quot;=&gt;&quot;Juin&quot;,\r\n    &quot;7&quot;=&gt;&quot;Juil&quot;,\r\n    &quot;8&quot;=&gt;&quot;Aout&quot;,\r\n    &quot;9&quot;=&gt;&quot;Sept&quot;,\r\n    &quot;10&quot;=&gt;&quot;Oct&quot;,\r\n    &quot;11&quot;=&gt;&quot;Nov&quot;,\r\n    &quot;12&quot;=&gt;&quot;Dec&quot;,\r\n);\r\n\r\n\r\n%lmonth=(\r\n    &quot;1&quot;=&gt;&quot;Janvier&quot;,\r\n    &quot;2&quot;=&gt;&quot;F\u00e9vrier&quot;,\r\n    &quot;3&quot;=&gt;&quot;Mars&quot;,\r\n    &quot;4&quot;=&gt;&quot;Avril&quot;,\r\n    &quot;5&quot;=&gt;&quot;Mai&quot;,\r\n    &quot;6&quot;=&gt;&quot;Juin&quot;,\r\n    &quot;7&quot;=&gt;&quot;Juillet&quot;,\r\n    &quot;8&quot;=&gt;&quot;Aout&quot;,\r\n    &quot;9&quot;=&gt;&quot;Septembre&quot;,\r\n    &quot;10&quot;=&gt;&quot;Octobre&quot;,\r\n    &quot;11&quot;=&gt;&quot;Novembre&quot;,\r\n    &quot;12&quot;=&gt;&quot;D\u00e9cembre&quot;,\r\n);\r\n\r\nif (!@ARGV ) { \r\n\thelp();\r\n\texit;\r\n}\r\n\r\n$file=pop;\r\n\r\nif (!-e $file) {\r\n   print &quot;File $file not found, aborting.\\n&quot;;\r\n   print &quot;Calling script with no arguments prints help\\n&quot; ;\r\n   exit;\r\n}\r\n\r\n$arg = join &quot; &quot;,@ARGV;\r\n\r\n$mtime = (stat($file))[9];\r\n($sec,$min,$hour,$day,$mon,$year,$wday,$yday) = localtime($mtime);\r\n$year+=1900;\r\n$mon++;\r\nif ($hour &gt; 12) {\r\n   $hour_ampm=abs($hour - 12); $p=&quot;pm&quot;\r\n} else {\r\n   $hour_ampm=&quot;$hour&quot;; $p=&quot;am&quot;\r\n}\r\n$ssec=&amp;paf($sec);\r\n$mmin=&amp;paf($min);\r\n$hhour=&amp;paf($hour);\r\n$hhour_ampm=&amp;paf($hour_ampm);\r\n$dday=&amp;paf($day);\r\n$mmon=&amp;paf($mon);\r\n$yyear=substr($year,2,2);\r\n\r\n$arg=&quot; &quot;.$arg;\r\n$result = &quot;&quot;;\r\n$translated = 0;\r\n\r\nforeach $letter (split \/\/,$arg) {\r\n\r\n$translated = 0;\r\n\r\n# time\r\n&amp;pouet(&quot;s&quot;,$ssec);\r\n&amp;pouet(&quot;t&quot;,$sec);\r\n&amp;pouet(&quot;m&quot;,$mmin);\r\n&amp;pouet(&quot;u&quot;,$min);\r\n&amp;pouet(&quot;h&quot;,$hhour);\r\n&amp;pouet(&quot;g&quot;,$hour);\r\n&amp;pouet(&quot;i&quot;,$hhour_ampm);\r\n&amp;pouet(&quot;k&quot;,$hour_ampm);\r\n&amp;pouet(&quot;p&quot;,$p);\r\n# day\r\n&amp;pouet(&quot;d&quot;,$dday);\r\n&amp;pouet(&quot;D&quot;,$day);\r\n&amp;pouet(&quot;W&quot;,($weekday{&quot;$wday&quot;}));\r\n&amp;pouet(&quot;w&quot;,lc($weekday{$wday}));\r\n&amp;pouet(&quot;X&quot;,$lweekday{$wday});\r\n&amp;pouet(&quot;x&quot;,lc($lweekday{$wday}));\r\n# month\r\n&amp;pouet(&quot;M&quot;,$mmon);\r\n&amp;pouet(&quot;T&quot;,$mon);\r\n&amp;pouet(&quot;N&quot;,$month{$mon});\r\n&amp;pouet(&quot;n&quot;,lc($month{$mon}));\r\n&amp;pouet(&quot;O&quot;,$lmonth{$mon});\r\n&amp;pouet(&quot;o&quot;,lc($lmonth{$mon}));\r\n# year\r\n&amp;pouet(&quot;y&quot;,$year);\r\n&amp;pouet(&quot;Y&quot;,$yyear);\r\n\r\n$previousletter=$letter;\r\nif (!$translated) { $result.=$letter }\r\n}\r\n\r\n$result=~s\/^ \/\/;\r\n$result=~s\/\\^\/\/;\r\n\r\nprint &quot;$result&quot;;\r\n\r\n\r\nsub pouet {\r\nmy ($in,$out) = @_ ;\r\nif ($previousletter ne &quot;^&quot; &amp;&amp; $letter eq $in) {\r\n\t$letter =~ s\/$in\/$out\/g ;\r\n\t$result .= $letter;\r\n\t$translated=1;\r\n}\r\nreturn\r\n}\r\n\r\nsub paf {\r\nmy ($in)=@_ ;\r\nif ($in &lt; 10) {$in=&quot;0&quot;.$in}\r\nreturn $in;\r\n}\r\n\r\nsub help {\r\nprint &lt;&lt;ENDHELP;\r\nOutput modification time and date in various format.\r\n\r\nUsage :\r\n$0 &lt;optional format&gt; [file]\r\n\r\nFormat arguments are :\r\n     arg   value   (output)\r\n &gt; Time \r\n      s : seconds  (01-59)\r\n      t : seconds  (1-59)\r\n      m : minutes  (01-12)\r\n      u : minutes  (1-12)\r\n      h : hour     (01-24)\r\n      g : hour     (1-24)\r\n      i : hour     (01-12)\r\n      k : hour     (1-12)\r\n      p : am or pm (am-pm)\r\n &gt; Day \r\n      d : monthday (01-31)\r\n      D : monthday (1-31)\r\n      W : weekday  (Lun-Dim)\r\n      w : weekday  (lun-dim)\r\n      X : weekday  (Lundi-Dimanche)\r\n      x : weekday  (lundi-dimanche)\r\n &gt; Month\r\n      M : month    (01-12)\r\n      T : month    (1-12)\r\n      N : month    (Jan-Dec)\r\n      n : month    (jan-dec)\r\n      O : month    (Janvier-Decembre)\r\n      o : month    (janvier-decembre)\r\n &gt; Year\r\n      y : year     (02)\r\n      Y : year     (2002)\r\n\r\nTo escape an argument, use ^\r\nEdit the script ($0) to translate weekdays and months if you wish.\r\n\r\nExamples :\r\n $0 ymd tutu.txt\r\n $0 m-d tutu.txt\r\n $0 D M Y tutu.txt\r\n $0 ^dd^mm^yy tutu.txt\r\n\r\nENDHELP\r\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Retrieves modification date of a file, returns it in various formats<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,3,9],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","tag-code","tag-linux","tag-perl"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}