sreek3502 has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts, Can you please check below code to get the customized date and time output, Please see the expected output

#!/usr/bin/perl -w use POSIX; my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(ti +me); $year += 1900; print "$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst\n"; $now_string = localtime; print "$now_string\n"; $date = strftime "%a %b %e %H:%M:%S %Y", localtime; print "Date1 is : $date\n"; $date = strftime "%a-%B-%e", localtime; print "Date2 is : $date\n"; $time = strftime "%H:%M:%S", localtime; print "Time1 is : $time\n"; $time1 = strftime "%h:%m:%s", localtime; print "Time2 is : $time1\n";

#OUTPUT #Date1 is : 06-Oct-2017 #Date2 is : 06-10-2017 #Time1 is : 23:35:10 #Time2 is : 11:35:10 PM

Replies are listed 'Best First'.
Re: date and time using localtime
by haukex (Archbishop) on Oct 07, 2017 at 08:20 UTC

    I generally recommend using a module for all date/time operations, as it will take over a lot of the details and work for you. There is Time::Piece, a core module since Perl v5.10:

    use Time::Piece; my $t = localtime; print "Date1 is : ",$t->strftime("%d-%b-%Y"),"\n"; print "Date2 is : ",$t->strftime("%d-%m-%Y"),"\n"; print "Time1 is : ",$t->strftime("%H:%M:%S"),"\n"; print "Time2 is : ",$t->strftime("%I:%M:%S %p"),"\n"; __END__ Date1 is : 07-Okt-2017 Date2 is : 07-10-2017 Time1 is : 10:19:42 Time2 is : 10:19:42 # - OR - under the en_US locale: Date1 is : 07-Oct-2017 Date2 is : 07-10-2017 Time1 is : 10:19:42 Time2 is : 10:19:42 AM

    And my personal favorite is DateTime, since it gives you a lot of powerful tools, including proper time zone handling. In the following I'm letting the module determine the current local time zone, although I recommend specifying one explicitly if possible. Note that it defaults to the en_US locale unless you specify a different one to the object.

    use DateTime; my $dt = DateTime->now(time_zone=>'local'); print "Date1 is : ",$dt->strftime("%d-%b-%Y"),"\n"; print "Date2 is : ",$dt->strftime("%d-%m-%Y"),"\n"; print "Time1 is : ",$dt->strftime("%H:%M:%S %Z"),"\n"; print "Time2 is : ",$dt->strftime("%I:%M:%S %p %Z"),"\n"; __END__ Date1 is : 07-Oct-2017 Date2 is : 07-10-2017 Time1 is : 10:19:42 CEST Time2 is : 10:19:42 AM CEST

    In general, I strongly recommend you Use strict and warnings.

      Using one of the time related modules to load strftime offers no advantage over the OP's choice of POSIX for the code shown. Perhaps he needs other features of POSIX. I was unaware that the function could be called as a method. Many readers would consider that an advantage.
      Bill
        Using one of the time related modules to load strftime offers no advantage over the OP's choice of POSIX for the code shown.

        If we're only talking about the strftime "...", localtime bits of code, then yes, there isn't a big difference. But otherwise I disagree. In the OP's code, note how they add 1900 to the year, but don't change $mon, which is zero-based - an easy thing to overlook; both Time::Piece and DateTime return the month as the more natural 1-based. Also, in my experience, one date/time task seldom comes alone - if the OP decides they want to display the date in different locales, or different time zones, or do any kind of math, DateTime handles that easily and correctly.

        I was unaware that the function could be called as a method.

        Time::Piece overrides localtime and gmtime to return an object.

        Using one of the time related modules to load strftime offers no advantage over the OP's choice of POSIX for the code shown. Perhaps he needs other features of POSIX. I was unaware that the function could be called as a method. Many readers would consider that an advantage.

        What?

Re: date and time using localtime
by BillKSmith (Monsignor) on Oct 06, 2017 at 20:00 UTC
    The exact meaning of strftime formats can be complicated. The documentation for POSIX helps you to refer to the appropriate documentation for your case. Your examples are rather simple. The documentation for any C or C++ library would be good enough.
    #!/usr/bin/perl -w use strict; use warnings; use POSIX; my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(ti +me); $year += 1900; print "$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst\n"; my $now_string = localtime; print "$now_string\n"; #my $date = strftime "%a %b %e %H:%M:%S %Y", localtime; my $date = strftime "%d-%b-%Y", localtime; print "Date1 is : $date\n"; #$date = strftime "%a-%B-%e", localtime; $date = strftime "%d-%m-%Y", localtime; print "Date2 is : $date\n"; my $time = strftime "%H:%M:%S", localtime; print "Time1 is : $time\n"; #my $time1 = strftime "%h:%m:%s", localtime; my $time1 = strftime "%I:%M:%S", localtime; print "Time2 is : $time1\n"; OUTPUT: 58, 39, 15, 6, 9, 2017, 5, 278, 1 Fri Oct 6 15:39:58 2017 Date1 is : 06-Oct-2017 Date2 is : 06-10-2017 Time1 is : 15:39:58 Time2 is : 03:39:58
    Bill
Re: date and time using localtime
by thanos1983 (Parson) on Oct 09, 2017 at 09:06 UTC

    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!
      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!