Get Current Time in Seconds Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get Current Time in Seconds Perl
# 1  
Old 07-13-2010
Get Current Time in Seconds Perl

hi guys,

i need to know how to get the current date/time in seconds and i want to be able to do this in a one liner. like say for instance, if want to get what the time is right now, i'll issue a command like this:

Code:
## perl -e ' print scalar(localtime(time + 0)), "\n"'
Tue Jul 13 17:45:50 2010
##

but i dont want the above output. i want it to be in seconds. like 3494932222.

thanks guys
# 2  
Old 07-13-2010
Code:
$ perl -e ' print time(), "\n"'

The output was
Code:
1279043432

(seconds since "epoch", so I'm not sure what century you are living in with 3494932222 Smilie)
# 3  
Old 07-13-2010
Quote:
Originally Posted by scottn
Code:
$ perl -e ' print time(), "\n"'

The output was
Code:
1279043432

(seconds since "epoch", so I'm not sure what century you are living in with 3494932222 Smilie)

thank you so much. would you happen to know how i can do this if i were to get how many seconds there is between the seconds of the present time and the seconds of a future date like say June 15, 2011?
# 4  
Old 07-13-2010
Perl is certainly not my thing!

I do use this script (mainly for analysing RRD's which use epoch times), but it only accepts numerical values.

Code:
cat ToEpoch
#!/usr/bin/perl

my $day = @ARGV[0];
my $mon = @ARGV[1] -1;
my $year = @ARGV[2];

my $hour = @ARGV[3];
my $min = @ARGV[4];
my $sec = @ARGV[5];

use Time::Local;
my $time = timelocal($sec,$min,$hour,$day,$mon,$year);
print $time."\n";

$ ./ToEpoch 15 06 2011
1308088800

$ ./ToEpoch 15 6 2011 16 00 00
1308146400

This User Gave Thanks to Scott For This Post:
# 5  
Old 07-13-2010
Quote:
Originally Posted by SkySmart
... how i can do this if i were to get how many seconds there is between the seconds of the present time and the seconds of a future date like say June 15, 2011?
You could use the Time::Local core module for that -

Code:
$
$
$ perl -MTime::Local -le 'print "June 15, 2011 12:00 AM is ", timelocal(0,0,0,15,5,2011) - time, " seconds from right now."'
June 15, 2011 12:00 AM is 29065434 seconds from right now.
$
$

tyler_durden
These 2 Users Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting seconds to time

I have a list of time spans in seconds, and want to compute the time span as hh:mm:nn I am coding in bash and have coded the following. However, the results are wrong as "%.0f" rounds the values. Example: ftm: 25793.5 tmspan(hrs,min,sec): 7.16 429.89 25793.50 hh: 7 mm: 10 ss:... (2 Replies)
Discussion started by: kristinu
2 Replies

2. Shell Programming and Scripting

Time in seconds on AIX 4.3.2.0

Hi to everybody again i Need your help, i wasting hours but can't find a solutuin for my Problem. I am not an expert with AIX script programming. I have a csh script and i need the time in seconds but since i have an old AIX the Option -%s doesnot exist with the date command. I seach in Google... (13 Replies)
Discussion started by: Nadielosabra
13 Replies

3. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

4. Shell Programming and Scripting

Current triggered time to epoch seconds

I have a requirement to find long running instances for notifying the stake holders based on the triggered time in AIX. I am not sure how to convert the triggered time to epoch seconds. For example : Current triggered time of instance is 13:06:19 -> how to convert this into epoch in the... (5 Replies)
Discussion started by: chandu123
5 Replies

5. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

6. Shell Programming and Scripting

Time difference in seconds

date1=$(date +"%H:%M:%S") date2=$(date +"01:00:54") diff=$date2-$date1 echo $diff How to get the time difference in seconds. (4 Replies)
Discussion started by: sandy1028
4 Replies

7. Shell Programming and Scripting

Script to capture date/time in seconds in PERL... Cant understand errors

I'm Using this script to find the time of a file. I'm very much new to PERL and found this script posted by some one on this forum. It runs perfectly fine, just that it gives me following errors with the accurate output as well. I jus want the output to be stored in another file so that i can... (0 Replies)
Discussion started by: bankimmehta
0 Replies

8. UNIX for Dummies Questions & Answers

Current system date in terms of seconds

Hello Friends, I've been struggling with extreme nagios passive service checks. In order to trigger a nagios passive service im going to write an easy shell script like below and will run it in crontab. As im working on Solaris 10 servers i used "S" instead of lowercase "s" below #!/bin/sh... (2 Replies)
Discussion started by: EAGL€
2 Replies

9. UNIX for Advanced & Expert Users

Time Difference in seconds

It is required to calculate time difference in seconds between epoch time (19700101 00:00:00) and any given date time (e.g. 20010214 14:30:30). Is there any command in unix to get it? Thanks in adv. (1 Reply)
Discussion started by: k_bijitesh
1 Replies

10. UNIX for Dummies Questions & Answers

getting time in mili seconds

hi all UNIX Gurus, this is my first post...so i posting this with great expectations:o...hoping to get the similar replies... my question is.... need to get timestamp with millisecond in UNIX. Date command gives Year,month day, hour,minute and second but it does not give millisecond. Any... (5 Replies)
Discussion started by: Bhups
5 Replies
Login or Register to Ask a Question