Solved

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

Posted on 2009-05-07
9
327 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

16 Experts available now in Live!

Get 1:1 Help Now