Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Getting a date/time in a certain format in a Windows and Linux environment

by TASdvlper (Monk)
on Aug 16, 2004 at 20:24 UTC ( [id://383445]=perlquestion: print w/replies, xml ) Need Help??

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

All,

I'm trying to get the date/time in the following format: month/day/year hour:minute:second.

In a Linux Environment I did the following:

use Date::Manip my $format = "%m/%d/%y %H:%M:%S"; my $start = &UnixDate(&ParseDate("today"), $format);
This worked fine, but the problem is the Windows environment I am using is a very slimmed down version of Perl and I don't have the authority to install new modules. So, I'd prefer to use what's available.

So, I'm looking for alternatives that would work in both environments. I was looking for way to manipulate the output of localtime() into the above format, but I'm not having much luck.

Any help would be greatly appreciated.

  • Comment on Getting a date/time in a certain format in a Windows and Linux environment
  • Download Code

Replies are listed 'Best First'.
Re: Getting a date/time in a certain format in a Windows and Linux environment
by Joost (Canon) on Aug 16, 2004 at 20:40 UTC
      I did quickly try that and it complained that strftime was not defined (or something like that). I'll try it again though, maybe I did something wrong.
Re: Getting a date/time in a certain format in a Windows and Linux environment
by davorg (Chancellor) on Aug 16, 2004 at 22:23 UTC

    POSIX::strftime should do the job on just about any platform.

    .
    #!/usr/bin/perl use strict; use warnings; use POSIX 'strftime'; my $start = strftime '%m/%d/%y %H:%M:%S', localtime; print "$start\n";
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Getting a date/time in a certain format in a Windows and Linux environment
by Grygonos (Chaplain) on Aug 16, 2004 at 20:44 UTC
    Here you go. localtime will show you how I derived this solution. If the date parts didn't require manipulation you could make this a little more eloquent and use 2 joins and slices, then concatenate those two together. a join on / for the date and a join on : for the time... (which is doable but still requires modification of the approriate values prior to joining)

    #!/Perl/bin/perl use strict; use warnings; my @time = (localtime); my $formatted_time = ($time[4] + 1)."/".$time[3]."/".($time[5]+1900)." + ".$time[2].":".$time[1].":".$time[0]; print $formatted_time;

      A word of caution: those won't necessarily give you mm/dd/yyyy. You'll need to use sprintf or printf to make sure you get 2 digits in the month and day. Similar considerations for the time portion:

      my $formatted_time = sprintf "%02d/%02d/%04d %02d:%02d:%02d", $time[4] + 1, $time[3], $time[5] + 1900, $time[2], $time[1], $time[0];

        excellent point. I need to brush up on my use of sprintf

Re: Getting a date/time in a certain format in a Windows and Linux environment
by kvale (Monsignor) on Aug 16, 2004 at 20:28 UTC
    If you have the authority to write code but not import modules, just include the relevant portions of Date::Manip into your own code directly.

    -Mark

      Before you even think of doing this, check whether Date::Manip's license allows it.

      Cutting and pasting code into yours brings up copyright issues. A good rule of thumb before doing something like this is to ask yourself, If slashdot got proof that Microsoft did X, would people be baying for blood? If the answer is "Yes!", then you probably should think twice.

        By that token, you may never dare do anything in the first place… ;-)

        Makeshifts last the longest.

Re: Getting a date/time in a certain format in a Windows and Linux environment
by Kyoichi (Novice) on Aug 16, 2004 at 22:22 UTC
    For easy date/time manipulation you can go to CPAN and search for: - Class::Date - Time::Format or you can take a look at POSIX module ( strftime ). Have fun!
    -- Kyoichi

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found