Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions ext/POSIX/POSIX.xs
Original file line number Diff line number Diff line change
Expand Up @@ -3589,7 +3589,7 @@ difftime(time1, time2)
# sv_setpv(TARG, ...) could be used rather than
# ST(0) = sv_2mortal(newSVpv(...))
void
strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = 0)
strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
SV * fmt
int sec
int min
Expand All @@ -3605,10 +3605,8 @@ strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = 0)
PERL_UNUSED_ARG(wday);
PERL_UNUSED_ARG(yday);

/* -isdst triggers backwards compatibility mode for non-zero
* 'isdst' */
SV *sv = sv_strftime_ints(fmt, sec, min, hour, mday, mon, year,
-abs(isdst));
isdst);
if (sv) {
sv = sv_2mortal(sv);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/POSIX/lib/POSIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use warnings;

our ($AUTOLOAD, %SIGRT);

our $VERSION = '2.24';
our $VERSION = '2.25';

require XSLoader;

Expand Down
44 changes: 6 additions & 38 deletions ext/POSIX/lib/POSIX.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1866,49 +1866,17 @@ Identical to the string form of C<$!>, see L<perlvar/$ERRNO>.
=item C<strftime>

Convert date and time information to string based on the current
underlying locale of the program (except for any daylight savings time).
Returns the string.
underlying locale of the program.
Returns the string in a mortalized SV; set to an empty string on error.

Synopsis:

strftime(fmt, sec, min, hour, mday, mon, year,
wday = -1, yday = -1, isdst = 0)

The month (C<mon>) begins at zero,
I<e.g.>, January is 0, not 1. The
year (C<year>) is given in years since 1900, I<e.g.>, the year 1995 is 95; the
year 2001 is 101. Consult your system's C<strftime()> manpage for details
about these and the other arguments.
my $sv = strftime(fmt, sec, min, hour, mday, mon, year,
wday = -1, yday = -1, isdst = -1)

The C<wday> and C<yday> parameters are both ignored. Their values are
always determinable from the other parameters.

C<isdst> should be C<1> or C<0>, depending on whether or not daylight
savings time is in effect for the given time or not.

If you want your code to be portable, your format (C<fmt>) argument
should use only the conversion specifiers defined by the ANSI C
standard (C99, to play safe). These are C<aAbBcdHIjmMpSUwWxXyYZ%>.
But even then, the B<results> of some of the conversion specifiers are
non-portable. For example, the specifiers C<aAbBcpZ> change according
to the locale settings of the user, and both how to set locales (the
locale names) and what output to expect are non-standard.
The specifier C<c> changes according to the timezone settings of the
user and the timezone computation rules of the operating system.
The C<Z> specifier is notoriously unportable since the names of
timezones are non-standard. Sticking to the numeric specifiers is the
safest route.

The arguments, except for C<isdst>, are made consistent as though by
calling C<mktime()> before calling your system's C<strftime()> function.
To get correct results, you must set C<isdst> to be the proper value.
When omitted, the function assumes daylight savings is not in effect.

The string for Tuesday, December 12, 1995 in the C<C> locale.

$str = POSIX::strftime( "%A, %B %d, %Y",
0, 0, 0, 12, 11, 95, 2 );
print "$str\n";
More details on the behavior and the specification of the other
parameters are described in L<perlapi/sv_strftime_ints>.

=item C<strlen>

Expand Down
7 changes: 6 additions & 1 deletion ext/POSIX/t/posix.t
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ print POSIX::strftime("ok $test # %H:%M, on %m/%d/%y\n", localtime());
# input fields to strftime().
sub try_strftime {
my $expect = shift;
my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
my @input = @_;

# Add zeros to missing parameters. The final 0 is for isdst, and the zero
# forces use of mini_mktime (unless the code changes).
push @input, 0 while @input < 9;
my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @input);
is($got, $expect, "validating mini_mktime() and strftime(): $expect");
}

Expand Down
86 changes: 75 additions & 11 deletions ext/POSIX/t/time.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ use strict;

use Config;
use POSIX;
use Test::More tests => 31;
use Test::More tests => 49;

# For the first go to UTC to avoid DST issues around the world when testing. SUS3 says that
# null should get you UTC, but some environments want the explicit names.
# Those with a working tzset() should be able to use the TZ below.
$ENV{TZ} = "EST5EDT";

# It looks like POSIX.xs claims that only VMS and Mac OS traditional
# don't have tzset(). Win32 works to call the function, but it doesn't
# actually do anything. Cygwin works in some places, but not others. The
# other Win32's below are guesses.
my $has_tzset = $^O ne "VMS" && $^O ne "cygwin"
&& $^O ne "MSWin32" && $^O ne "interix";

