![]() |
[Inhalt] -> [Date] |
[info] |
|
Die Perl-Funktion localtime() erwartet als Argument eine Zeitangabe wie sie von time() generiert wird. Im skalaren Kontext gibt sie einen Unix Datums-String zurück:
print scalar(localtime); # ergibt: Sat Oct 6 13:09:52 2001
Im Listenkontext gibt localtime ein Array zurück:
# 0 1 2 3 4 5 6 7 8 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $mon++; # da $mon im Range 0..11 vorlag $year += 1900; # da $year ab 1900 gerechnet wurde
Das Datum kann nun über printf() ausgegeben werden:
printf("%04d-%02d-%02d", $year, $mon, $mday); # ergibt: 2001-10-06
Date::Format bietet zwei Routinen an, die das Datum über einen Format-String ausgeben und damit relativ beliebige Ausgaben ermöglichen:
use Date::Format; my $format = "%D %T"; # Beispiel: %D = MM/TT/JJ und %T = HH:MM:SS print time2str($format, time); # skalar oder my @lt = localtime; print strftime($format, @lt); # array
Optional kann als dritter Parameter eine Zeitzone angegeben werden.
Die Formatierungsparameter sind:
%% PERCENT %a day of the week abbr %A day of the week %b month abbr %B month %c MM/DD/YY HH:MM:SS %C ctime format: Sat Nov 19 21:05:57 1994 %d numeric day of the month, with leading zeros (eg 01..31) %e numeric day of the month, without leading zeros (eg 1..31) %D MM/DD/YY %h month abbr %H hour, 24 hour clock, leading 0's %I hour, 12 hour clock, leading 0's %j day of the year %k hour %l hour, 12 hour clock %m month number, starting with 1 %M minute, leading 0's %n NEWLINE %o ornate day of month -- "1st", "2nd", "25th", etc. %p AM or PM %r time format: 09:05:57 PM %R time format: 21:05 %s seconds since the Epoch, UCT %S seconds, leading 0's %t TAB %T time format: 21:05:57 %U week number, Sunday as first day of week %w day of the week, numerically, Sunday == 0 %W week number, Monday as first day of week %x date format: 11/19/94 %X time format: 21:05:57 %y year (2 digits) %Y year (4 digits) %Z timezone in ascii. eg: PST %z timezone in format -/+0000
Um eine nicht-englische Sprache für die Ausgabe einzustellen, muß eine objekt-orientierte Syntax gewählt werden.
use Date::Format;
use Date::Language;
my $lang = Date::Language->new('German');
my $format = "%A, der %d. %B\n"; # ergibt: Samstag, der 6. Oktober
print $lang->time2str($format, time);
Mit Date::Parse können einfachere Datums-Strings gelesen und korrekt interpretiert werden:
use Date::Parse;
$time = str2time("...string...");
my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime("...string...");
$month++;
$year += 1900;
Auch hier gilt für internationale Datums-Strings die Verwendung von Date::Language:
use Date::Parse;
use Date::Language;
my $lang = Date::Language->new('German');
my $date = "1 Okt 2001";
$time = $lang->str2time($date);
my ($ss,$mm,$hh,$day,$month,$year,$zone) = $lang->strptime($date);
$month++;
$year += 1900;
Mit Date::Manip können komplexe Datums-Berechnungen durchgeführt werden. Bitte beachtet Sie, daß Date::Manip sehr langsam ist und nur dann verwendet werden sollte, wenn die Funktionalität der anderen Module (auch Date::Calc) nicht ausreichend ist. Hier folgen nur einige wenige Beispiele für die Verwendung von Date::Manip zum Parsen und formatierten Ausgeben von Datums-Strings:
use Date::Manip;
Date_Init("Language=German","DateFormat=non-US");
my $time = ParseDate("...string...");
my $format = "%C";
print UnixDate($time, $format);
Die verschiedenen Format-Optionen sind:
Year
%y year - 00 to 99
%Y year - 0001 to 9999
%G year - 0001 to 9999 (see below)
%L year - 0001 to 9999 (see below)
Month, Week
%m month of year - 01 to 12
%f month of year - " 1" to "12"
%b,%h month abbreviation - Jan to Dec
%B month name - January to December
%U week of year, Sunday
as first day of week - 01 to 53
%W week of year, Monday
as first day of week - 01 to 53
Day
%j day of the year - 001 to 366
%d day of month - 01 to 31
%e day of month - " 1" to "31"
%v weekday abbreviation - " S"," M"," T"," W","Th"," F",
%a weekday abbreviation - Sun to Sat
%A weekday name - Sunday to Saturday
%w day of week - 1 (Monday) to 7 (Sunday)
%E day of month with suffix - 1st, 2nd, 3rd...
Hour
%H hour - 00 to 23
%k hour - " 0" to "23"
%i hour - " 1" to "12"
%I hour - 01 to 12
%p AM or PM
Minute, Second, Timezone
%M minute - 00 to 59
%S second - 00 to 59
%s seconds from 1/1/1970 GMT - negative if before 1/1/1970
%o seconds from Jan 1, 1970
in the current time zone
%Z timezone - "EDT"
%z timezone as GMT offset - "+0100"
Date, Time
%c %a %b %e %H:%M:%S %Y - Fri Apr 28 17:23:15 1995
%C,%u %a %b %e %H:%M:%S %z %Y - Fri Apr 28 17:25:57 EDT 1995
%g %a, %d %b %Y %H:%M:%S %z - Fri, 28 Apr 1995 17:23:15 EDT
%D,%x %m/%d/%y - 04/28/95
%l date in ls(1) format
%b %e $H:$M - Apr 28 17:23 (if within 6 months)
%b %e %Y - Apr 28 1993 (otherwise)
%r %I:%M:%S %p - 05:39:55 PM
%R %H:%M - 17:40
%T,%X %H:%M:%S - 17:40:58
%V %m%d%H%M%y - 0428174095
%Q %Y%m%d - 19961025
%q %Y%m%d%H%M%S - 19961025174058
%P %Y%m%d%H%M%S - 1996102517:40:58
%F %A, %B %e, %Y - Sunday, January 1, 1996
%J %G-W%W-%w - 1997-W02-2
%K %Y-%j - 1997-045
Other formats
%n insert a newline character
%t insert a tab character
%% insert a `%' character
%+ insert a `+' character
Zusammengestellt von Alex Pleiner
© 2001-2003 zeitform Internet Dienste Bei Problemen wenden Sie sich bitte an den Webmaster
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1.