The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

EAI::DateUtil - Date and Time helper functions for EAI::Wrap

SYNOPSIS

 monthsToInt ($mmm, $locale)
 intToMonths ($m, $locale)
 addLocaleMonths ($locale, $monthsArray)
 get_curdate ()
 get_curdatetime ()
 get_curdate_dot ()
 formatDate ($y, $m, $d, [$template])
 formatDateFromYYYYMMDD ($date, [$template])
 get_curdate_gen ([$template])
 get_curdate_dash ()
 get_curdate_dash_plus_X_years ($years)
 get_curtime_epochs ()
 make_time ($time, [$secondsToAdd])
 formatTime ($timeval [$format])
 get_curtime ([$format, $secondsToAdd])
 get_curtime_HHMM ()
 is_first_day_of_month ($date YYYYMMDD)
 is_last_day_of_month ($date YYYYMMDD, [$cal])
 get_last_day_of_month ($date YYYYMMDD)
 weekday ($date YYYYMMDD)
 is_weekend ($date YYYYMMDD)
 is_holiday ($cal, $date YYYYMMDD)
 is_easter ($cal, $date YYYYMMDD)
 addCalendar ($cal, $fixedHol hash, $easterHol .. hash, $specialFunction)
 first_week ($d,$m,$y,$day,[$month])
 first_weekYYYYMMDD ($date,$day,[$month])
 last_week ($d,$m,$y,$day,[$month])
 last_weekYYYYMMDD ($date,$day,[$month])
 convertDate ($date YYYY.MM.DD or YYYY/MM/DD)
 convertDateFromMMM ($inDate dd-mmm-yyyy, out $day, out $mon, out $year, [$locale])
 convertDateToMMM ($day, $mon, $year, [$locale])
 convertToDDMMYYYY ($date YYYYMMDD)
 addDays ($day, $mon, $year, $dayDiff, [$locale])
 subtractDays ($date, $days)
 addDaysHol ($date, $days, [$template, $cal])
 subtractDaysHol ($date, $days, [$template, $cal])
 addDatePart ($date, $count, $datepart, [$template])
 get_lastdateYYYYMMDD ()
 get_lastdateDDMMYYYY ()
 convertcomma ($number, $divideBy)
 convertToThousendDecimal($value, $ignoreDecimal)
 get_dateseries ($fromDate, $toDate, $cal)
 parseFromDDMMYYYY ($dateStr)
 parseFromYYYYMMDD ($dateStr)
 convertEpochToYYYYMMDD ($epoch)

DESCRIPTION

EAI::DateUtil contains all date/time related API-calls.

API

monthsToInt ($$)

convert from english/german/custom locale short months -> numbers, monthsToInt("Oct","en") equals 10, monthsToInt("mär","ge") equals 3. months and locale are case insensitive.

 $mon ..  month in textual format (as defined in locale)
 $locale .. locale as defined in monthsToInt (builtin "en" and "ge", can be added with addLocaleMonths)
intToMonths ($$)

convert from int to english/german/custom locale months -> numbers, intToMonths(10,"en") equals "Oct", intToMonths(3,"ge") equals "Mär". locale is case insensitive, month is returned with first letter uppercase resp. as it was added (see below).

 $mon ..  month in textual format (as defined in locale)
 $locale .. locale as defined in monthsToInt (builtin "en" and "ge", can be added with addLocaleMonths)
addLocaleMonths ($$)

adds custom locale $locale with ref to array $monthsArray to above conversion functions. locale is case insensitive.

 $locale .. locale to be defined
 $months .. ref to array of months in textual format

Example:

 addLocaleMonths("fr",["Jan","Fév","Mars","Mai","Juin","Juil","Août","Sept","Oct","Nov","Déc"]);
get_curdate

gets current date in format YYYYMMDD

get_curdatetime

gets current datetime in format YYYYMMDD_HHMMSS

get_curdate_dot

gets current date in format DD.MM.YYYY

formatDate ($$$;$)

formats passed date (given in arguments $y,$m,$d) into format as defined in $template

 $y .. year part
 $m .. month part
 $d .. day part
 $template .. optional, date template with D for day, M for month and Y for year (e.g. D.M.Y for 01.02.2016),
              D and M is always 2 digit, Y always 4 digit; if empty/nonexistent defaults to "YMD"
              special formats are MMM und mmm als monthpart, here three letter month abbreviations in english (MMM) or german (mmm) are returned as month
              additionally a locale can be passed in brackets with MMM and mmm, resulting in conversion to locale dependent months.
              e.g. formatDate(2002,6,1,"Y-MMM-D[fr]") would yield 2002-Juin-01 for the addLocaleMonths given above.
formatDateFromYYYYMMDD ($;$)