SKIP: {
# It looks like POSIX.xs claims that only VMS and Mac OS traditional
# don't have tzset(). Win32 works to call the function, but it doesn't
# actually do anything. Cygwin works in some places, but not others. The
# other Win32's below are guesses.
skip "No tzset()", 1
if $^O eq "VMS" || $^O eq "cygwin" ||
$^O eq "MSWin32" || $^O eq "interix";
skip "No tzset()", 1 unless $has_tzset;
tzset();
SKIP: {
my @tzname = tzname();
Expand All @@ -42,9 +43,7 @@ SKIP: {
$ENV{TZ} = "UTC0UTC";

SKIP: {
skip "No tzset()", 2
if $^O eq "VMS" || $^O eq "cygwin" ||
$^O eq "MSWin32" || $^O eq "interix";
skip "No tzset()", 2 unless $has_tzset;
tzset();
my @tzname = tzname();
like($tzname[0], qr/(GMT|UTC)/i, "tzset() to GMT/UTC");
Expand Down Expand Up @@ -226,3 +225,68 @@ SKIP: {
is(strftime(undef, CORE::localtime), '', "strftime() works if format is undef");
like($warnings, qr/^Use of uninitialized value in subroutine entry /, "strftime(undef, ...) produces expected warning");
}

# Tue 01 Jul 2025 05:01:01 PM GMT. This is chosen as being in the middle of
# Daylight Savings Time. Below, we set locales, and verify that these
# actually worked by checking that the hour returned is the expected value.
my $reference_time = 1751389261;

SKIP: { # GH #23878; test that dst fall back works properly
my $skip_count = 9;
skip "No mktime()", $skip_count if $Config{d_mktime} ne 'define';
my $locale = "Europe/Paris";
$ENV{TZ} = $locale;
tzset() if $has_tzset;

skip "'$locale' not understood", $skip_count
if POSIX::strftime("%H", localtime($reference_time)) != 19;

my $t = 1761436800; # an hour before time should have changed
my @fall = (
[ -1, "2025-10-26 01:59:59+0200", "Chg -1 hr, 1 sec" ],
[ 0, "2025-10-26 02:00:00+0200", "Chg -1 hr, 0 sec" ],
[ 1, "2025-10-26 02:00:01+0200", "Chg -59 min, 59 sec" ],
[ 3599, "2025-10-26 02:59:59+0200", "Chg -1 sec" ],
[ 3600, "2025-10-26 02:00:00+0100", "At Paris DST fallback" ],
[ 3601, "2025-10-26 02:00:01+0100", "Chg +1 sec" ],
[ 7199, "2025-10-26 02:59:59+0100", "Chg +1 hr, 59m, 59s" ],
[ 7200, "2025-10-26 03:00:00+0100", "Chg +1 hr" ],
[ 7201, "2025-10-26 03:00:01+0100", "Chg +1 hr, 1 sec" ],
);
for (my $i = 0; $i < @fall; $i++) {
is(POSIX::strftime("%F %T%z", localtime $t + $fall[$i][0]),
$fall[$i][1], $fall[$i][2]);
}
}

SKIP: { # GH #23878: test that dst spring forward works properly; use a
# locale that MS narcissism should be able to handle
my $skip_count = 9;
skip "No mktime()", $skip_count if $Config{d_mktime} ne 'define';

my $locale = "PST8PDT";
$ENV{TZ} = $locale;
tzset() if $has_tzset;

skip "'$locale' not understood", $skip_count if
POSIX::strftime("%H", localtime($reference_time)) != 10;

my $t = 1741510800; # an hour before time should have changed

my @spring = (
[ -1, "2025-03-09 00:59:59-0800", "Chg -1 hr,-1 sec" ],
[ 0, "2025-03-09 01:00:00-0800", "Chg -1 hr, 0 sec" ],
[ 1, "2025-03-09 01:00:01-0800", "Chg -59 min,-59 sec" ],
[ 3599, "2025-03-09 01:59:59-0800", "Chg -1 sec" ],
[ 3600, "2025-03-09 03:00:00-0700",
"At Redmond DST spring forward" ],
[ 3601, "2025-03-09 03:00:01-0700", "Chg +1 sec" ],
[ 7199, "2025-03-09 03:59:59-0700", "Chg +1 hr, 59m, 59s" ],
[ 7200, "2025-03-09 04:00:00-0700", "Chg +1 hr" ],
[ 7201, "2025-03-09 04:00:01-0700", "Chg +1 hr, 1 sec" ],
);
for (my $i = 0; $i < @spring; $i++) {
is(POSIX::strftime("%F %T%z", localtime $t + $spring[$i][0]),
$spring[$i][1], $spring[$i][2]);
}
}
Loading
Loading