7

I am trying to find the week that a date falls in, in a certain year. I have a bunch of files that need to be sorted into folders like "week1-2012" and "week34-2011". I tried searching but a lot of the results aren't really helping because I am currently using perl v5.6.1, super old and I can't download any modules. I also found this link ( How do I calculate the week number given a date?) of interest but was wondering how I would go about getting the day of year and week easily. Was thinking of getting the month, and adding the appropriate amount of days to find out the day in the year. Any help would be appreciated. An example of the year format I am looking for is

//year 2012
S  M  T  W  R  F  S
            1  2  3    <-- week #1 
4  5  6  7  8  9 10    <-- week #2 //came from the link

//dec year 2011
S   M  T  W  T  F  S
27 28 29 31            <-- week #52 or 53, not to sure the actual week
Community
  • 1
  • 1
imakeitrayne
  • 155
  • 1
  • 2
  • 11

3 Answers3

8

You can use core modules: POSIX and Time::Local

1.parse your date to (sec, min, hour, mday, month, year)

2.convert your date to seconds (epoch)

3.use function strftime to get week from current date

use strict;
use Time::Local;
use POSIX qw(strftime);

my $date = '08/15/2012';
my ($month, $day, $year) = split '/', $date;

my $epoch = timelocal( 0, 0, 0, $day, $month - 1, $year - 1900 );
my $week  = strftime( "%U", localtime( $epoch ) );

printf "Date: %s № Week: %s\n", $date, $week;

OUTPUT

Date: 08/15/2012 № Week: 33
Pavel Vlasov
  • 3,455
  • 21
  • 21
  • YES!!! you just solved my problem. I forgot to mention the reason why i couldnt install modules is because this work computer won't give it to me, and i need to carry this program to another pc that might not have the required modules. Thanks a ton! – imakeitrayne Aug 15 '12 at 20:05
6

Perl 5.6.1 dates from April 2001. Someone is making your life much harder than it needs to be by not giving you modern tools to use. I suggest it's worth spending some time fixing that problem.

If you had Perl 5.10 or greater, then it would include the Time::Piece module. And your problem would become trivial.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Time::Piece;

my $date = '08/15/2012';

my $dt = Time::Piece->strptime($date, '%m/%d/%Y');

say $dt->strftime('week%W-%Y');

Running it gives:

$ ./week 
week33-2012

This counts the week as Mon-Sun, not Sun-Sat.

Community
  • 1
  • 1
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
4

Here's an alternate solution, using DateTime:

use strict;
use warnings;
use DateTime;

my $dt=DateTime->now(time_zone=>"local");
print $dt->week_number . "\n";

The output is, of course:

33

Edit: Of course you can download modules! If nothing else, you can copy and use the relevant code from DateTime.

  • It appears i dont have datetime on this version of perl...but thanks for the help... – imakeitrayne Aug 15 '12 at 20:06
  • Again: if nothing else, you can copy and use the relevant code from `DateTime`. It's open source. That means you can actually **look at the source**. –  Aug 15 '12 at 20:21