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