Time based switch in Twig - date

I currently have a php switch statement to display content at a certain time. I want to be able to rewrite this as a twig statement.
The php switch is this:
switch($day){
case "Sun":
if ($time >= 00 && $time <= 12){
CODE...;
}elseif ($time >= 12 && $time <= 14){
CODE...;
}elseif ($time >= 14 && $time <= 2359){
CODE...;
}
break;
cases for every day....
}
Obviously I call the day and time with:
date_default_timezone_set('Europe/London');
$day = date('D');
$time = date('H:i');
I understand how twig switches work but I'm confused on how I could attach time and day variables.
Hope you can help!

Related

Why the numbers are not resolved in IF condition in Perl without quotes

I have a script which will do certain set of operation based on the time in IF condition. So basically IF will look for the Current Time which is Hour and Minutes.
Here is the script:
use strict;
use warnings;
my $TIME = `date '+%H%M'`;
chomp $TIME;
print $TIME."\n";
if (( $TIME > 0100) && ($TIME < 0300)){
print "1st IF\n";
} elsif (( $TIME > 0300) && ($TIME < 0500)){
print "2nd IF\n";
} else {
print "Default IF\n";
}
Lets say now the time is 0130 and it suppose to print 1st IF. But its printing Default IF.
Similarly when the time is 0430 and it suppose to print 2nd IF. But its printing Default IF.
But when I enclose the time value in double quotes "" it works. Like below -
if (( $TIME > "0100") && ($TIME < "0300")){
Do the $TIME is resolving as a string ? I was thinking the $TIME would have numbers and it doesn't require quotes.
As you can find in perlnumber, literal numbers starting with zeros are interpreted as octal numbers. 0100 is therefore 64 and 0300 is 192.
When numifying a string, leading zeros are ignored.
See also Time::Piece on how to handle time in Perl without the need to shell out.
use Time::Piece;
my $time = localtime->strftime('%H%M');
Time::Piece has been a standard part of the Perl distribution since Perl 5.10 (in 2002). If you use the right tool for the job, then things get a lot easier :-)
use Time::Piece;
my $hour = localtime->hour;
if ($hour > 1 and $hour < 3) {
# do something
} elsif ($hour > 3 and $hour < 5) {
# do something else
} else {
# do the default thing
}
Update: You don't even need to use Time::Piece. You can get the hour from localtime() easily enough.
my $hour = (localtime)[2];
# The rest of the code is the same as my previous example

While loop until time is reached

I have an application that loops reading a big text file and runs for at least 48 hours.
I would like to stop its execution from 23.00 to 08.00 (11pm to 8am) and then continue its loop during the day.
I tried the following code, but it doesn't exit from the loop.
Edit after Dave Cross' reply
my $min = (localtime)[1];
my $hour = (localtime)[2];
while ( $hour > 18 and $min > 0 ) {
sleep(1);
$hour = (localtime)[2];
$min = (localtime)[1];
}
This doesn't work as expected. Now it's 18:48 and the loop exits immediately unless I use one hour before:
while ( $hour > 17 and $min > 0 )
You need to check the time more than once :-)
And rather than calling strftime(), why not just use the return value from localtime()?
my $hour = (localtime)[2];
while ($hour >= 23 or $hour < 8) {
sleep(1);
$hour = (localtime)[2];
}
Update: A more efficient approach might be to work out how long to sleep() in order to wake up at 8am. A naive approach would be something like this:
if ((localtime)[2]) >= 23) {
sleep(9 * 60 * 60); # sleep for nine hours
}

Perl find closest date in array

How do I get the closest date that is earlier than the input date or equal to from an array using an input date?
For example, my array would look like this.
#dates = ("200811","200905","200912","201005","201202");
and my input date is
$inputdate = "201003";
How do I get the closest date in the array which is "200912".
The format of the date is YEARMM.
Thanks
Sort the dates, select only the ones preceding the input date, take the last such one:
print ((grep $_ <= $inputdate, sort #dates)[-1]);
use List::Util qw( max );
my $date = max grep { $_ <= $inputdate } #dates;
The logic here is to go one year back and change month from January to December if the month is January, otherwise go back one month in the same year.
I don't code much in Perl, the code in PHP is:
(I'm putting it here to give you the logic. Coding it should be trivial)
$dates = array("200811","200905","200912","201005","201202");
$inputdate = "201003";
$date = $inputdate;
while ($found==0) {
if (in_array($date, $dates)) {
$found = 1;
echo "the date is " . $date;
}
if ($date%100==1) { // if it's january, we need to change to december of the previous year
$date = $date - 100 + 12;
}
else {
$date = $date - 1; //go one month back in the same year
}
}

Format Date and text

I need to format the pubDate to example :
2 hours ago, 2 days ago or 20 hours ago ....
while keeping the original date of the feed cause i tried to format before and i get all my feeds same pubDate the one i set in Date format and the date builder. How can i solve this issue ?
As well i tried to format the text of my pubdate and (Read full article ) when i run pipe its perfect but in pipe output and when i call the rss file from my website i always get the format as code.
You help will be greatly appreciated...
function ShowDate($date) // $date --> time(); value
{
$stf = 0;
$cur_time = time();
$diff = $cur_time - $date;
$phrase = array('second','minute','hour','day','week','month','year','decade');
$length = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($i =sizeof($length)-1; ($i >=0)&&(($no = $diff/$length[$i])< =1); $i--); if($i < 0) $i=0; $_time = $cur_time -($diff%$length[$i]);
$no = floor($no); if($no <> 1) $phrase[$i] .='s'; $value=sprintf("%d %s ",$no,$phrase[$i]);
if(($stf == 1)&&($i >= 1)&&(($cur_tm-$_time) > 0)) $value .= time_ago($_time);
return $value.' ago ';
}
You need to pass the date within that and you can get the desire format.
This is php function that will convert the time as a 2 hours ago and so on...
For more further function you can check this link Date time formate

How do I get an array of days of the month with Perl Date::Manip?

I'm using Date::Manip for a variety of things, and want to create an array of days of the month.
I think I need:
#date = &ParseRecur("2010:4:0:0:0:0:0");
but, it doesn't do it.
I've read & reread the man page but can't get the syntax.
#date = &ParseRecur("2010:4:0:1:0:0:0");
#date = &ParseRecur("2010:4:0:1*:0:0:0");
don't work either!
You could build the list with your own loop, instead of using ParseRecur.
$month = 4;
for ($day = 1; $day <= 31; $day++) {
my $date = UnixDate( "$month/$day/2010", "%m-%d-%Y" );
push( #list, $date ) if (defined $date);
}
From the man pages:
"There are a small handful of English strings which can be parsed in place of a numerical recur description."
Check out the examples in the man page.
So, if you want an array of days of a month - say for June in 2010 you would do:
#dates = ParseRecur("every day in June 2010");