perl 4.0 patch 14: patch #11, continued
[p5sagit/p5-mst-13.2.git] / lib / ctime.pl
CommitLineData
b1248f16 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
6;# $Id: ctime.pl,v 1.8 91/02/04 18:28:12 hakanson Exp $
b1248f16 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 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 22
23sub ctime {
7e1cf235 24 package ctime;
25
b1248f16 26 local($time) = @_;
27 local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
28
fe14fcc3 29 # Determine what time zone is in effect.
30 # Use GMT if TZ is defined as null, local time if TZ undefined.
31 # There's no portable way to find the system default timezone.
32
33 $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : '';
b1248f16 34 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
35 ($TZ eq 'GMT') ? gmtime($time) : localtime($time);
fe14fcc3 36
b1248f16 37 # Hack to deal with 'PST8PDT' format of TZ
fe14fcc3 38 # Note that this can't deal with all the esoteric forms, but it
39 # does recognize the most common: [:]STDoff[DST[off][,rule]]
40
41 if($TZ=~/^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/){
42 $TZ = $isdst ? $4 : $1;
b1248f16 43 }
fe14fcc3 44 $TZ .= ' ' unless $TZ eq '';
45
b1248f16 46 $year += ($year < 70) ? 2000 : 1900;
47 sprintf("%s %s %2d %2d:%02d:%02d %s%4d\n",
48 $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
49}
501;