Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Convert an existing Perl Function that returns Epoch time to return Day of the Week

I have a function I created a couple of days with help from experts here:
sub epoch_time
{
    local @_;
    @d=/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
    return(sprintf(timelocal(@d[5,4,3,2],$d[1]-1,$d[0])));
}
Now I have the need to create another function to return only day of the week from my time stamp.  I have played with this all morning - I see scalar would give me day of the week as the first element - but this newbie cannot make her code work.  Any help to speed me on my way would be greatly appreciated.
0
Angela_Wilcox
Asked:
Angela_Wilcox
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
CallandorCommented:
Take a look at this: http://perldoc.perl.org/functions/localtime.html - $wday gives you the day of the week.
0
 
Adam314Commented:
You can use localtime to get the weekday as a number, and them convert that to a string, or use the strftime function:

use POSIX 'strftime';  #put at the top
 
sub epoch_time
{
    local @_;
    @d=/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
    return strftime('%A',timelocal(@d[5,4,3,2],$d[1]-1,$d[0]));
    #use lowercase '%a' to get abbreviated day
}

Open in new window

0
 
Angela_WilcoxAuthor Commented:
Thanks so much - I plugged it into my script and put the use string at the top - I get this error in the log:
Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at C:\Documents and Settings\W957226\Desktop\TESTAPP3_T.pl line 753, <$fh_input_log> chunk 2.

753 is the return line of this chunk of code.  I assume it is telling me there is an improper usage of this function but that is about as far as I get.  Thanks.  The entire code is part of a very large script of which I have chunked out just part of it for testing.  But I have attached it in case it is helpful...  The new function is at the very bottom - use statements at the very top, and the line of code where I am using it is line 456.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Angela_WilcoxAuthor Commented:
when I run it interactively from a command line I get:

M:\>d:\temp\TESTAPP3_T2.pl
Can't locate POSIX/strftime.pm in @INC (@INC contains: D:/Perl64/site/lib D:/Per
l64/lib .) at d:\temp\TESTAPP3_T2.pl line 11.
BEGIN failed--compilation aborted at d:\temp\TESTAPP3_T2.pl line 11.

M:\>
I thought - OK - I am missing the POSIX.pm module - but I was doing some readin and we have Perl 5.10 and it is supposed to be included... Lost in space now...
0
 
Angela_WilcoxAuthor Commented:
An finally - I did search our installation the the D:/Perl64... does contain the POSIC.pm so I guess that is not the issue.
0
 
Angela_WilcoxAuthor Commented:
sed on Callander receommendation to look at the module - I eventually wormed my way around to this solution:
sub dow_time
{
    local @_;
    @d=/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
    return(sprintf(scalar(localtime(timelocal(@d[5,4,3,2],$d[1]-1,$d[0])))));
}

When I create the final file I just get rid of the excess and keep the abbreviated day of the week which works for me... Get's me where I want to be anyway - thanks for everyone's help.
0
 
ozoCommented:
sprintf is unnecessary here

Also, I think
    local @_;
was meant to be
    local $_ = shift;
0
 
Adam314Commented:
The command should have been:


return strftime('%A',localtime(timelocal(@d[5,4,3,2],$d[1]-1,$d[0])));

Open in new window

0
 
ozoCommented:
instead of localtime(timelocal(@d[5,4,3,2],$d[1]-1,$d[0]))
it seems easier to do (@d[5,4,3,2],$d[1]-1,$d[0]-1900)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now