package DateFrench; # remd: [utf8] Ce module permet d'otenir dates et heures en français en * # plusieurs formats, (adaptable à vos besoins). Il donne donc la date * # et/ou l'heure pour "maintenant" ainsi que celle de la dernière modif, * # création, ouverture de tel ou tel fichier comme la fonction X (M,A,C) * #---------------------------------------------------------------------- * # Donc: $french_date_now donnera la date heure de maintenant au format * # $format_affichage (si omis par/defaut: Samedi 15 Novembre 2008 12:45) * # usage: * # use Datefrench; * # $french_date_now = maintenant($format_affichage); * # -------------------------------------------------------------------- * # pour obtenir la date de création, de dernière ouverture ou d'accés a * # un fichier, au format $format_affichage (par defaut idem que + haut) * # usage: * # use DateFrench; * # $fr_date_file = datefichier($format_affichage, $opt_date_file, $file);* #-----------------------------------------------------------------------* # Pessac le 27 Novembre 2008 écrit par Alain Adelmar aadelmar@free.fr * #-----------------------------------------------------------------------* # attention:l'opt_date_file 3 donne la date création et pas une # concatenation de trois date, l'erreur est en commantaire, attention # revoir de retablir because => un pb et en 10 sec 85Mo de données. #************************************************************************ # $id use Exporter; our @ISA = qw( Exporter ); our @EXPORT = qw( $format_affichage $opt_date_file $file maintenant datefichier ); #my $reflotime = []; #my $rstatfile = []; my $lbl = ""; our $format_affichage = @_; our $opt_date_file = @_; our $file = @_; sub convert_time; our $french_date_now = maintenant($format_affichage); our $fr_date_file = datefichier($format_affichage, $opt_date_file, $file); sub maintenant { my($format_affichage) = @_; @loctime = localtime(time); $reflotime = \@loctime; $now= 1; &convert_time; } sub datefichier { my($format_affichage, $opt_date_file, $file)= @_; $now = 0; @{$rstatfile} = stat ($file); #-----------------choix des options if ($opt_date_file eq "1") { @loctime = localtime($rstatfile->[9]); $lbl = "Modif"; &convert_time($format_affichage); } elsif ($opt_date_file eq "2") { @loctime = localtime($rstatfile->[10]); $lbl = "Change"; &convert_time("$format_affichage"); } elsif ($opt_date_file eq "3") { # $now = 1; @loctime = localtime($rstatfile->[8]); $reflotime = \@loctime; $lbl = "Acces"; &convert_time("$format_affichage"); # $fr_date_file .= "$lbl " . "$fr_date_file" . " _"; # @loctime = localtime($rstatfile->[9]); # $reflotime = \@loctime; # $lbl = "Modif"; # &convert_time("$format_affichage"); # $fr_date_file .= "$lbl " . "$fr_date_file" . " _"; # @loctime = localtime($rstatfile->[10]); # $reflotime = \@loctime; # $lbl = "Change"; # &convert_time; # $fr_date_file .= "$lbl " . "$fr_date_file"; } else { @loctime = localtime($rstatfile->[8]); $reflotime = \@loctime; $lbl = "Acces"; &convert_time; } } sub convert_time { my($format_affichage)= @_; @lsjour = qw( Dimanche Lundi Mardi Mercredi Jeudi Vendredi Samedi ); for (0 ..6) { $fj{$_} = $lsjour[$_]; } @lsmois = qw( Janvier Février Mars Avril Mai Juin Juillet Aout Septembre Octobre Novembre Décembre ); for (0..11) { $fm{$_} = $lsmois[$_]; } $reflotime = \@loctime; for ($reflotime) { $sjour = $fj{$reflotime->[6]}; $sjour_sm = "\l$sjour"; $abrev_sjour = substr($sjour, 0, 3); $smois = "$fm{$reflotime->[4]}"; $smois_sm = "\l$smois"; $abrev_smois = substr($smois, 0, 3); if ($reflotime->[5] <= 100) { #$paa est le champ ann�e 04 pour 2004, 99 pour 1999 $paa = $reflotime->[5]; $aa = "19" . "$reflotime->[5]"; } else { $paa = "0" . ($reflotime->[5] - 100); $aa = "200" . ($reflotime->[5] - 100); } $haa = $hchar{"$reflotime->[5]" - 70}; # mois idem localtime(x)[4] + 1 (because janvier = 0) if ($reflotime->[4] <= 8) { $mm = "0" . ($reflotime->[4] + 1); } else { $mm = ("$reflotime->[4]" + 1); } $hmm = $hchar{("$reflotime->[4]" + 2)}; # jour idem localtime(x)[3] + 1 (because le 1 commence a 0) if ($reflotime->[3] <= 8) { $jj = "0" . "$reflotime->[3]"; } else { $jj = $reflotime->[3]; } $hjj = $hchar{("$reflotime->[3]" + 1)}; # les heures if ($reflotime->[2] <= 9) { $hh = "0" . "$reflotime->[2]"; } else { $hh = $reflotime->[2]; } $hhh = $hchar{$reflotime->[2]}; # les minutes if ($reflotime->[1] <= 9) { $mn = "0" . "$reflotime->[1]"; } else { $mn = $reflotime->[1]; } $hmn = $hchar{"$reflotime->[1]"}; # les secondes if ($reflotime->[0] <= 9) { $ss = "0" . "$reflotime->[0]"; } else { $ss = $reflotime->[0]; } $hss = $hchar{"$reflotime->[0]"}; } if ($format_affichage == "1") { if ($now == 1) { #----------------------------------------- 1 pour -- Mardi 03 Mars 2004 $fd = "$sjour $jj $smois $aa"; return $fd; } else { $fr_date_file = "$sjour $jj $smois $aa"; return $fr_date_file; } } elsif ($format_affichage eq "2") { if ($now == 1) { #----------------------------------------- 2 pour -- Mardi 03 Mars 2004 15:09:28 $fd = "$sjour $jj $smois $aa $hh:$mn:$ss"; return $fd; } else { $fr_date_file = "$sjour $jj $smois $aa $hh:$mn:$ss"; return $fr_date_file; } } elsif ($format_affichage eq "3") { if ($now == 1) { #------------------------------------------ 3 pour -- lun 09 Oct 20O5 $fd = "$abrev_sjour $jj $abrev_smois $aa"; return $fd; } else { $fr_date_file = "$abrev_sjour $jj $abrev_smois $aa"; return $fr_date_file; } } elsif ($format_affichage eq "4") { if ($now == 1) { #------------------------------------------- 4 pour -- lun 09 Oct 2005 - 15h 59min $fd = "$abrev_sjour $jj $abrev_smois $aa -" . " $hh" . "h $mm" . "min"; return $fd; } else { $fr_date_file = "$abrev_sjour $jj $abrev_smois $aa -" . " $hh" . "h $mm" . "min"; return $fr_date_file; } } elsif ($format_affichage eq "5") { if ($now == 1) { #--------------------------------------------- 5 pour -- mardi 03 Mars 2004 $fd = "$sjour_sm $jj $smois $aa"; return $fd; } else { $fr_date_file = "$sjour_sm $jj $smois $aa"; return $fr_date_file; } } elsif ($format_affichage eq "6") { if ($now == 1) { #--------------------------------------------- 6 pour -- mardi 03 Mars 2004 à 15:38:45 $fd = "$sjour_sm $jj $smois $aa à $hh:$mm:$ss"; return $fd; } else { $fr_date_file = "$sjour_sm $jj $smois $aa à $hh:$mm:$ss"; return $fr_date_file; } } elsif ($format_affichage eq "7") { if ($now == 1) { #----------------------------------------------- 7 pour -- 040309152532 $fd = "$paa" . "$mm" . "$jj" . "$hh" . "$mn" . "$ss"; return $fd; } else { $fr_date_file = "$paa" . "$mm" . "$jj" . "$hh" . "$mn" . "$ss"; return $fr_date_file; } } elsif ($format_affichage eq "8") { if ($now == 1) { #----------------------------------------------- 8 pour -- 20040309-XHU $fd = "$aa" . "$mm" . "$jj" . "-$hhh" . "$hmn" . "$hss"; return $fd; } else { $fr_date_file = "$aa" . "$mm" . "$jj" . "$hhh" . "$hmn" . "$hss"; return $fr_date_file; } } elsif ($format_affichage eq "9") { if ($now == 1) { #------------------------------------------------ 9 pour -- G-0309-HTY $fd = "$haa-" . "$mm" . "$jj-" . "$hhh" . "$hmn" . "$hss"; return $fd; } else { $fr_date_file = "$haa-" . "$mm" . "$jj-" . "$hhh" . "$hmn" . "$hss"; return $fr_date_file; } } elsif ($format_affichage eq "10") { if ($now == 1) { #------------------------------------------------ 10 pour -- HMJU7X $fd = "$haa" . "$hmm" . "$hjj" . "$hhh" . "$hmn" . "$hss"; return $fd; } else { $fr_date_file = "$haa" . "$hmm" . "$hjj" . "$hhh" . "$hmn" . "$hss"; return $fr_date_file; } } elsif ($format_affichage eq "11") { if ($now == 1) { #------------------------------------------------ 11 pour -- Jeudi 18/03/05 $fd = "$sjour $jj\/$mm\/$aa"; return $fd; } else { $fr_date_file = "$sjour $jj\/$mm\/$aa"; return $fr_date_file; } } elsif ($format_affichage eq "12") { if ($now == 1) { #------------------------------------------------- 12 pour -- Lundi 09/10/05 18:24:11 $fd = "$sjour $jj\/$mm\/$aa $hh:$mn:$ss"; return $fd; } else { $fr_date_file = "$sjour $jj\/$mm\/$aa $hh:$mn:$ss"; return $fr_date_file; } } elsif ($format_affichage eq "13") { if ($now == 1) { #-------------------------------------------------- 13 pour -- lun 09 Oct $fd = "$abrev_sjour $jj $abrev_smois"; return $fd; } else { $fd = "$abrev_sjour $jj $abrev_smois"; return $fr_date_file; } } elsif ($format_affichage eq "14") { if ($now == 1) { #-------------------------------------------------- 14 pour -- Octobre 2005 $fd = "$smois $aa"; return $fd; } else { $fr_date_file = "$smois $aa"; return $fr_date_file; } } elsif ($format_affichage eq "15") { if ($now == 1) { #-------------------------------------------------- 15 pour -- Avr/2002 $fd = "$smois//$aa"; return $fd; } else { $fr_date_file = $fd = "$smois//$aa"; return $fr_date_file; } } elsif ($format_affichage eq "16") { if ($now == 1) { #-------------------------------------------------- 16 pour -- mardi 09 Jan 04 $fd = "$sjour_sm $jj $abrev_smois $paa"; return $fd; } else { $fr_date_file = "$sjour_sm $jj $abrev_smois $paa"; return $fr_date_file; } } elsif ($format_affichage eq "17") { if ($now == 1) { #-------------------------------------------------- 17 pour -- mardi 09 Jan 04 15:36:45 $fd = "$sjour_sm $jj $abrev_smois $paa $hh:$mm:$ss"; return $fd; } else { $fr_date_file = "$sjour_sm $jj $abrev_smois $paa $hh:$mm:$ss"; return $fr_date_file; } } elsif ($format_affichage eq "18") { if ($now == 1) { #-------------------------------------------------- 18 pour -- mardi 09 janvier 2008 /15:36:45 $fd = "$sjour_sm $jj $smois_sm $paa \/$hh:$mm:$ss"; return $fd; } else { $fr_date_file = "$sjour_sm $jj $smois_sm $paa \/$hh:$mm:$ss"; return $fr_date_file; } } elsif ($format_affichage eq "19") { if ($now == 1) { #-------------------------------------------------- 19 pour -- |_ mardi 09 Janvier 2008 _|_15:36:45_| $fd = "\t\t" . '|_ ' . "$sjour_sm $jj $smois $aa " . '_|_ ' . "$hh:$mm:$ss " . '_|'; return $fd; } else { $fr_date_file = "\t\t" . '|_ ' . "$sjour_sm $jj $smois $aa " . '_|_ ' . "$hh:$mm:$ss " . '_|'; return $fr_date_file; } } elsif ($format_affichage eq "20") { if ($now == 1) { #------------------------------------------------- 20 pour (sortie fixe 23c)-- lun 09 Oct 05 |15:59:45 $fd = "$abrev_sjour $jj $abrev_smois $paa \|" . "$hh" . "$mm" . "$ss"; return $fd; } else { $fr_date_file = "$abrev_sjour $jj $abrev_smois $aa \|" . "$hh" . "$mm" . "$ss"; return $fr_date_file; } } elsif ($format_affichage eq "21") { if ($now == 1) { #------------------------------------------------- 21 pour (sortie fixe 24c)-- mar 03 Mar 2002-18:38:45 $fd = "$abrev_sjour $jj $abrev_smois $aa -" . "$hh:$mm:$ss"; return $fd; } else { $fr_date_file = "$abrev_sjour $jj $abrev_smois $aa -" . "$hh:$mm:$ss"; return $fr_date_file; } } elsif ($format_affichage eq "22") { if ($now == 1) { #------------------------------------------------- 22 pour (sortie fixe 15c)-- 03Mar2002-18:38:45 $fd = "$jj$abrev_smois$aa-$hh:$mm:$ss"; return $fd; } else { $fr_date_file = "$jj$abrev_smois$aa-$hh:$mm:$ss"; return $fr_date_file; } } else { if ($now eq "1") { #----------- defaut ------------------------------ pour -- Mardi 09 Mars 2004 15:09 $fd = "$sjour $jj $smois $aa $hh:$mn"; return $fd; } else { # par defaut (si pas donner) $fr_date_file = "$sjour $jj $smois $aa $hh:$mn"; return $fr_date_file; } } } 1; =head1 NOM B - donne l'heure et la date en français dans tout les formats, et ce pour I ou pour les date de dernier Accés, Modification ou Création. =head1 SYNTAXE use Datefr; $french_date_now = maintenant($format_affichage); print "Salut nous sommes le $french_date_now\n"; # donne la date et l'heure du moment sous le format par defaut >Salut nous sommes le Samedi 15 Novembre 2008 15:05 $file = '/usr/lib/perl5/5.8.8/Datefr.pm'; $fr_date_file = datefile($format_affichage, $opt_date_file, $file); print "Ce module a été conçu le $fr_date_file\n"; >Ce module a été conçu le 10 Novembre 2008 à 23:15 # idem suivant $opt_date_file(accés,modif,change). 0 = dernier accés (defaut) 1 = derniere modifications 2 dernier changement 3 = les trois, c'est a dire 3 dates exemple: $fr_date_file = datefichier(1, 1, "mon_fichier.log"); ou $fr_date_file = datefichier("DlMl_no-h", "1", "mon_fichier.log"); print "$fr_date_file\n"; donnera : Samedi 6 Aout 2005 Dans l'exemple au dessus DlMl_no-h signifie (Iay Iong Iois Iong I<_no-h> pour no hours) # donne la date du fichier $file suivant ($opt_date_file: dernier access, modif, chang) # au format défini par $format_affichage: # Résultat -------- $format_affichage format fixe # Mardi 09 Janvier 2004 15:09:28 -------- I # Mardi 03 Mars 2004 -------- "1" ou "DlMl_no-h" # Mardi 03 Mars 2004 15:09:28 -------- "2" ou "DlMl" # jeu 09 Oct -------- "3" ou "dcMc_no-h" oui (10c) # jeu 09 Oct 18h24:11 -------- "4" ou "dcMc" oui (19c) # mardi 03 Mars 2004 -------- "5" ou "dlMl_no-h" # mardi 03 Mars 2004 à 15:38:45 -------- "6" ou "dlMl" # 040309152532 -------- "7" ou "chiffre" oui (12c) # 20040309-XHU -------- "8" ou "chiffre-hexa" oui (12c) # G-0309-HTY -------- "9" ou "hexa-char" oui (10c) # HTMXHU -------- "10" ou "alpha" oui ( 6c) # Jeudi 18/03/05 -------- "11" ou "Dl_no-h" # Lundi 09/10/05 18:24:11 -------- "12" ou "Dl" # lun 09 Oct -------- "13" ou "dcMc_no-a" oui (10c) # Octobre 2005 -------- "14" ou "Ml_no-d" # Avr/2002 -------- "15" ou "Mc_no-d" oui (8c) # mardi 09 Jan 04 -------- "16" ou "dlmc_no-h" # mardi 09 Jan 04 15:36:45 -------- "17" ou "dlmc" # mardi 09 janvier 2008 /15:36:45 -------- "18" # |_ mardi 09 Janvier 2008 _|_15:36:45_| -- "19" # lun 09 Oct 05 |15:59:45 -------- "20" oui (23c) # mar 03 Mar 2002-18:38:45 -------- "21" oui (24c) # 09Oct2005 15:59 -------- "22" oui (15c) # Mardi 09 Mars 2004 15:09 -------- (defaut) Mercredi 11/12/2003 18:45 ou 20031211 ou 1031211184503 ou M-1211-UHX ou Mer 09 Oct 12h45:28, etc.... =head1 DESCRIPTION La variable scalaire retourné par le module sera une date en français formater selon votre besoin pour: B avec la ligne: B<$fr_date_file = maintenant($format_affichage, $opt_date_file, $file)> ou B<$format_affichage> est le numéro (0-17) qui représente le format choisi mais vous pouvez aussi créer votre propre format, si si c'est facile. B<$opt_date_file> est un chiffre de 0 a 2 representant: 0 date de dernier acces 1 date de dernière modification 2 date du dernier changement d inode B<$file> est le nom du fichier vous pouvez aussi appelé le libellé Acces, Modif ou Création qui ce trouve dans $lbl, exemple: print "date du fichier $file " . $fr_date_file=datefichier(0,2,$file) . " du dernier $lbl\n"; B<$format_affichage> est un chiffre de 0 a 16 representant les formats d'affichage style: # mardi 09/03/2004 15:09:28 (Defaut) # mardi 03 Mars 2004 (1) # 1040309152528 (7) # HMJU7X (10) # lun 18 Oct (13) # Octobre 2005 (14) # mardi 09/03/04 15:09:28 (17) # etc.... pour 0 la date comme: Mercredi 11/12/2003 18:45 (par defaut) pour 1 la date comme: Mercredi 11 Decembre 2003 (pour la Secla) pour 7 la date comme: 20031211184503 (pour liste et filename) pour 10 la date comme: AMJhms (en caractères acsii triable) pour 13 la date comme: lun 24 Jui etc ... voir les tableaux au dessus expl: si je veux la date du dernier acces en format liste je demanderai: $file = $foo; $opt_date_file = 0; $format_affichage =2; $fr_date_file = datefichier($format_affichage, $opt_date_file, $file); ou $fr_date_file = datefichier(2, 0, "$foo"); # a savoir que $lbl stocke le label d'option "Acces, Creation, Modif" pour savoir la date et l'heure du moment present: $french_date_now = maintenant($format_affichage); =head2 PS: I