Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: date and time using localtime

by thanos1983 (Parson)
on Oct 09, 2017 at 09:06 UTC ( [id://1200976]=note: print w/replies, xml ) Need Help??


in reply to date and time using localtime

Hello sreek3502,

Welcome to the Monastery. Fellow monks have answered your question, but I wanted to add my favorite formatting date module Date::Manip. I discovered the module fairly recent but I was blast by the capabilities. It may look a bit complicated at the beginning but more you experiment easier it becomes.

Having said that, I think you are looking for something like that?

#!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my $dateLocal = ParseDate('now'); # say $dateLocal; my $str = UnixDate($dateLocal,"It is now %T on %b %e, %Y."); say $str; my $str_second = UnixDate($dateLocal,"It is now %T on %B %e, %Y."); say $str_second; my $str_decimal = UnixDate($dateLocal,"It is now decimal %T on %B %d, +%Y."); say $str_decimal; my $deltastr = "12 hours ago"; my $date_past = DateCalc($dateLocal,$deltastr); # say $date; my $str_past = UnixDate($date_past,"It was 12 hours ago %T on %B %d, % +Y."); say $str_past; __END__ $ perl test.pl It is now 11:05:12 on Oct 9, 2017. It is now 11:05:12 on October 9, 2017. It is now decimal 11:05:12 on October 09, 2017. It was 12 hours ago 23:05:12 on October 08, 2017.

You can modify the output of the date by using any of the POSIX::strftime::GNU/FORMAT parameters.

Or an alternative to simplify it even more is to use the POSIX::strftime::GNU. Check out also the POSIX::strftime::GNU/FORMAT on how to play around with the stdout format based on your desire.

Sample of code:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use POSIX 'strftime'; use POSIX::strftime::GNU; say POSIX::strftime('%a, %d %b %Y %T %z', localtime); __END__ $ perl test.pl Mon, 09 Oct 2017 10:57:25 +0200

Depending on what you want to do, and what is easier for you to create / maintain you can choose to apply.

I hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: date and time using localtime
by hippo (Bishop) on Oct 09, 2017 at 13:06 UTC
    use POSIX 'strftime'; # ... say POSIX::strftime('%a, %d %b %Y %T %z', localtime);

    Now forgive me if that's just a result of a copy-and-paste on your part but it might just be that there's some misconception about the first line. As written it imports strftime() from the POSIX module into the enclosing scope (your script). This means that the function can then be called without prefixing it with the full module name as you have done. To see an already imported function called with the full module prefix looks wrong, even though it works. To my eyes it's like incorrect/inconsistent indentation but YMMV.

    Either import it and use the short form:

    use POSIX 'strftime'; # ... say strftime('%a, %d %b %Y %T %z', localtime);

    Or don't import it at all:

    use POSIX (); # ... say POSIX::strftime('%a, %d %b %Y %T %z', localtime);

    See also the CAVEATS section of the docs which points out that POSIX exports almost everything by default so you can even do:

    use POSIX; # ... say strftime('%a, %d %b %Y %T %z', localtime);

    but that is absolutely not recommended in the case of POSIX.

    HTH.

    (Edited to specify that the final example is bad for POSIX because it exports tons of symbols - the approach is generally OK for other, better-behaved modules)

      Hello hippo

      You are right, it is not needed to use both. To be honest I saw the example of the documentation POSIX::strftime::GNU which is using both so I used both but you are right it also works by importing only one.

      Thanks for the tip.

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1200976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-03-29 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found