Finding Palm Sunday - perl

Palm Sunday is the Sunday before Easter Sunday. Finding Easter Sunday can be achieved with:
use Time::Moment;
use Time::Moment::Adjusters qw(WesternEasterSunday PreviousDayOfWeek);
# 2018-04-01T00:00:00Z
my $easter_sunday = Time::Moment->new(year => 2018)->with(WesternEasterSunday);
Finding the Sunday before that could be achieved with Time::Moment::Adjusters' PreviousDayOfWeek(7):
$adjuster = PreviousDayOfWeek($day);
The $adjuster adjusts the date to the previous occurrence of the given day of the week [1=Monday, 7=Sunday] that is before the date.
But if I apply this adjuster, I get a Tuesday five days earlier!
# 2018-03-27T00:00:00Z
my $palm_sunday = $easter_sunday->with(PreviousDayOfWeek(7))
Since Palm Sunday is always seven days prior to Easter Sunday, I could achieve this with Time::Moment's minus_days(7), but since I want to find a number of other holidays that are much simpler to find using Time::Moment::Adjusters, I would really like to find the root of this unexpected behavior.

Thank you Simon for the report and the PR and thank you #simbabque for the test! I have shipped v0.44 to CPAN. You are excellent citizens in the opensource community!
--
chansen

Related

how many days have passed since Friday - Perl Variable

I'm trying to calculate the number of days that have passed since last Friday?
For example, today is 7/19/2022, and last Friday was 7/15/2022 therefore, four days have passed.
7/19/2022 - 7/15/2022 = 4 days
I need this to update every day to just display # of days.
Can someone help me write this out to assign to a variable?
Thank you in advance!!! (new to coding)
You can use localtime and simple modular arithmetic:
( (localtime)[6] - 5 ) % 7
( (current day index) - (friday day index) ) mod (days in a week)
You can use Date::Calc Module. Here is the one liner version.
perl -E 'use Date::Calc qw(:all); say Delta_Days(2022,7,15, 2022,7,19,)'

how to get a week's date with autohotkey

i'm using autohotkey to work with date, i need to catch the day a week ago
example
if today is the 28th then I have to take the 21st of last week
calendar
in the following script I take the current date
FormatTime, date, , dd/MM/yyyy
MsgBox %date%
I even thought of a logic, to take the current day subtract by 7 that will take the day a week ago. I need help to create a better script
28 - 7 = 21
if anyone can help me thanks :)
Just subtracting numbers would be bad when you encountered a change between months.
Would have to create custom logic for that.
Luckily AutoHotkey's += operator supports date/time math.
So this is all you need:
;we're starting off the date1 variable as blank,
;which means the current time will be used.
date1 += -7, days
FormatTime, finalDate, % date1, dd/MM/yyyy ;format the result to our desired format
MsgBox, % finalDate
I did it that way
FormatTime, date_, , dd
sub += date_-7
FormatTime date, , /MM/yyyy
MsgBox,%sub%%date%

Get last day of month in Template Toolkit / Date::Manip

I want to offer promotions on my site that always begin and end on the first and last days of the current month.
Getting the first day of the month is trivial (it's always the first), but I'd like to also do the same for the last day of the month.
In the docs it mentions that you can use either Date::Manip or Date::Calc , but based on those documentations I'm not clear on the best way to accomplish this.
What's the best way to show the last day of the month in Template Toolkit?
The way I got this working was basically to load the Date::Calc module through the template toolkit plugin. Then I set "last_day" to the number of days in the month.
The Days_in_Month call requires the date and month (to handle leap years), so I pass the result of date format.
This is the doc containing the format for dates, which is where I got %m.
[%
calc = date.calc;
last_day = calc.Days_in_Month(date.format(date.now(), "%Y"), date.format(date.now(), "%m"));
%]
So to show a range of Month/Day that begins and ends within the current month, I do:
[% date.format(date.now(), "%m/01") %] - [% date.format(date.now(), "%m/") %][% last_day %]
... since the last day of the month will always be double digits, there's no need to pad it with zeros.
Make sure you install the Date::Calc module. If you have it at all, Template Toolkit will load it.

Get week number according to your locale in Zend_Date

I am trying to get the current week number with Zend Framework.
In France, weeks are defined like this :
A week begins on Monday (whereas weeks begin on Sunday in the US).
The first week of the year is the week which contains the 4th of January.
In 2014, the first week begins January 1st. But if I use in Zend Framework 1.12 for this date like $zend_date->get(Zend_Date::WEEK) it returns 53 (and not 1). and for January 12th, it returns 1 (and not 2)
How can I correct that ? I already tried to change the locale to fr-fr but it didn't work.
Regards
Tried to replicate your problem doing
$date1 = new Zend_Date('2014-01-01');
$date2 = new Zend_Date('2014-01-12');
$date1->get(Zend_Date::WEEK) // gives 01
$date2->get(Zend_Date::WEEK) // gives 02
Also tried passing 'fr' as locale. It gives the correct answer.
I am not sure if it's a Zend issue.
Try the PHP solution:
php > echo date('W',strtotime('2014-01-01'));
01
php > echo date('W',strtotime('2014-01-12'));
02

Perl workweek conversion is incorrect

I'm faced a weird problem.
I have date in form of Tue Feb 25 00:20:13 2014.
my task is to calculate the week number and the week day.
I tried the following
use Time::Piece;
my $date="Tue Feb 25 00:20:13 2014";
my $db_date=Time::Piece->strptime($date, "%a %b %d %H:%M:%S %Y");
my $ww=$db_date->strftime("%W.%w-%Y);
print $ww;
When I run the script I get the output as
08.2-2014
which is wrong, the expected output is
09.2-2014
I want to know where did i go wrong?
pls help...
You're using the "%W" strftime() conversion. Time::Piece doesn't specify the meaning of "%W", but the documentation for the equivalent C function says that "%W" starts counting with the first week that contains a Monday. It sounds like you want the ISO 8601 week number, which starts counting with the first week that contains at least four days, in which case the "%V" conversion should do what you want.