This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.0 alpha 2
[perl5.git] / lib / ctime.pl
CommitLineData
b1248f16
LW
1;# ctime.pl is a simple Perl emulation for the well known ctime(3C) function.
2;#
3;# Waldemar Kebsch, Federal Republic of Germany, November 1988
4;# kebsch.pad@nixpbe.UUCP
fe14fcc3 5;# Modified March 1990, Feb 1991 to properly handle timezones
79072805 6;# $RCSfile: ctime.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:23:47 $
b1248f16
LW
7;# Marion Hakanson (hakanson@cse.ogi.edu)
8;# Oregon Graduate Institute of Science and Technology
9;#
10;# usage:
11;#
12;# #include <ctime.pl> # see the -P and -I option in perl.man
ff8e2863 13;# $Date = &ctime(time);
b1248f16 14
7e1cf235
LW
15CONFIG: {
16 package ctime;
17
18 @DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
19 @MoY = ('Jan','Feb','Mar','Apr','May','Jun',
20 'Jul','Aug','Sep','Oct','Nov','Dec');
21}
b1248f16
LW
22
23sub ctime {
7e1cf235
LW
24 package ctime;
25
b1248f16 26 local($time) = @_;
68decaef 27 local($[) = 0;
b1248f16
LW
28 local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
29
fe14fcc3
LW
30 # Determine what time zone is in effect.
31 # Use GMT if TZ is defined as null, local time if TZ undefined.
32 # There's no portable way to find the system default timezone.
33
34 $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : '';
b1248f16
LW
35 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
36 ($TZ eq 'GMT') ? gmtime($time) : localtime($time);
fe14fcc3 37
b1248f16 38 # Hack to deal with 'PST8PDT' format of TZ
fe14fcc3
LW
39 # Note that this can't deal with all the esoteric forms, but it
40 # does recognize the most common: [:]STDoff[DST[off][,rule]]
41
42 if($TZ=~/^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/){
43 $TZ = $isdst ? $4 : $1;
b1248f16 44 }
fe14fcc3
LW
45 $TZ .= ' ' unless $TZ eq '';
46
b1248f16
LW
47 $year += ($year < 70) ? 2000 : 1900;
48 sprintf("%s %s %2d %2d:%02d:%02d %s%4d\n",
49 $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
50}
511;