d3b03545ca4e7ef93cb7889973794d401e99637e
[p5sagit/p5-mst-13.2.git] / lib / ctime.pl
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
5 ;# Modified March 1990 to better handle timezones
6 ;#  $Id: ctime.pl,v 1.3 90/03/22 10:49:10 hakanson Exp $
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
13 ;#     $Date = do ctime(time);
14
15 @DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
16 @MoY = ('Jan','Feb','Mar','Apr','May','Jun',
17         'Jul','Aug','Sep','Oct','Nov','Dec');
18
19 sub ctime {
20     local($time) = @_;
21     local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
22
23     # Use GMT if can't find local TZ
24     $TZ = defined($ENV{'TZ'}) ? $ENV{'TZ'} : 'GMT';
25     ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
26         ($TZ eq 'GMT') ? gmtime($time) : localtime($time);
27     # Hack to deal with 'PST8PDT' format of TZ
28     if ( $TZ =~ /-?\d+/ ) {
29         $TZ = $isdst ? $' : $`;
30     }
31     $TZ .= " " unless $TZ eq "";
32     $year += ($year < 70) ? 2000 : 1900;
33     sprintf("%s %s %2d %2d:%02d:%02d %s%4d\n",
34       $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
35 }
36 1;