returns passed date (argument $date) formatted as defined in $template

 $date .. date in format YYYYMMDD
 $template .. same as in formatDate above
get_curdate_gen (;$)

returns current date in format as defined in $template

 $template .. same as in formatDate above
get_curdate_dash

returns current date in format DD-MM-YYYY

get_curdate_dash_plus_X_years ($;$$)
 $y .. years to be added to the current or given date
 $date .. optional date to which X years should be added (if not given, then current date is taken instead).
 $daysToSubtract .. optional days that should be subtracted from above result

returns (current or given) date + X years in format DD-MM-YYYY

get_curtime_epochs

returns current time in epochs as from builtin function time()

get_curtime (;$$)

returns current time in format HH:MM:SS + optional $secondsToAdd (or as given in formatstring $format, however ordering of format is always hour, minute and second). Additionally 1 is put in front if adding $secondsToAdd lead to a day overflow.

 $format .. optional sprintf format string (e.g. %02d:%02d:%02d) for hour, minute and second. If less than three tags are passed then a warning is "Redundant argument in sprintf at ..." is thrown here.
 $secondsToAdd .. optional seconds to add to current time before returning
get_curtime_HHMM

returns current time in format HHMM

make_time ($;$)

returns localtime datastructure from given $time argument (format HHMMSS) optionally adding $secondsToAdd

 $time .. time in format HHMMSS to make a localtime datastructure
 $secondsToAdd .. optional seconds to add to current time before returning
formatTime ($;$)

returns formatted time from localtime datastructure $timeval using optional format string $format (defaulting to %02d:%02d:%02d). Supported are format strings containing 1, 2, 3 or 4 %d placeholders.

 $timeval .. localtime datastructure that should be formatted
 $format .. optional sprintf format string (e.g. %02d %02d:%02d:%02d) for (maximum) day, hour, minute and second to return. There can also be three decimals, (hour, minute and second), two (hour, minute) and one (only hour).
is_first_day_of_month ($)

returns 1 if first day of months, 0 else

 $date .. date in format YYYYMMDD
is_last_day_of_month ($;$)

returns 1 if last day of month, 0 else

 $date .. date in format YYYYMMDD
 $cal .. optional, calendar for holidays used to get the last of month
get_last_day_of_month ($)

returns last day of month of passed date

 $date .. date in format YYYYMMDD
weekday ($)

returns 1..sunday to 7..saturday

 $date .. date in format YYYYMMDD
is_weekend ($)

returns 1 if saturday or sunday

 $date .. date in format YYYYMMDD
is_holiday ($$)

returns 1 if weekend or holiday

 $cal .. holiday calendar; currently supported: AT (Austria), TG (Target), UK (see is_holiday) and WE (for only weekends).
         throws warning if calendar not supported (fixed lookups or additionally added). To add a calendar use addCalendar.
 $date .. date in format YYYYMMDD
is_easter ($$)

returns 1 if date is an easter holiday for that calendar

 $cal .. holiday calendar;
 $date .. date in format YYYYMMDD
addCalendar ($$$$)

add an additional calendar for calendar holiday dependent calculations

 $cal .. name of holiday calendar to be added, warns if already existing (builtin)
 $fixedHol .. hash of fixed holiday dates for that calendar (e.g. {"0105"=>1,"2512"=>1} for may day and christmas day)
 $easterHol .. hash of easter holidays for that calendar (possible: {"GF"=>1,"EM"=>1,"ES"=>1,"AS"=>1,"WM"=>1,"CC"=>1}) = good friday,easter monday, easter sunday, ascension day, whitmonday, corpus christi day
 $specialFunction .. pass ref to sub used for additional calculations; this sub should receive a date (YYYYMMDD) and return 1 for holiday, 0 otherwise.

Example:

 sub testCalSpecial {
   my ($y,$m,$d) = $_[0] =~ /(.{4})(..)(..)/;
   return 1 if $y eq "2002" and $m eq "09" and $d eq "08";
   return 0;
 }
 addCalendar("TC",{"0101"=>1,"0105"=>1,"2512"=>1,"2612"=>1},{"EM"=>1,"GF"=>1},\&testCalSpecial);
first_week ($$$$;$)

returns 1 if given date ($d,$m,$y) is the first given weekday ($day: 0 - 6, sunday==0) in given month ($month), if $month is not passed, then it is taken from passed date.

 $d .. day part
 $m .. month part
 $y .. year part
 $day .. given weekday
 $month .. optional, given month
first_weekYYYYMMDD ($$;$)

returns 1 if given date ($date in Format YYYYMMDD) is the first given weekday ($day: 0 - 6, sunday==0) in given month ($month), if $month is not passed, then it is taken from passed date.

 $date .. given date
 $day .. given weekday
 $month .. optional, given month
