Solved

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

Posted on 2009-05-07
9
348 Views
Last Modified: 2012-05-06
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
Comment
Question by:Angela_Wilcox
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 69

Accepted Solution

by:
Callandor earned 250 total points
ID: 24327239
Take a look at this: http://perldoc.perl.org/functions/localtime.html - $wday gives you the day of the week.
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 250 total points
ID: 24327578
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
 

Author Comment

by:Angela_Wilcox
ID: 24336018
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:Angela_Wilcox
ID: 24336370
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
 

Author Comment

by:Angela_Wilcox
ID: 24336434
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
 

Author Comment

by:Angela_Wilcox
ID: 24336617
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
 
LVL 84

Expert Comment

by:ozo
ID: 24336781
sprintf is unnecessary here

Also, I think
    local @_;
was meant to be
    local $_ = shift;
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24336875
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
 
LVL 84

Expert Comment

by:ozo
ID: 24337339
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now