Solved

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

Posted on 2009-05-07
9
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script - Remove row of data based on column value 3 77
Perl modules on linux ec2 3 131
Export Variables in Perl 3 123
Merging two files with Perl 5 57
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

734 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