last_week ($$$$;$)

returns 1 if given date ($d,$m,$y) is the last given weekday ($day: 0 - 6, sunday==0) in given month ($month), if $month is not passed, then it is taken from passed date.

 $d .. day part
 $m .. month part
 $y .. year part
 $day .. given weekday
 $month .. optional, given month
last_weekYYYYMMDD ($$;$)

returns 1 if given date ($date in Format YYYYMMDD) is the last given weekday ($day: 0 - 6, sunday==0) in given month ($month), if $month is not passed, then it is taken from passed date.

 $date .. given date
 $day .. given weekday
 $month .. optional, given month
convertDate ($)

converts given date to format YYYYMMDD

 $date .. date in format YYYY.MM.DD or YYYY/MM/DD
convertDateFromMMM ($$$$;$)

converts date from format dd-mmm-yyyy (mmm as defined in $locale, defaults to "en"glish), returns date in format DD.MM.YYYY ($day, $mon, $year are returned by ref as well)

 $inDate .. date to be converted
 $day .. ref for day part
 $mon .. ref for month part
 $year ..  ref for year part
 $locale .. optional locale as defined in monthsToInt (builtin "en" and "ge", can be added with addLocaleMonths)
convertDateToMMM ($$$;$)

converts date into format dd-mmm-yyyy (mmm as defined in $locale, defaults to "en"glish) from ($day, $mon, $year)

 $day .. day part
 $mon .. month part
 $year .. year part
 $locale .. optional locale as defined in monthsToInt (builtin "en" and "ge", can be added with addLocaleMonths)
convertToDDMMYYYY ($)

converts date into $datestring (dd.mm.yyyy) from format YYYYMMDD

 $date .. date in format YYYYMMDD
addDays ($$$$;$)

adds $dayDiff to date ($day, $mon, $year) and returns in format dd-mmm-yyyy (mmm as defined in $locale, defaults to "en"glish), arguments $day, $mon, $year are returned by ref as well if not passed as literal

 $day .. day part
 $mon .. month part
 $year .. year part
 $dayDiff .. days to be added
 $locale .. optional locale as defined in monthsToInt (builtin "en" and "ge", can be added with addLocaleMonths)
subtractDays ($$)

subtracts $days actual calendar days from $date

 $date .. date in format YYYYMMDD
 $days .. calendar days to subtract
addDaysHol ($$;$$)

adds $days days to $date and regards weekends and holidays of passed calendar

 $date .. date in format YYYYMMDD
 $days .. calendar days to add
 $template .. as in formatDate
 $cal .. holiday calendar; currently supported: NO (no holidays = default if not given), rest as in is_holiday
subtractDaysHol ($$;$$)

subtracts $days days from $date and regards weekends and holidays of passed calendar

 $date .. date in format YYYYMMDD
 $days .. calendar days to subtract
 $template .. as in formatDate
 $cal .. holiday calendar; currently supported: NO (no holidays  = default if not given), rest as in is_holiday
addDatePart ($$$;$)

adds $count dateparts to $date. when adding to months ends (>28 in february, >29 or >30 else), if the month end is not available in the target month, then date is moved into following month

 $date .. date in format YYYYMMDD
 $count .. count of dateparts to add
 $datepart .. can be "d" or "day" for days, "m"/"mon"/"month" for months and "y" or "year" for years
 $template .. as in formatDate
get_lastdateYYYYMMDD

returns the last business day (only weekends, no holiday here !) in format YYYYMMDD

get_lastdateDDMMYYYY

returns the last business day (only weekends, no holiday here !) in format DDMMYYYY

convertcomma ($$)

converts decimal point in $number to comma, also dividing by $divideBy before if $divideBy is set

 $number ..  number to be converted
 $divideBy .. number to be divided by
convertToThousendDecimal ($$)

converts $value into thousand separated decimal (german format) ignoring decimal places if wanted

 $value .. number to be converted
 $ignoreDecimal .. return number without decimal places (truncate)
get_dateseries ($$$)

returns date values (format YYYYMMMDD) starting at $fromDate until $toDate, if a holiday calendar is set in $cal (optional), these holidays (incl. weekends) are regarded as well.

 $fromDate .. start date
 $toDate .. end date
 $cal .. holiday calendar
parseFromDDMMYYYY ($)

returns time epoch from given datestring (dd.mm.yyyy)

 $dateStr .. datestring
parseFromYYYYMMDD ($)

returns time epoch from given datestring (yyyymmdd)

 $dateStr .. datestring
convertEpochToYYYYMMDD ($)

returns datestring (yyyymmdd) from epoch/Time::piece

 $arg .. date either as epoch (seconds since 1.1.1970) or as Time::piece object

COPYRIGHT

Copyright (c) 2024 Roland Kapl

All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.