Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Convert weekday to numerical date

by james28909 (Deacon)
on Apr 29, 2017 at 03:23 UTC ( [id://1189144]=perlquestion: print w/replies, xml ) Need Help??

james28909 has asked for the wisdom of the Perl Monks concerning the following question:

Hello again wise monks :)

I have a list of weekdays:

my @weekdays = qw( Today Yesterday Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday );

I need to format these weekdays into an absolute numerical date based off of scalar localtime. eg:

my ($local_weekday, $local_month, $local_numerical_date) = split (/\s+ +/, scalar localtime), 3; __END__ Fri Apr 28 'Yesterday' would be '27'

I need to use $local_weekday and $local_numerical_date (eg. 'fri 28') to format @weekdays from the list. eg. 'wednesday' to eg. '26'.

Also, i never need to check what the numerical date in the future is, it is always current date or earlier. I am pretty sure there is a way to loop while subtracting but I cant figure it out lol.

If I can be any more clear on anything please let me know. I have tried to explain it the best i can :)

EDIT: changed 'wednesday 27' to just '27' and added 'Yesterday' would be '27' to __END__

Replies are listed 'Best First'.
Re: Convert weekday to numerical date
by huck (Prior) on Apr 29, 2017 at 03:55 UTC

    Pretty simple.

    use strict; use warnings; my @weekdays = qw( Today Yesterday Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday ); my %lookup; my @backs; use Time::Local; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime( +time) ; # move to noon so DST changes dont kill ya my $now_fix12=timelocal( 0, 0, 12, $mday, $mon, $year); for my $back (0..6) { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtim +e(time-24*60*60*$back) ; push @backs,[$wday,$mday]; $lookup{$weekdays[$wday+2]}=$mday; } $lookup{'Today'}=$backs[0][1]; $lookup{'Yesterday'}=$backs[1][1]; for my $wday (@weekdays) { print $wday.' '.$lookup{$wday}."\n"; }

    Edit:code fixed to fit changed OP

Re: Convert weekday to numerical date
by choroba (Cardinal) on Apr 29, 2017 at 05:54 UTC
    Using Time::Piece and Time::Seconds:
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Time::Piece; use Time::Seconds; sub show { my ($date, $label) = @_; $label ||= $date->fullday; say join ' ', $label, $date->month, $date->mday; } my $today = localtime; show($today, 'Today'); my $yesterday = $today - ONE_DAY; show($yesterday, 'Yesterday'); for my $minus (0 .. 7) { my $prev = $today - $minus * ONE_DAY; show($prev); }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Convert weekday to numerical date
by kcott (Archbishop) on Apr 29, 2017 at 06:34 UTC

    G'day james28909,

    You can do this sort of thing fairly easily with Time::Piece and Time::Seconds.

    There were some points I wasn't too sure about from your spec:

    • Did you want the named days to be presented sequentially, or work from just the last week, which would mostly present two sequences. In the code below, I've assumed the latter.
    • Should Today and Yesterday be duplicated in @weekdays or are they separate dates. I've assumed the former.
    • Was the second Sunday an intended duplicate. I've assumed not. If you do want a second Sunday, probably subtracting Time::Seconds' ONE_WEEK constant from the first Sunday will do what you want.

    Here's code, showing the basic techniques. Bear in mind my stated assumptions. Note that the BEGIN block is just to get a point of reference before Time::Piece reimplements localtime. Also, in case the dates and times look a bit screwy, my timezone is currently UTC+10:00.

    #!/usr/bin/env perl -l use strict; use warnings; BEGIN { print scalar localtime } use Time::Piece; use Time::Seconds; my $t = localtime; my @weekdays = ($t->mday); my %lastweek = ($t->day => $t->mday); $t -= ONE_DAY; push @weekdays, $t->mday; $lastweek{$t->day} = $t->mday; for (1 .. 5) { $t -= ONE_DAY; $lastweek{$t->day} = $t->mday; } push @weekdays, @lastweek{qw{Sun Mon Tue Wed Thu Fri Sat}}; print "@weekdays";

    Output:

    Sat Apr 29 16:20:18 2017 29 28 23 24 25 26 27 28 29

    Had you also wanted the second Sunday, the output would have been:

    29 28 23 24 25 26 27 28 29 16

    If you had wanted the eight days, with two Sundays, to be sequential, the output would have been:

    29 28 16 17 18 19 20 21 22 23

    I'm guessing you can probably work it out from here; however, if you need further help, please provide expected output.

    — Ken

Re: Convert weekday to numerical date
by james28909 (Deacon) on May 02, 2017 at 01:36 UTC
    Just want to thank you guys/gals. As always, you come through with plenty of perlmonks wisdom :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1189144]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found