This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
untodo the no-longer-failing todo test for rgs' patch
[perl5.git] / lib / ctime.pl
1 ;# ctime.pl is a simple Perl emulation for the well known ctime(3C) function.
2 #
3 # This library is no longer being maintained, and is included for backward
4 # compatibility with Perl 4 programs which may require it.
5 # This legacy library is deprecated and will be removed in a future
6 # release of perl.
7 #
8 # In particular, this should not be used as an example of modern Perl
9 # programming techniques.
10 #
11 # Suggested alternative: the POSIX ctime function
12
13 warn( "The 'ctime.pl' legacy library is deprecated and will be"
14       . " removed in the next major release of perl. Please use the"
15       . " POSIX module (ctime function) instead." );
16
17 ;#
18 ;# Waldemar Kebsch, Federal Republic of Germany, November 1988
19 ;# kebsch.pad@nixpbe.UUCP
20 ;# Modified March 1990, Feb 1991 to properly handle timezones
21 ;#  $RCSfile: ctime.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:23:47 $
22 ;#   Marion Hakanson (hakanson@cse.ogi.edu)
23 ;#   Oregon Graduate Institute of Science and Technology
24 ;#
25 ;# usage:
26 ;#
27 ;#     #include <ctime.pl>          # see the -P and -I option in perl.man
28 ;#     $Date = &ctime(time);
29
30 CONFIG: {
31     package ctime;
32
33     @DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
34     @MoY = ('Jan','Feb','Mar','Apr','May','Jun',
35             'Jul','Aug','Sep','Oct','Nov','Dec');
36 }
37
38 sub ctime {
39     package ctime;
40
41     local($time) = @_;
42     local($[) = 0;
43     local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
44
45     # Determine what time zone is in effect.
46     # Use GMT if TZ is defined as null, local time if TZ undefined.
47     # There's no portable way to find the system default timezone.
48
49     $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : '';
50     ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
51         ($TZ eq 'GMT') ? gmtime($time) : localtime($time);
52
53     # Hack to deal with 'PST8PDT' format of TZ
54     # Note that this can't deal with all the esoteric forms, but it
55     # does recognize the most common: [:]STDoff[DST[off][,rule]]
56
57     if($TZ=~/^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/){
58         $TZ = $isdst ? $4 : $1;
59     }
60     $TZ .= ' ' unless $TZ eq '';
61
62     $year += 1900;
63     sprintf("%s %s %2d %2d:%02d:%02d %s%4d\n",
64       $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
65 }
66 1;