1

I have a time of format 2013-04-29 08:17:58 and I need to convert it into seconds.

In UNIX, we can use date +%s. Is there anyway to do it in Solaris?

RobEarl
  • 7,862
  • 6
  • 35
  • 50
Asish Pati
  • 103
  • 2
  • 8

2 Answers2

3

Use Time::Piece. It has been part of the standard Perl 5 distribution since version 9.5 and shouldn't need installing.

use strict;
use warnings;
use 5.010;

use Time::Piece;

my $t = '2013-04-29 08:17:58';

$t = Time::Piece->strptime($t, '%Y-%m-%d %H:%M:%S');

say $t->epoch;

output

1367223478
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 1
    perhaps from cmd line, `perl -MTime::Piece -E 'say Time::Piece->strptime(pop,"%Y-%m-%d %H:%M:%S")->epoch' "2013-04-29 08:17:58"` – mpapec Sep 13 '13 at 10:05
  • Perl v5.10.0 required--this is only v5.8.4, stopped at abc.pl line 3. BEGIN failed--compilation aborted at abc.pl line 3. This is the error i am getting – Asish Pati Sep 13 '13 at 10:15
  • 3
    @AsishPati: Version 8.4 is *over nine years old*! You really should upgrade. For now you can install `Time::Piece` from CPAN and remove line 3 `use 5.010`. – Borodin Sep 13 '13 at 10:22
  • 2
    @AsishPati: Well you should make some noise about it! – Borodin Sep 13 '13 at 10:23
  • they will fire me after that... :P – Asish Pati Sep 13 '13 at 10:25
  • Versions 14 and earlier are *no longer supported*. – Borodin Sep 13 '13 at 10:29
  • isnt there any other way. without using perl – Asish Pati Sep 13 '13 at 10:30
  • If someone wanted me to use Perl 5.8.4, they wouldn't need to fire me - I'd resign on the spot :-) – Dave Cross Sep 13 '13 at 12:16
  • Well, isn't Solaris itself ancient and should be upgraded? :-) – Slaven Rezic Sep 13 '13 at 14:05
  • @Borodin: This is not true. Solaris ships perl 5.8.4 and of course they're supporting it, it's part of the OS. – Slaven Rezic Sep 13 '13 at 14:40
  • @SlavenRezic: Perl 5 version 16 is the oldest version that is officially supported *by the Perl 5 team*. You can always find someone who will support anything as long as you offer enough money, but there will be no bug fixes to version 8.4, and I doubt if Solaris will make any attempt to correct any problems found with it. It is *not* part of the OS - it is merely bundled with it. – Borodin Sep 13 '13 at 18:24
  • @AsishPati: There are many ways. Your question is tagged `Perl` so we (all) assumed you wanted a Perl solution. – Borodin Sep 13 '13 at 18:26
  • @Borodin: I have access to a Solaris 10 system, and the perl there lists 37 locally applied patches. So if there's a serious problem (e.g. a CVE), then Oracle will probably solve it. And `perldoc perlsolaris` says "... is supplied with the operating system". Remember that not everybody is regarding just the kernel as the OS. – Slaven Rezic Sep 13 '13 at 20:11
  • @SlavenRezic: I'm astonished. I would love to hear Oracle's reason for duplicating other people's effort in order to maintain a decade-old language out of step with anyone else's system. I would also point out that *"[Perl] is supplied with the operating system"* implies that Perl *isn't part of* the OS. – Borodin Sep 13 '13 at 21:40
  • @Borodin: what's unusual about an OS vendor to add patches to their packages? Everybody is doing this, Debian, Solaris, RedHat, FreeBSD... Just look at "perl -V" of the vendor perl, some of the vendors (e.g. Debian and Solaris) list the locally applied patches. – Slaven Rezic Sep 14 '13 at 07:31
  • @Borodin: Solaris 10 was released in January 2005, and perl 5.8.4 wasn't so outdated then. Solaris 11 (released in 2011) has perl 5.12, which is a bit better. – alanc Sep 14 '13 at 16:16
  • x=$(perl -e 'print int(time)') echo $x with this code i can print the present time in seconds. Do i have some way to convert my date to some format and convert it using such code? – Asish Pati Sep 18 '13 at 08:39
  • If you have a new question, then you should ask it as a new question. – Dave Cross Sep 18 '13 at 09:18
2

With a little more effort, you can do this with your horribly outdated version of Perl.

#!/usr/bin/perl

use strict;
use warnings;
use Time::Local;

my $str = '2013-04-29 08:17:58';
my @dt  = split /[- :]/, $str;

$dt[0] -= 1900;
$dt[1] -= 1;

print timelocal reverse @dt;

Time::Local has been included with Perl since the first release of Perl 5 (in 1994).

But please do what you can to get your ancient version of Perl updated.

Update: Getting a few downvotes on this. But no-one has bothered to explain why.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • x=$(perl -e 'print int(time)') echo $x with this code i can print the present time in seconds. Do i have some way to convert my date to some format and convert it using such code? – Asish Pati Sep 18 '13 at 08:39