@@ -4232,33 +4232,30 @@ X<localtime> X<ctime>
4232
4232
=for Pod::Functions convert UNIX time into record or string using local time
4233
4233
4234
4234
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:
4237
4240
4238
4241
# 0 1 2 3 4 5 6 7 8
4239
4242
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
4240
4243
localtime(time);
4241
4244
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
4244
4247
of the specified time.
4245
4248
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:
4249
4253
4250
4254
my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
4251
4255
print "$abbr[$mon] $mday";
4252
4256
# $mon=9, $mday=18 gives "Oct 18"
4253
4257
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).
4262
4259
4263
4260
C<$wday> is the day of the week, with 0 indicating Sunday and 3 indicating
4264
4261
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>
4267
4264
C<$isdst> is true if the specified time occurs when Daylight Saving
4268
4265
Time is in effect, false otherwise.
4269
4266
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);
4272
4290
4273
4291
In scalar context, L<C<localtime>|/localtime EXPR> returns the
4274
4292
L<ctime(3)> value:
4275
4293
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"
4277
4295
4278
4296
This scalar value is always in English, and is B<not> locale-dependent.
4279
4297
To get similar but locale-dependent date strings, try for example:
4280
4298
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;
4285
4303
4286
4304
C<$now_string> will be formatted according to the current LC_TIME locale
4287
4305
the program or thread is running in. See L<perllocale> for how to set
0 commit comments