Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get this week's dates in Perl?

Tags:

datetime

perl

I have the following loop to calculate the dates of the current week and print them out. It works, but I am swimming in the amount of date/time possibilities in Perl and want to get your opinion on whether there is a better way. Here's the code I've written:

#!/usr/bin/env perl
use warnings;
use strict;

use DateTime;

# Calculate numeric value of today and the 
# target day (Monday = 1, Sunday = 7); the
# target, in this case, is Monday, since that's
# when I want the week to start
my $today_dt = DateTime->now;
my $today = $today_dt->day_of_week;
my $target = 1;

# Create DateTime copies to act as the "bookends"
# for the date range
my ($start, $end) = ($today_dt->clone(), $today_dt->clone());

if ($today == $target)
{
  # If today is the target, "start" is already set;
  # we simply need to set the end date
  $end->add( days => 6 );
}
else
{
  # Otherwise, we calculate the Monday preceeding today
  # and the Sunday following today
  my $delta = ($target - $today + 7) % 7;
  $start->add( days => $delta - 7 );
  $end->add( days => $delta - 1 );
}

# I clone the DateTime object again because, for some reason,
# I'm wary of using $start directly...
my $cur_date = $start->clone();

while ($cur_date <= $end)
{
  my $date_ymd = $cur_date->ymd;
  print "$date_ymd\n";
  $cur_date->add( days => 1 );
}

As mentioned, this works, but is it the quickest or most efficient? I'm guessing that quickness and efficiency may not necessarily go together, but your feedback is very appreciated.

like image 385
ABach Avatar asked May 26 '10 20:05

ABach


People also ask

How do I get the day of the week in Perl?

$year += 1900; To get the last two digits of the year (e.g., "01" in 2001) do: $year = sprintf("%02d", $year % 100); $wday is the day of the week, with 0 indicating Sunday and 3 indicating Wednesday.

How do I get today's date in Perl?

localtime() function in Perl returns the current date and time of the system, if called without passing any argument.

How do I get yesterday's date in Perl?

If you want yesterday's date: use DateTime qw( ); my $yday_date = DateTime ->now( time_zone => 'local' ) ->set_time_zone('floating') ->truncate( to => 'day' ) ->subtract( days => 1 ) ->strftime('%Y-%m-%d'); For example, in New York on 2019-05-14 at 01:00:00, it would have returned 2019-05-13.

What does $_ mean in Perl?

There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used.


2 Answers

A slightly improved version of friedo's answer ...

my $start_of_week =
    DateTime->today()
            ->truncate( to => 'week' );

for ( 0..6 ) {
    print $start_of_week->clone()->add( days => $_ );
}

However, this assumes that Monday is the first day of the week. For Sunday, start with ...

my $start_of_week =
    DateTime->today()
            ->truncate( to => 'week' )
            ->subtract( days => 1 );

Either way, it's better to use the truncate method than re-implement it, as friedo did ;)

like image 90
Dave Rolsky Avatar answered Oct 14 '22 08:10

Dave Rolsky


You can use the DateTime object to get the current day of the week as a number ( 1-7 ). Then just use that to find the current week's Monday. For example:

my $today = DateTime->now;
my $start = $today->clone;

# move $start to Monday
$start->subtract( days => ( $today->wday - 1 ) );   # Monday gives 1, so on monday we
                                                    # subtract zero. 

my $end = $start->clone->add( days => 7 );

The above is untested but the idea should work.

like image 24
friedo Avatar answered Oct 14 '22 07:10

friedo