[comp.lang.perl] ctime.pl

jv@mh.nl (Johan Vromans) (03/17/90)

In article <JV.90Mar15140107@squirrel.mh.nl> jv@mh.nl (Johan Vromans) writes:
> 
> Someone posted a 'ctime.pl' some time ago. Has anyone archived it?
> 

I have found it, and take the liberty to include it here:

------ start of ctime.pl -- ascii -- complete ------
;# ctime.pl is a simple Perl emulation for the well known ctime(3C) function.
;#
;# Waldemar Kebsch, Federal Republic of Germany, November 1988
;# kebsch.pad@nixpbe.UUCP
;# My private System: 80286 with Microport System V/AT 2.2
;#
;# usage:
;#
;#     #include <importenv.pl>      # see Perl library. We need the
;#                                  # environment variable TZ.
;#     #include <ctime.pl>          # see the -P and -I option in perl.man
;#     $Date = do ctime(time);

@DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
@MoY = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

sub ctime {
    local($time) = @_;
    local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
    local($date);

    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
							  = localtime($time);
    $year += ($year < 70)? 2000: 1900;
    $date = sprintf("%s %s %2d %2d:%02d:%02d %s %4d\n",
	      $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
    return $date;
}
------ end of ctime.pl -- ascii -- complete ------
--
Johan Vromans				       jv@mh.nl via internet backbones
Multihouse Automatisering bv		       uucp: ..!{uunet,hp4nl}!mh.nl!jv
Doesburgweg 7, 2803 PL Gouda, The Netherlands  phone/fax: +31 1820 62944/62500
------------------------ "Arms are made for hugging" -------------------------

hakanson@ogicse.ogi.edu (Marion Hakanson) (03/20/90)

I tried out the posted ctime.pl, which works OK if you don't care what
the timezone is.  In other words, many hosts are set up to not have
the TZ envariable set by default, and ctime.pl uses $ENV{'TZ'} as the
timezone string in its output.  Note that in many timezones, even if
the variable IS set, it's set to something like "PST8PDT", which is
not the proper thing to print out in the date.  Thus the timezone
string is dependent on both the local timezone and on whether it's
daylight time (or some other special adjustment is in effect).

If one does NOT care about the timezone, then ctime.pl can be modified
to call "gmtime" instead of "localtime", and you can just substitute
a literal "GMT" or "UCT" for the timezone string.  Portable, too.

Modern (POSIX-compliant) Unix systems have a "tzfile" which one could
pick through to get the timezone name, but older systems would require
grunging through kernel structures.  In my opinion, the only portable
solution (if you care about timezones) is to call the ctime(3) library
call.  Perhaps it's time to look into building a foreign-function
interface -- otherwise Perl will just have to link in every possible
library function to satisfy the wants of every programmer.

-- 
Marion Hakanson         Domain: hakanson@cse.ogi.edu
                        UUCP  : {hp-pcd,tektronix}!ogicse!hakanson

tneff@bfmny0.UU.NET (Tom Neff) (03/22/90)

Please stick a closing

	1;

at the end of any such new library files!

hakanson@ogicse.ogi.edu (Marion Hakanson) (03/23/90)

In article <8095@ogicse.ogi.edu> I write:
>. . .
>If one does NOT care about the timezone, then ctime.pl can be modified
>to call "gmtime" instead of "localtime", and you can just substitute
>a literal "GMT" or "UCT" for the timezone string.  Portable, too.
>. . .

Here's the previously-posted ctime.pl, with my modifications to
deal with an undefined TZ envariable.  There's also a change to print
out the proper portion of a "PST8PDT" format TZ, depending on whether
or not "isdst" is set.  At least now it doesn't print out "" for the
timezone, or otherwise mislead you.

Larry, this is probably of general enough use that it should be
included in the Perl distribution library.  Unless, of course,
you plan to add a ctime(3) call to Perl itself (:-).

-- 
Marion Hakanson         Domain: hakanson@cse.ogi.edu
                        UUCP  : {hp-pcd,tektronix}!ogicse!hakanson

==========cut here========
;# ctime.pl is a simple Perl emulation for the well known ctime(3C) function.
;#
;# Waldemar Kebsch, Federal Republic of Germany, November 1988
;# kebsch.pad@nixpbe.UUCP
;# Modified March 1990 to better handle timezones
;#  $Id: ctime.pl,v 1.3 90/03/22 10:49:10 hakanson Exp $
;#   Marion Hakanson (hakanson@cse.ogi.edu)
;#   Oregon Graduate Institute of Science and Technology
;#
;# usage:
;#
;#     #include <ctime.pl>          # see the -P and -I option in perl.man
;#     $Date = do ctime(time);

@DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
@MoY = ('Jan','Feb','Mar','Apr','May','Jun',
        'Jul','Aug','Sep','Oct','Nov','Dec');

sub ctime {
    local($time) = @_;
    local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);

    # Use GMT if can't find local TZ
    $TZ = defined($ENV{'TZ'}) ? $ENV{'TZ'} : 'GMT';
    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
        ($TZ eq 'GMT') ? gmtime($time) : localtime($time);
    # Hack to deal with 'PST8PDT' format of TZ
    if ( $TZ =~ /\d+/ ) {
        $TZ = $isdst ? $' : $`;
    }
    $year += ($year < 70) ? 2000 : 1900;
    sprintf("%s %s %2d %2d:%02d:%02d %s %4d\n",
      $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
}
==========cut here========

jv@mh.nl (Johan Vromans) (03/24/90)

In article <8174@ogicse.ogi.edu> hakanson@ogicse.ogi.edu (Marion Hakanson) writes:

| Here's the previously-posted ctime.pl, with my modifications to
| deal with an undefined TZ envariable.  There's also a change to print
| out the proper portion of a "PST8PDT" format TZ, depending on whether
| or not "isdst" is set.  At least now it doesn't print out "" for the
| timezone, or otherwise mislead you.

Please modify lines

|     # Hack to deal with 'PST8PDT' format of TZ
|     if ( $TZ =~ /\d+/ ) {
|	   $TZ = $isdst ? $' : $`;
|     }
|     $year += ($year < 70) ? 2000 : 1900;
|     sprintf("%s %s %2d %2d:%02d:%02d %s %4d\n",

to

|     # Hack to deal with 'PST8PDT' format of TZ
|     if ( $TZ =~ /-?\d+/ ) {
                   ^^ allow leading minus
|	   $TZ = $isdst ? $' : $`;
|     }
|     $TZ .= " " unless $TZ eq "";
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ suppress if empty
|     $year += ($year < 70)? 2000: 1900;
|     sprintf("%s %s %2d %2d:%02d:%02d %s%4d\n",
                                         ^ no space

to allow European timezones, e.g. MET-1MET, and empty TZ suppression.

	Johan
--
Johan Vromans				       jv@mh.nl via internet backbones
Multihouse Automatisering bv		       uucp: ..!{uunet,hp4nl}!mh.nl!jv
Doesburgweg 7, 2803 PL Gouda, The Netherlands  phone/fax: +31 1820 62944/62500
------------------------ "Arms are made for hugging" -------------------------