f56bff93eba9549546dcf20247ce9a74a4bb458b
# Date::Format $Id: //depot/TimeDate/lib/Date/Format.pm#8 $
# Copyright (c) 1995-1999 Graham Barr. All rights reserved. This program is free
# software; you can redistribute it and/or modify it under the same terms
use vars
qw(@EXPORT @ISA $VERSION);
@EXPORT = qw(time2str strftime ctime asctime);
Date
::Format
::Generic
->time2str(@_);
Date
::Format
::Generic
->strftime(@_);
Date
::Format
::Generic
->time2str("%a %b %e %T %Y\n", $t, $tz);
Date
::Format
::Generic
->strftime("%a %b %e %T %Y\n", $t, $tz);
package Date
::Format
::Generic
;
use vars
qw($epoch $tzname);
$me->time2str("%a %b %e %T %Y\n", $t, $tz);
$me->strftime("%a %b %e %T %Y\n", $t, $tz);
($_[0]->can("format_$1") || sub { $1 })->($_[0]);
($pkg,$fmt,$time,$tzname) = @_;
my $me = ref($pkg) ? $pkg : bless [];
$tzname = sprintf("%+05d",$tzname)
$epoch = timegm(@{$time}[0..5]);
@$me = gmtime($epoch + tz_offset($tzname) - tz_offset());
($pkg,$fmt,$time,$tzname) = @_;
my $me = ref($pkg) ? $pkg : bless [], $pkg;
$tzname = sprintf("%+05d",$tzname)
$time += tz_offset($tzname);
my(@DoW,@MoY,@DoWs,@MoYs,@AMPM,%format,@Dsuf);
@DoW = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
@MoY = qw(January February March April May June
July August September October November December);
@DoWs = map { substr($_,0,3) } @DoW;
@MoYs = map { substr($_,0,3) } @MoY;
@Dsuf = (qw(th st nd rd th th th th th th)) x
3;
@Dsuf[11,12,13] = qw(th th th);
@Dsuf[30,31] = qw(th st);
%format = ('x' => "%m/%d/%y",
'C' => "%a %b %e %T %Z %Y",
my $locale = "/usr/share/lib/locale/LC_TIME/default";
if(open(LOCALE
,"$locale"))
chop(@locale = <LOCALE
>);
@MoYs = @locale[0 .. 11];
@MoY = @locale[12 .. 23];
@DoWs = @locale[24 .. 30];
@DoW = @locale[31 .. 37];
@format{"X","x","C"} = @locale[38 .. 40];
@AMPM = @locale[41 .. 42];
my($wstart, $wday, $yday) = @_;
$wday = ($wday + 7 - $wstart) % 7;
return int(($yday - $wday + 13) / 7 - 1);
## these 6 formatting routins need to be *copied* into the language
my @roman = ('',qw(I II III IV V VI VII VIII IX));
(my $t = $roman[$1]) =~ tr/IVX/XLC/;
(my $t = $roman[$1]) =~ tr/IVX/CDM/;
(my $t = $roman[$1]) =~ tr/IVX/M../;
sub format_a
{ $DoWs[$_[0]->[6]] }
sub format_A
{ $DoW[$_[0]->[6]] }
sub format_b
{ $MoYs[$_[0]->[4]] }
sub format_B
{ $MoY[$_[0]->[4]] }
sub format_h
{ $MoYs[$_[0]->[4]] }
sub format_p
{ $_[0]->[2] >= 12 ?
$AMPM[1] : $AMPM[0] }
sub format_P
{ lc($_[0]->[2] >= 12 ?
$AMPM[1] : $AMPM[0]) }
sub format_d
{ sprintf("%02d",$_[0]->[3]) }
sub format_e
{ sprintf("%2d",$_[0]->[3]) }
sub format_H
{ sprintf("%02d",$_[0]->[2]) }
sub format_I
{ sprintf("%02d",$_[0]->[2] % 12 || 12)}
sub format_j
{ sprintf("%03d",$_[0]->[7] + 1) }
sub format_k
{ sprintf("%2d",$_[0]->[2]) }
sub format_l
{ sprintf("%2d",$_[0]->[2] % 12 || 12)}
sub format_L
{ $_[0]->[4] + 1 }
sub format_m
{ sprintf("%02d",$_[0]->[4] + 1) }
sub format_M
{ sprintf("%02d",$_[0]->[1]) }
sub format_q
{ sprintf("%01d",int($_[0]->[4] / 3) + 1) }
$epoch = timegm
(@
{$_[0]}[0..5])
sub format_S
{ sprintf("%02d",$_[0]->[0]) }
sub format_U
{ wkyr
(0, $_[0]->[6], $_[0]->[7]) }
sub format_w
{ $_[0]->[6] }
sub format_W
{ wkyr
(1, $_[0]->[6], $_[0]->[7]) }
sub format_y
{ sprintf("%02d",$_[0]->[5] % 100) }
sub format_Y
{ sprintf("%04d",$_[0]->[5] + 1900) }
my $o = tz_local_offset
(timelocal
(@
{$_[0]}[0..5]));
defined $tzname ?
$tzname : uc tz_name
($o, $_[0]->[8]);
my $t = timelocal
(@
{$_[0]}[0..5]);
my $o = defined $tzname ? tz_offset
($tzname, $t) : tz_offset
(undef,$t);
sprintf("%+03d%02d", int($o / 3600), abs(int($o % 3600)));
sub format_c
{ &format_x
. " " . &format_X
}
sub format_D
{ &format_m
. "/" . &format_d
. "/" . &format_y
}
sub format_r
{ &format_I
. ":" . &format_M
. ":" . &format_S
. " " . &format_p
}
sub format_R
{ &format_H
. ":" . &format_M
}
sub format_T
{ &format_H
. ":" . &format_M
. ":" . &format_S
}
sub format_o
{ sprintf("%2d%s",$_[0]->[3],$Dsuf[$_[0]->[3]]) }
sub format_x
{ my $f = $format{'x'}; _subs
($_[0],$f); }
sub format_X
{ my $f = $format{'X'}; _subs
($_[0],$f); }
sub format_C
{ my $f = $format{'C'}; _subs
($_[0],$f); }
sub format_Od
{ roman
(format_d
(@_)) }
sub format_Oe
{ roman
(format_e
(@_)) }
sub format_OH
{ roman
(format_H
(@_)) }
sub format_OI
{ roman
(format_I
(@_)) }
sub format_Oj
{ roman
(format_j
(@_)) }
sub format_Ok
{ roman
(format_k
(@_)) }
sub format_Ol
{ roman
(format_l
(@_)) }
sub format_Om
{ roman
(format_m
(@_)) }
sub format_OM
{ roman
(format_M
(@_)) }
sub format_Oq
{ roman
(format_q
(@_)) }
sub format_Oy
{ roman
(format_y
(@_)) }
sub format_OY
{ roman
(format_Y
(@_)) }
Date::Format - Date formating subroutines
print time2str($template, time);
print strftime($template, @lt);
print time2str($template, time, $zone);
print strftime($template, @lt, $zone);
print ctime(time, $zone);
print asctime(@lt, $zone);
This module provides routines to format dates into ASCII strings. They
correspond to the C library routines C<strftime> and C<ctime>.
=item time2str(TEMPLATE, TIME [, ZONE])
C<time2str> converts C<TIME> into an ASCII string using the conversion
specification given in C<TEMPLATE>. C<ZONE> if given specifies the zone
which the output is required to be in, C<ZONE> defaults to your current zone.
=item strftime(TEMPLATE, TIME [, ZONE])
C<strftime> is similar to C<time2str> with the exception that the time is
passed as an array, such as the array returned by C<localtime>.
=item ctime(TIME [, ZONE])
C<ctime> calls C<time2str> with the given arguments using the
conversion specification C<"%a %b %e %T %Y\n">
=item asctime(TIME [, ZONE])
C<asctime> calls C<time2str> with the given arguments using the
conversion specification C<"%a %b %e %T %Y\n">
=head1 MULTI-LANGUAGE SUPPORT
Date::Format is capable of formating into several languages, these are
English, French, German and Italian. Changing the language is done via
a static method call, for example
Date::Format->language('German');
will change the language in which all subsequent dates are formatted.
This is only a first pass, I am considering changing this to be
$lang = Date::Language->new('German');
$lang->time2str("%a %b %e %T %Y\n", time);
I am open to suggestions on this.
=head1 CONVERSION SPECIFICATION
Each conversion specification is replaced by appropriate
characters as described in the following list. The
appropriate characters are determined by the LC_TIME
category of the program's locale.
%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)
%H hour, 24 hour clock, leading 0's)
%I hour, 12 hour clock, leading 0's)
%L month number, starting with 1
%m month number, starting with 01
%o ornate day of month -- "1st", "2nd", "25th", etc.
%P am or pm (Yes %p and %P are backwards :)
%q Quarter number, starting with 1
%r time format: 09:05:57 PM
%s seconds since the Epoch, UCT
%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
%Z timezone in ascii. eg: PST
%z timezone in format -/+0000
C<%d>, C<%e>, C<%H>, C<%I>, C<%j>, C<%k>, C<%l>, C<%m>, C<%M>, C<%q>,
C<%y> and C<%Y> can be output in Roman numerals by prefixing the letter
with C<O>, e.g. C<%OY> will output the year as roman numerals.
Graham Barr <gbarr@pobox.com>
Copyright (c) 1995-1999 Graham Barr. All rights reserved. This program is free
software; you can redistribute it and/or modify it under the same terms