Advanced-Config
view release on metacpan or search on metacpan
lib/Advanced/Config/Date.pm view on Meta::CPAN
should be very rare to directly call any methods defined by this module. But
it's perfectly OK to use this module directly if you wish.
It's main job is to handle parsing dates passed in various formats and languages
while returning it in the standardized format of: S<YYYY-MM-DD>. Hiding all the
messy logic of how to interpret any given date string.
=head1 MULTI-LANGUAGE SUPPORT
By default this module only supports parsing B<English> language dates.
But if you have the I<Date::Language> and/or I<Date::Manip> modules installed
you can ask for it to use another language supported by either of these modules
instead.
You have to explicitly allow languages that require the use of I<Wide Chars>.
Otherwise they are not supported.
If a language is defined in both modules, it will merge the data together.
Since both modules sometimes give extra information that can be useful in
parsing a date..
=head1 FOUR-DIGIT VS TWO-DIGIT YEARS IN A DATE
This module will accept both 4-digit and 2-digit years in the dates it parses.
But two-digit years are inherently ambiguous if you aren't given the expected
format up front. So 2-digit years generate more unreliability in the parsing
of any dates by this module.
So when used by the L<Advanced::Config> module, that module gives you the
ability to turn two-digit years on or off. This is done via the B<Get Option>
"B<date_enable_yy>" which defaults to 0, B<not> allowing two-digit years.
To help resolve ambiguity with numeric dates, there is an option "B<date_format>"
that tells the L<Advanced::Config> how to parse these dates. See the order
argument for I<parse_6_digit_date()> and I<parse_8_digit_date()> for how this
is done.
Finally if you use "B<date_dl_conversion>" and module L<Date::Language> is
installed, it will enhance parse_date() with that module's str2time() parser.
So if this option was used, it doesn't make much sense to disable 2-digit years.
Since we can't turn off 2-digit year support for str2time().
See L<Advanced::Config::Options> for more options telling how that module
controls how L<Advanced::Config> uses this module for parsing dates.
Those options have no effect if you are calling these methods directly.
=head1 FUNCTIONS
=over 4
=cut
package Advanced::Config::Date;
use strict;
use warnings;
use File::Spec;
use File::Glob qw (bsd_glob);
use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION );
use Exporter;
use Fred::Fish::DBUG 2.09 qw / on_if_set ADVANCED_CONFIG_FISH /;
$VERSION = "1.14";
@ISA = qw( Exporter );
@EXPORT = qw( get_languages
swap_language
parse_date
adjust_future_cutoff
make_it_a_4_digit_year
parse_8_digit_date
parse_6_digit_date
init_special_date_arrays
_date_language_installed
_date_manip_installed
_validate_date_str
is_leap_year
calc_hundred_year_date
calc_day_of_week
convert_hyd_to_date_str
calc_day_of_year
adjust_date_str
);
@EXPORT_OK = qw( );
my $global_cutoff_date = 30; # Defaults to 30 years in the future ...
# Thesee haahes tell which language modules are available ...
my %date_language_installed_languages;
my %date_manip_installed_languages;
# ========================================================================
# Detects if the optional Date::Language module is available ...
# If it's not installed, you'll be unable to swap languages using it!
BEGIN
{
eval {
local $SIG{__DIE__} = "";
require Date::Language;
# Find out where it's installed
my $loc = $INC{"Date/Language.pm"};
$loc =~ s/[.]pm$//;
my $search = File::Spec->catfile ($loc, "*.pm");
# Get's the list of languages supported.
foreach my $f ( bsd_glob ($search) ) {
my $module = (File::Spec->splitdir( $f ))[-1];
$module =~ s/[.]pm$//;
my %data = ( Language => $module,
Module => "Date::Language::${module}" );
$date_language_installed_languages{lc($module)} = \%data;
}
};
}
# ========================================================================
# Detects if the optional Date::Manip module is available ...
# If it's not installed, you'll be unable to swap languages using it!
BEGIN
{
eval {
local $SIG{__DIE__} = "";
require Date::Manip::Lang::index;
Date::Manip::Lang::index->import ();
foreach my $k ( sort keys %Date::Manip::Lang::index::Lang ) {
my $mod = $Date::Manip::Lang::index::Lang{$k};
my $lang = ( $k eq $mod ) ? ucfirst ($mod) : $mod;
my $module = "Date::Manip::Lang::${mod}";
my %data = ( Language => $lang, # A guess that's wrong sometimes
Module => $module );
$date_manip_installed_languages{lc ($k)} = \%data;
}
};
# -------------------------------------------------------------
# Proves sometimes the module name is different from the
# real language name.
# -------------------------------------------------------------
# foreach my $k ( sort keys %date_manip_installed_languages ) {
# printf STDERR ("Key (%s) Language (%s)\n", $k, $date_manip_installed_languages{$k}->{Language});
# }
}
# ========================================================================
# Hashes used to help validate/parse dates with ...
# Always keep the keys in lower case.
# Using the values from Date::Language::English for initialization ...
# Hard coded here in case Date::Language wasn't installed ...
# These hashes get rebuilt each time swap_language() is
# successfully called!
# ========================================================================
# Used by parse_date ();
my %last_language_edit_flags;
# Variants for the month names & days of month ...
# We hard code the initialization in case neither
# language module is installed locally.
my %Months;
my %Days;
lib/Advanced/Config/Date.pm view on Meta::CPAN
for the parsing of date strings.
If neither L<Date::Language> and/or L<Date::Manip> are installed, only
I<English> is supported and you'll be unable to swap languages.
Both modules are used since each language module supports a different
set of languages with a lot of overlap between them.
Also L<Date::Manip> supports common aliases for some languages. These
aliases appear in lower case. When these aliases are used by
swap_language, it returns the real underlying language instead of
the alias.
=cut
sub get_languages
{
DBUG_ENTER_FUNC ( @_ );
my %languages;
# For Date::Language ... (straight forward)
foreach my $k1 ( keys %date_language_installed_languages ) {
my $lang = $date_language_installed_languages{$k1}->{Language};
$languages{$lang} = 1;
}
# For Date::Manip ... (a bit messy)
# Messy since we can't verify the language without 1st loading it!
foreach my $k1 ( keys %date_manip_installed_languages ) {
my $lang = $date_manip_installed_languages{$k1}->{Language};
my $k2 = ($k1 eq lc($lang)) ? $lang : $k1;
$languages{$k2} = 1;
}
if ( scalar ( keys %languages ) == 0 ) {
$languages{English} = 1;
}
DBUG_RETURN ( sort keys %languages );
}
# ==============================================================
# Done this way to the warning goes to fish no matter what.
sub _warn_msg
{
DBUG_ENTER_FUNC ( @_ );
my $ok = shift;
my $msg = shift;
if ( $ok ) {
warn "==> ${msg}\n";
}
DBUG_VOID_RETURN ();
}
# ==============================================================
# No POD on purpose ...
# Does some common logic for swap_language() & init_special_date_arrays().
# Requires knowledge of the internals to Date::Language::<language>
# in order to work.
# This method should avoid referencing any global variables!
# Returns: undef or the references to the 5 arrays!
sub _swap_lang_common
{
DBUG_ENTER_FUNC ( @_ );
my $lang_ref = shift;
my $warn_ok = shift;
my $allow_wide = shift || 0;
my $base = "Date::Language";
my $lang = $lang_ref->{Language};
my $module = $lang_ref->{Module};
my %issues;
# Check if the requested language module exists ...
{
local $SIG{__DIE__} = "";
my $sts = eval "require ${module}";
unless ( $sts ) {
_warn_msg ( $warn_ok, "${base} doesn't recognize '${lang}' as valid!" );
return DBUG_RETURN ( undef, undef, undef, undef, undef, \%issues );
}
}
# @Dsuf isn't always available for some modules & buggy for others.
my @lMoY = eval "\@${module}::MoY"; # The fully spelled out Months.
my @lMoYs = eval "\@${module}::MoYs"; # The legal Abbreviations.
my @lDsuf = eval "\@${module}::Dsuf"; # The suffix for the Day of Month.
my @lDoW = eval "\@${module}::DoW"; # The Day of Week.
my @lDoWs = eval "\@${module}::DoWs"; # The Day of Week Abbreviations.
# Detects Windows bug caused by case insensitive OS.
# Where the OS says the file exists, but it doesn't match the package name.
# Ex: Date::Language::Greek vs Date::Language::greek
if ( $#lMoY == -1 && $#lMoYs == -1 && $#lDsuf == -1 && $#lDoW == -1 && $#lDoWs == -1 ) {
_warn_msg ( $warn_ok, "${base} doesn't recognize '${lang}' as valid due to case!" );
return DBUG_RETURN ( undef, undef, undef, undef, undef, \%issues );
}
# Add the missing end of the month for quite a few Dsuf!
# Uses the suffixes from the 20's.
my $num = @lDsuf;
if ( $num > 29 ) {
my $fix = $num % 10;
foreach ( $num..31 ) {
my $idx = $_ - $num + 20 + $fix;
$lDsuf[$_] = $lDsuf[$idx];
DBUG_PRINT ("FIX", "lDsuf[%d] = lDsuf[%d] = %s (%s)",
$_, $idx, $lDsuf[$_], $lang);
}
}
# --------------------------------------------------
# Check if Unicode/Wide Chars were used ...
my $wide_flag = 0;
foreach ( @lMoY, @lMoYs, @lDsuf, @lDoW, @lDoWs ) {
# my $wide = utf8::is_utf8 ($_) || 0;
my $wide = ( $_ =~ m/[^\x00-\xff]/ ) || 0; # m/[^\x00-\x7f]/ doesn't completely work!
if ( $wide ) {
$wide_flag = 1; # Multi-byte chars detected!
} else {
# Fix so uc()/lc() work for languages like German.
utf8::encode ($_);
utf8::decode ($_); # Sets utf8 flag ...
# Are any of these common variants wide chars?
if ( $_ =~ m/[^\x00-\xff]/ ||
uc ($_) =~ m/[^\x00-\xff]/ ||
lc ($_) =~ m/[^\x00-\xff]/ ) {
$wide_flag = -1;
}
}
}
$lang_ref->{Wide} = $wide_flag;
if ( $wide_flag && ! $allow_wide ) {
_warn_msg ( $warn_ok, "'${lang}' uses Wide Chars. It's not currently enabled!" );
return DBUG_RETURN ( undef, undef, undef, undef, undef, \%issues );
}
# Put in the number before the suffix ... (ie: nd => 2nd, rd => 3rd)
# Many langages built this array incorrectly & shorted it.
foreach ( 0..31 ) {
last unless ( defined $lDsuf[$_] );
$lDsuf[$_] = $_ . $lDsuf[$_];
$issues{dsuf_period} = 1 if ($lDsuf[$_] =~ m/[.]/ );
}
# Now check if any RegExp wild cards in the value ...
foreach ( @lMoY, @lMoYs ) {
$issues{month_period} = 1 if ( $_ =~ m/[.]/ );
}
foreach ( @lDoW, @lDoWs ) {
$issues{dow_period} = 1 if ( $_ =~ m/[.]/ );
}
DBUG_RETURN ( \@lMoY, \@lMoYs, \@lDsuf, \@lDoW, \@lDoWs, \%issues );
}
# ==============================================================
# No POD on purpose ...
# Does some common logic for swap_language() & init_special_date_arrays().
# Requires knowledge of the internals to Date::Manip::Lang::<language>
# in order to work.
# This method should avoid referencing any global variables!
# Returns: undef or the references to the 5 arrays!
# I would have broken it up ino multiple functions if not for the wide test!
sub _swap_manip_language_common
{
DBUG_ENTER_FUNC ( @_ );
my $lang_ref = shift;
my $warn_ok = shift;
my $allow_wide = shift || 0;
my $base = "Date::Manip";
my $lang = $lang_ref->{Language};
my $module = $lang_ref->{Module};
# Check if the requested language module exists ...
{
local $SIG{__DIE__} = "";
my $sts = eval "require ${module}";
unless ( $sts ) {
_warn_msg ( $warn_ok, "${base} doesn't recognize '${lang}' as valid!" );
return ( DBUG_RETURN ( undef, undef, undef, undef, undef, undef, undef, undef ) );
}
}
# Get the proper name of this language fom the module.
$lang_ref->{Language} = $lang = eval "\$${module}::LangName";
# Get the language data from the module.
my $langData = eval "\$${module}::Language"; # A hash reference with the data!
# The 3 return values used by swap_language () ...
my (%months, %days, %issues);
# The 5 return values used by init_special_date_arrays()
my ( @MoY, @MoYs, @Dsuf, @DoW, @DoWs);
my $wide = 0;
my $has_period = 0;
foreach my $month_idx (1..12) {
foreach my $name ( @{$langData->{month_name}->[$month_idx-1]} ) {
my ($w, $k, $pi, $pe, $alt) = _fix_key ( $name );
$wide = 1 if ($w);
next if ( $pe && exists $months{$alt} && $months{$alt} == $month_idx );
$has_period = 1 if ( $pi || $pe );
$months{$k} = $month_idx;
}
foreach my $abb ( @{$langData->{month_abb}->[$month_idx-1]} ) {
my ($w, $k, $pi, $pe, $alt) = _fix_key ( $abb );
$wide = 1 if ($w);
next if ( $pe && exists $months{$alt} && $months{$alt} == $month_idx );
$has_period = 1 if ( $pi || $pe );
$months{$k} = $month_idx;
}
my $first_name = $langData->{month_name}->[$month_idx-1]->[0];
my $first_abb = $langData->{month_abb}->[$month_idx-1]->[0];
push ( @MoY, (_fix_key ($first_name, 1))[1] );
push ( @MoYs, (_fix_key ($first_abb, 1))[1] );
}
$issues{month_period} = $has_period;
lib/Advanced/Config/Date.pm view on Meta::CPAN
{
DBUG_ENTER_FUNC ( @_ );
my $lang = shift;
my $warn_ok = shift;
my $allow_wide = shift;
my $k = lc ($lang);
my $manip_ref = $date_manip_installed_languages{$k};
my $lang_ref = $date_language_installed_languages{$k};
if ( $manip_ref && ! $lang_ref ) {
$k = lc ($manip_ref->{Language});
$lang_ref = $date_language_installed_languages{$k};
}
unless ( $lang_ref || $manip_ref ) {
_warn_msg ( $warn_ok, "Language '$lang' does not exist! So can't swap to it!" );
return DBUG_RETURN ( undef, undef );
}
unless ( $allow_wide ) {
$manip_ref = undef if ( $manip_ref && $manip_ref->{Wide} );
$lang_ref = undef if ( $lang_ref && $lang_ref->{Wide} );
unless ( $lang_ref || $manip_ref ) {
_warn_msg ( $warn_ok, "Language '$lang' uses Wide Chars. It's not currently enabled!" );
return DBUG_RETURN ( undef, undef );
}
}
DBUG_RETURN ( $manip_ref, $lang_ref );
}
# ==============================================================
=item $lang = swap_language ( $language[, $give_warning[, $wide]] );
This method allows you to change the I<$language> used when this module parses
a date string if you have modules L<Date::Language> and/or L<Date::Manip>
installed. But if neither are installed, only dates in B<English> are
supported. If a language is defined in both places the results are merged.
It always returns the active language. So if I<$language> is B<undef> or
invalid, it will return the current language from before the call. But if the
language was successfully changed, it will return the new I<$language> instead.
Should the change fail and I<$give_warning> is set to a non-zero value, it will
write a warning to your screen telling you why it failed.
So assuming one of the language modules are installed, it asks it for the list
of months in the requested language. And once that list is retrieved only
months in that language are supported when parsing a date string.
Languages like 'Greek' that rely on I<Wide Chars> require the I<$wide> flag set
to true. Otherwise that language is disabled. Using the I<use_ut8> option
when creating the Advanced::Config object causes the I<$wide> flag to be set to
B<1>.
=cut
# NOTE: Sets the following global variables for use by parse_date() ...
# %last_language_edit_flags
# %Months
# %Days
sub swap_language
{
DBUG_ENTER_FUNC ( @_ );
my $lang = shift;
my $warn_ok = shift;
my $allow_wide = shift || 0;
if ( (! defined $lang) || lc($lang) eq lc($last_language_edit_flags{language}) ) {
return DBUG_RETURN ( $last_language_edit_flags{language} );
}
my ($manip_ref, $lang_ref) = _select_language ($lang, $warn_ok, $allow_wide);
unless ( $lang_ref || $manip_ref ) {
return DBUG_RETURN ( $last_language_edit_flags{language} );
}
my ($month_ref, $day_ref, $issue1_ref);
if ( $manip_ref ) {
my $old = $manip_ref->{Language};
($month_ref, $day_ref, $issue1_ref) =
_swap_manip_language_common ($manip_ref, $warn_ok, $allow_wide );
$lang = $manip_ref->{Language};
if ( $old ne $lang && ! $lang_ref ) {
$lang_ref = $date_language_installed_languages{lc($lang)};
$lang_ref = undef if ($lang_ref && $lang_ref->{Wide} && ! $allow_wide);
}
}
my ($MoY_ref, $MoYs_ref, $Dsuf_ref, $issue2_ref);
if ( $lang_ref ) {
my ($unused_DoW_ref, $unused_DoWs_ref);
($MoY_ref, $MoYs_ref, $Dsuf_ref, $unused_DoW_ref, $unused_DoWs_ref, $issue2_ref) =
_swap_lang_common ( $lang_ref, $warn_ok, $allow_wide );
$lang = $lang_ref->{Language};
}
unless ( $MoY_ref || $month_ref ) {
return DBUG_RETURN ( $last_language_edit_flags{language} );
}
DBUG_PRINT ("SWAP", "Swapping from '%s' to '%s'.",
$last_language_edit_flags{language}, $lang);
# ---------------------------------------------------------
foreach my $k ( keys %last_language_edit_flags ) {
$last_language_edit_flags{$k} = $issue1_ref->{$k} || $issue2_ref->{$k} || 0;
}
$last_language_edit_flags{language} = $lang;
# ---------------------------------------------------------
# Bug Alert: For some languges the following isn't true!
# lc(MoY) != lc(uc(lc(MoY)))
# So we have multiple lower case letters mapping to the
# same upper case letters#.
lib/Advanced/Config/Date.pm view on Meta::CPAN
my $day;
my $digits = 0;
my @lst = sort { length($b) <=> length($a) || $a cmp $b } keys %Days;
my $all_digits = $skip_period ? "^\\d+[.]?\$" : "^\\d+\$";
foreach my $dom ( @lst ) {
# Ignore numeric keys, can't get the correct one from string ...
next if ( $dom =~ m/${all_digits}/ );
my $flag1 = ( $last_language_edit_flags{dsuf_period} &&
$dom =~ s/[.]/\\./g );
if ( $month_str ) {
# Makes sure dom doesn't match month name ...
$month_str =~ s/[.]/\\./g;
if ( $date_str =~ m/${month_str}.*(${dom})/ ||
$date_str =~ m/(${dom}).*${month_str}/ ) {
$day = $1;
$day =~ s/[.]/\\./g if ( $flag1 );
last;
}
# There is no month name to worry about ...
} elsif ( $date_str =~ m/(${dom})/ ) {
$day = $1;
$day =~ s/[.]/\\./g if ( $flag1 );
last;
}
}
# Allow any number between 1 and 31 ...
unless ( $day ) {
$day = "[1-9]|0[1-9]|[12][0-9]|3[01]";
$digits = 1;
}
DBUG_RETURN ( $day, $digits ); # Suitable for use in a RegExpr.
}
# ==============================================================
=item adjust_future_cutoff ( $num_years );
Changes the cutoff future date from B<30> years to I<$num_years>.
Set to B<0> to disable years in the future!
This affects all L<Advanced::Config> objects, not just the current one.
=cut
sub adjust_future_cutoff
{
DBUG_ENTER_FUNC ( @_ );
my $years = shift;
if ( defined $years && $years =~ m/^\d+$/ ) {
$global_cutoff_date = shift;
}
DBUG_VOID_RETURN ();
}
# ==============================================================
=item $year = make_it_a_4_digit_year ( $two_digit_year );
Used whenever this module needs to convert a two-digit year into a four-digit
year.
When it converts YY into YYYY, it will assume 20YY unless the
resulting date is more than B<30> years in the future. Then it's 19YY.
If you don't like this rule, use B<adjust_future_cutoff> to change
this limit!
=cut
sub make_it_a_4_digit_year
{
DBUG_ENTER_FUNC ( @_ );
my $year = shift || 0; # Passed as a 2-digit year ...
$year += 2000; # Convert it to a 4-digit year ...
# Get the current 4-digit year ...
my $this_yr = (localtime (time()))[5];
$this_yr += 1900;
if ( $this_yr < $year && ($year - $this_yr) >= $global_cutoff_date ) {
$year -= 100; # Make it last century instead.
}
DBUG_RETURN ( $year );
}
# ==============================================================
=item ($year, $month, $day) = parse_8_digit_date ( $date_str, $order[, $skip] );
Looks for a valid date in an 8 digit string. It checks each of the formats below
in the order specified by I<$order> until it hits something that looks like a
valid date.
(1) YYYYMMDD - ISO
(2) MMDDYYYY - American
(3) DDMMYYYY - European
The I<$order> argument helps deal with ambiguities in the date. Its a comma
separated list of numbers specifying to order to try out. Ex: 3,2,1 means
try out the European date format 1st, then the American date format 2nd, and
finally the ISO format 3rd. You could also just say I<$order> is B<3> and
only accept European dates.
It assumes its using the correct format when the date looks valid. It does this
by validating the B<MM> is between 1 and 12 and that the B<DD> is between 1 and
31. (Using the correct max for that month). And then assumes the year is
always valid.
If I<$skip> is a non-zero value it will skip over the B<ISO> format if it's
listed in I<$order>.
Returns 3 B<undef>'s if nothing looks good.
=cut
sub parse_8_digit_date
{
DBUG_ENTER_FUNC ( @_ );
my $date_str = shift;
my $order = shift;
my $skip_iso = shift || 0;
my @order = split (/\s*,\s*/, $order);
my @lbls = ( "", "YYYYMMDD - ISO", "MMDDYYYY - American", "DDMMYYYY - European" );
my ( $year, $month, $day );
foreach my $id ( @order ) {
next unless ( defined $id && $id =~ m/^[123]$/ );
my ( $y, $m, $d ) = ( 0, 0, 0 );
if ( $id == 1 && (! $skip_iso) && # YYYYMMDD - ISO
$date_str =~ m/^(\d{4})(\d{2})(\d{2})$/ ) {
( $y, $m, $d ) = ( $1, $2, $3 );
}
if ( $id == 2 && # MMDDYYYY - American
$date_str =~ m/^(\d{2})(\d{2})(\d{4})$/ ) {
( $m, $d, $y ) = ( $1, $2, $3 );
lib/Advanced/Config/Date.pm view on Meta::CPAN
=cut
sub init_special_date_arrays
{
DBUG_ENTER_FUNC ( @_ );
my $lang = shift;
my $mode = shift || 0; # Default to numeric arrays ...
my $warn_ok = shift || 0;
my $allow_wide = shift || 0;
my @months = ( "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" );
my @week_days = ( "1", "2", "3", "4", "5", "6", "7" );
my $numbers = ($mode != 1 && $mode != 2 );
my ( $lang_ref, $manip_ref );
if ( defined $lang ) {
($manip_ref, $lang_ref) = _select_language ($lang, $warn_ok, $allow_wide);
unless ( $lang_ref || $manip_ref ) {
$lang = undef; # So it will enter the early out if block ...
}
}
if ( (! defined $lang) || lc($lang) eq lc($prev_array_lang) || $numbers ) {
if ( $mode == 1 ) {
@months = @gMoYs; # Abrevited month names ...
@week_days = @gDoWs; # Abrevited week names ...
} elsif ( $mode == 2 ) {
@months = @gMoY; # Full month names ...
@week_days = @gDoW; # Full week names ...
}
return DBUG_RETURN ( \@months, \@week_days );
}
my ($MoY_ref, $MoYs_ref, $Dsuf_ref, $DoW_ref, $DoWs_ref);
DBUG_PRINT ("INFO", "Manip: %s, Lang: %s", $manip_ref, $lang_ref);
if ( $manip_ref ) {
my ( $u1, $u2, $u3 ); # Unused placeholders.
($u1, $u2, $u3, $MoY_ref, $MoYs_ref, $Dsuf_ref, $DoW_ref, $DoWs_ref) =
_swap_manip_language_common ($manip_ref, $warn_ok, $allow_wide );
$lang = $manip_ref->{Language};
if ( $u1 ) {
$lang_ref = undef; # Skip lang_ref lookup if successsful ...
} else {
$lang_ref = $date_language_installed_languages{lc($lang)};
}
}
if ( $lang_ref ) {
($MoY_ref, $MoYs_ref, $Dsuf_ref, $DoW_ref, $DoWs_ref) =
_swap_lang_common ( $lang_ref, $warn_ok, $allow_wide );
$lang = $lang_ref->{Language};
}
# If the new language was valid, update the global variables ...
if ( $MoY_ref ) {
$prev_array_lang = $lang;
@gMoY = @{$MoY_ref};
@gMoYs = map { uc($_) } @{$MoYs_ref};
@gDoW = @{$DoW_ref};
@gDoWs = map { uc($_) } @{$DoWs_ref};
@gDsuf = @{$Dsuf_ref};
DBUG_PRINT ( "LANGUAGE", "%s\n%s\n%s\n%s\n%s",
join (", ", @gMoY), join (", ", @gMoYs),
join (", ", @gDoW), join (", ", @gDoWs),
join (", ", @gDsuf)
);
}
# Numeric handled earlier ...
if ( $mode == 1 ) {
@months = @gMoYs; # Abrevited month names ...
@week_days = @gDoWs; # Abrevited week names ...
} elsif ( $mode == 2 ) {
@months = @gMoY; # Full month names ...
@week_days = @gDoW; # Full week names ...
}
DBUG_RETURN ( \@months, \@week_days );
}
# ==============================================================
sub _is_leap_year
{
my $year = shift;
my $leap = ($year % 4 == 0) && ($year % 100 != 0 || $year % 400 == 0);
return ($leap ? 1 : 0);
}
# ==============================================================
# Validate the input date.
sub _validate_date_str
{
DBUG_ENTER_FUNC ( @_ );
my $date_str = shift;
my ($year, $mon, $day);
if ( defined $date_str && $date_str =~ m/^(\d+)-(\d+)-(\d+)$/ ) {
($year, $mon, $day) = ($1, $2, $3);
my $leap = _is_leap_year ($year);
local $days_in_months[2] = $leap ? 29 : 28;
unless ( 1 <= $mon && $mon <= 12 &&
1 <= $day && $day <= $days_in_months[$mon] ) {
return DBUG_RETURN ( undef, undef, undef );
}
} else {
return DBUG_RETURN ( undef, undef, undef );
}
DBUG_RETURN ( $year, $mon, $day );
}
( run in 1.134 second using v1.01-cache-2.11-cpan-39bf76dae61 )