Skip to content

Commit aec2cdc

Browse files
committed
perlfunc/localtime: field ranges, use of strftime()
- move description of default argument to the top of the section - be more explicit about the inconsistent ranges used by $mday (1-based) and $mon (0-based) - give strftime examples first (for getting a full timestamp or just the year), as opposed to manual arithmetic and sprintf - consistently indent all code blocks by 4 spaces
1 parent 7537474 commit aec2cdc

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

pod/perlfunc.pod

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4232,33 +4232,30 @@ X<localtime> X<ctime>
42324232
=for Pod::Functions convert UNIX time into record or string using local time
42334233

42344234
Converts a time as returned by the time function to a 9-element list
4235-
with the time analyzed for the local time zone. Typically used as
4236-
follows:
4235+
with the time analyzed for the local time zone. If EXPR is omitted,
4236+
L<C<localtime>|/localtime EXPR> uses the current time (as returned by
4237+
L<C<time>|/time>).
4238+
4239+
Typically used as follows:
42374240

42384241
# 0 1 2 3 4 5 6 7 8
42394242
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
42404243
localtime(time);
42414244

4242-
All list elements are numeric and come straight out of the C 'struct
4243-
tm'. C<$sec>, C<$min>, and C<$hour> are the seconds, minutes, and hours
4245+
All list elements are numeric and come straight out of the C C<struct
4246+
tm>. C<$sec>, C<$min>, and C<$hour> are the seconds, minutes, and hours
42444247
of the specified time.
42454248

4246-
C<$mday> is the day of the month and C<$mon> the month in
4247-
the range C<0..11>, with 0 indicating January and 11 indicating December.
4248-
This makes it easy to get a month name from a list:
4249+
C<$mday> is the day of the month in the range C<1..31> (i.e. 1-based). C<$mon>
4250+
is the month in the range C<0..11> (i.e. 0-based), with 0 indicating January
4251+
and 11 indicating December. This makes it easy to get a month name from a
4252+
list:
42494253

42504254
my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
42514255
print "$abbr[$mon] $mday";
42524256
# $mon=9, $mday=18 gives "Oct 18"
42534257

4254-
C<$year> contains the number of years since 1900. To get the full
4255-
year write:
4256-
4257-
$year += 1900;
4258-
4259-
To get the last two digits of the year (e.g., "01" in 2001) do:
4260-
4261-
$year = sprintf("%02d", $year % 100);
4258+
C<$year> contains the number of years since 1900 (e.g. C<129> for 2029).
42624259

42634260
C<$wday> is the day of the week, with 0 indicating Sunday and 3 indicating
42644261
Wednesday. C<$yday> is the day of the year, in the range C<0..364>
@@ -4267,21 +4264,42 @@ Wednesday. C<$yday> is the day of the year, in the range C<0..364>
42674264
C<$isdst> is true if the specified time occurs when Daylight Saving
42684265
Time is in effect, false otherwise.
42694266

4270-
If EXPR is omitted, L<C<localtime>|/localtime EXPR> uses the current
4271-
time (as returned by L<C<time>|/time>).
4267+
To get a human-readable date/time string, use L<POSIX/C<strftime>>:
4268+
4269+
use POSIX qw(strftime);
4270+
my @now = localtime;
4271+
my $now_string = strftime "%Y-%m-%d %H:%M:%S", @now;
4272+
# e.g. "2025-11-29 15:19:02"
4273+
4274+
To get just the year, you can use either L<POSIX/C<strftime>>:
4275+
4276+
use POSIX qw(strftime);
4277+
# full year:
4278+
my $year = strftime "%Y", localtime;
4279+
# just the last two digits of the year:
4280+
my $ar = strftime "%y", localtime;
4281+
4282+
... or manual arithmetic:
4283+
4284+
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
4285+
localtime;
4286+
# full year:
4287+
$year += 1900;
4288+
# just the last two digits of the year:
4289+
my $ar = sprintf("%02d", $year % 100);
42724290

42734291
In scalar context, L<C<localtime>|/localtime EXPR> returns the
42744292
L<ctime(3)> value:
42754293

4276-
my $now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"
4294+
my $now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"
42774295

42784296
This scalar value is always in English, and is B<not> locale-dependent.
42794297
To get similar but locale-dependent date strings, try for example:
42804298

4281-
use POSIX qw(strftime);
4282-
my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
4283-
# or for GMT formatted appropriately for your locale:
4284-
my $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
4299+
use POSIX qw(strftime);
4300+
my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
4301+
# or for GMT formatted appropriately for your locale:
4302+
my $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
42854303

42864304
C<$now_string> will be formatted according to the current LC_TIME locale
42874305
the program or thread is running in. See L<perllocale> for how to set

0 commit comments

Comments
 (0)