This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make timelocal work better when time is close to the epoch
[perl5.git] / lib / Time / gmtime.pm
1 package Time::gmtime;
2 use strict;
3 use Time::tm;
4
5 BEGIN { 
6     use Exporter   ();
7     use vars       qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
8     @ISA         = qw(Exporter Time::tm);
9     @EXPORT      = qw(gmtime gmctime);
10     @EXPORT_OK   = qw(  
11                         $tm_sec $tm_min $tm_hour $tm_mday 
12                         $tm_mon $tm_year $tm_wday $tm_yday 
13                         $tm_isdst
14                     );
15     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
16     $VERSION     = 1.01;
17 }
18 use vars      @EXPORT_OK;
19
20 sub populate (@) {
21     return unless @_;
22     my $tmob = Time::tm->new();
23     @$tmob = (
24                 $tm_sec, $tm_min, $tm_hour, $tm_mday, 
25                 $tm_mon, $tm_year, $tm_wday, $tm_yday, 
26                 $tm_isdst )
27             = @_;
28     return $tmob;
29
30
31 sub gmtime (;$)    { populate CORE::gmtime(@_ ? shift : time)}
32 sub gmctime (;$)   { scalar   CORE::gmtime(@_ ? shift : time)} 
33
34 1;
35 __END__
36
37 =head1 NAME
38
39 Time::gmtime - by-name interface to Perl's built-in gmtime() function
40
41 =head1 SYNOPSIS
42
43  use Time::gmtime;
44  $gm = gmtime();
45  printf "The day in Greenwich is %s\n", 
46     (qw(Sun Mon Tue Wed Thu Fri Sat Sun))[ gm->wday() ];
47
48  use Time::gmtime w(:FIELDS;
49  printf "The day in Greenwich is %s\n", 
50     (qw(Sun Mon Tue Wed Thu Fri Sat Sun))[ gm_wday() ];
51
52  $now = gmctime();
53
54  use Time::gmtime;
55  use File::stat;
56  $date_string = gmctime(stat($file)->mtime);
57
58 =head1 DESCRIPTION
59
60 This module's default exports override the core gmtime() function,
61 replacing it with a version that returns "Time::tm" objects.
62 This object has methods that return the similarly named structure field
63 name from the C's tm structure from F<time.h>; namely sec, min, hour,
64 mday, mon, year, wday, yday, and isdst.
65
66 You may also import all the structure fields directly into your namespace
67 as regular variables using the :FIELDS import tag.  (Note that this
68 still overrides your core functions.)  Access these fields as variables
69 named with a preceding C<tm_> in front their method names.  Thus,
70 C<$tm_obj-E<gt>mday()> corresponds to $tm_mday if you import the fields.
71
72 The gmctime() function provides a way of getting at the 
73 scalar sense of the original CORE::gmtime() function.
74
75 To access this functionality without the core overrides,
76 pass the C<use> an empty import list, and then access
77 function functions with their full qualified names.
78 On the other hand, the built-ins are still available
79 via the C<CORE::> pseudo-package.
80
81 =head1 NOTE
82
83 While this class is currently implemented using the Class::Struct
84 module to build a struct-like class, you shouldn't rely upon this.
85
86 =head1 AUTHOR
87
88 Tom Christiansen