Algorithm-ToNumberMunger

 view release on metacpan or  search on metacpan

t/mungers-datetime-strptime.t  view on Meta::CPAN

#!perl
# The datetime munger's strptime path, exercised across the common real-world
# stamp formats. Every format here is deliberately NOT fast-eligible (it uses a
# code outside %Y %m %d %H %M %S, or omits one of the six), so parsing MUST go
# through Time::Piece->strptime -- and we prove that two ways: white-box, by
# asserting _compile_fast_format rejects the format, and black-box, by counting
# actual Time::Piece::strptime calls through a wrapper.
use 5.006;
use strict;
use warnings;
use Test::More;

BEGIN {
	eval { require Time::Piece; 1 }
		or plan skip_all => 'Time::Piece not available';
}

use Algorithm::ToNumberMunger;
my $M = 'Algorithm::ToNumberMunger';

# Count real strptime calls. The munger resolves Time::Piece->strptime at call
# time, so wrapping the glob here is seen by code inside Mungers.pm too.
my $STRPTIME_CALLS = 0;
{
	no warnings 'redefine';
	my $orig = \&Time::Piece::strptime;
	*Time::Piece::strptime = sub { $STRPTIME_CALLS++; goto &$orig };
}

# One reference instant expressed in many notations: Sunday 2026-07-05
# 13:37:42 UTC. The expected numbers are literals (not re-derived via
# Time::Piece) so the module is never its own oracle.
my $EPOCH = 1783258662;

# Each row: [ description, format, stamp, { part => expected } ].
my @FORMATS = (
	[
		'Apache CLF access log (%b month name)',
		'%d/%b/%Y:%H:%M:%S',
		'05/Jul/2026:13:37:42',
		{
			epoch => $EPOCH,
			year  => 2026,
			mon   => 7,
			mday  => 5,
			hour  => 13,
			min   => 37,
			sec   => 42,
			wday  => 0
		},
	],
	[
		'RFC 2822 email Date header (%a weekday, %b month)',
		'%a, %d %b %Y %H:%M:%S',
		'Sun, 05 Jul 2026 13:37:42',
		{ epoch => $EPOCH, hour => 13, wday => 0 },
	],
	[
		'RFC 7231 HTTP-date (fixed GMT literal)',
		'%a, %d %b %Y %H:%M:%S GMT',
		'Sun, 05 Jul 2026 13:37:42 GMT',
		{ epoch => $EPOCH, hour => 13 },
	],
	[
		'RFC 850 cookie date (%A full weekday, %y two-digit year)',
		'%A, %d-%b-%y %H:%M:%S', 'Sunday, 05-Jul-26 13:37:42',
		{ epoch => $EPOCH, year => 2026 },    # POSIX rule: 26 => 2026
	],
	[
		'asctime/ctime (%e space-padded day)',
		'%a %b %e %H:%M:%S %Y',
		'Sun Jul  5 13:37:42 2026',
		{ epoch => $EPOCH, mday => 5 },
	],
	[
		'US 12-hour clock (%I with %p AM/PM)',
		'%m/%d/%Y %I:%M:%S %p',
		'07/05/2026 01:37:42 PM',
		{ epoch => $EPOCH, hour => 13 },
	],
	[
		'ISO 8601 with numeric zone (%z, offset applied)', '%Y-%m-%dT%H:%M:%S%z',



( run in 0.690 second using v1.01-cache-2.11-cpan-0fb53d1c279 )