Issue with localtime function

Posted on 2006-06-05
Last Modified: 2008-02-26
I had written a perl script to use the localtime function to get the year, date, month, etc. I am a novice in perl so if there are better ways of doing this, bare with me. I'm working on a windows XP machine with Cygwin.

$date=(join'-',(split/ /,localtime)[1,2,4]);
$year = (join'-', (split/ /,localtime)[4]);
print "Date: $date\n";      # printed "Date: May-31-2006"
print "Year: $year\n";      # printed "Year: 2006"

This worked fine last week. But, today when I was runing the script it was not working. After a little debugging I had to change the above to below to get the same results:

$date=(join'-',(split/ /,localtime)[1,3,5]);
$year = (join'-', (split/ /,localtime)[5]);
print "Date: $date\n";      # printed "Date: Jun-5-2006"
print "Year: $year\n";      # printed "Year: 2006"

Why did this change? what am I missing?
Question by:nalinw1
    LVL 48

    Accepted Solution

    use POSIX 'strftime';
    print strftime("%b-%d-%Y\n",localtime);
    LVL 2

    Expert Comment

    you should read "perldoc -tf localtime" and "perldoc POSIX" to be sure that you will understand that in future
    LVL 84

    Expert Comment

    (split' ',localtime)
    may have been closer to what you intended originally.
    But using localtime in list contex instead of scalar context as the others have suggested is a better way to do it.
    And even if you use (split' ',localtime) it is not good practice to call localtime twice to get different parts of a date becase the date may change between calls, which could make your parts inconsistent.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    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…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  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…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 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

    19 Experts available now in Live!

    Get 1:1 Help Now