Adding Seconds to UNIX/Epoch-Time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding Seconds to UNIX/Epoch-Time
# 1  
Old 05-17-2013
Adding Seconds to UNIX/Epoch-Time

Hello All,

I have a Perl script I'm writing where I ask the user to enter a "start time" for something. The "$start_time" will be in the format of:
Code:
# The Time CLI Option Can be in the format of:
    --start-time="1day"
    --start-time="2hours"
    --start-time="45min"
    --start-time="50sec"

So what I do is pull out the number from the option supplied, convert it to seconds, then add those seconds to the current
"UNIX Time" which is calculated using --> my "$current_time = timelocal($sec,$min,$hour,$mday,$mon,$year);".

So if the following is the Current Time and the option supplied by the user:
Code:
> ./my_script --start-time="1day"

### Calculate the Current Time in UNIX-Time format
#         current_time = "1368808927"
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
my $current_time = timelocal($sec,$min,$hour,$mday,$mon,$year);

### Convert the User's $start_time to Seconds:
#         *Lets pretend I already captured the Arg and split out the "$time_number" == 1 and the "$time-unit" == day

### If the time_unit is days, then convert 'n' days to seconds
if ($time_unit eq "day")
{
        # total_seconds == 86400 seconds (*86400 sec in 1 day)
        my $total_seconds = (($time_number * 24) * 60) * 60
        #  (1368808927  +  86400) == 1368895327
        my $START_TIME = ($current_time + $total_seconds);
}

Now that I have the Future time in seconds (i.e. UNIX/Epoch Time) I should be able to take those seconds then convert it
to a human-readable format.

But after I convert the future UNIX-Time to a regular date format it is actually giving me the following:
Code:
Current Date/Time in UNIX and Standard Formats:
- If the Current UNIX-Time == 1368808927
- Current Standard-Time (using time-stamp above) == 05-17-2013 12:42:07 PM 

Calculated Date/Time in UNIX and Standard Formats:
- if the Future UNIX-Time == 1368895327
- Future Standard-Time (using from the CLI "date --date="1970-01-01 1368895327 sec" "+%m-%d-%Y %r") == 05-18-2013 05:42:07 PM

As you can see in the output from the "date" command it is showing the "future start time" as what looks like 29 hours or so into the future. Instead of being 24-Hours, which is how
many seconds I added to the UNIX-Time...

Does anyone know if I'm not doing this correctly? I figure my output, if the start_time = 24 hours, should actually be:
05-18-2013 12:42:07 PM


Maybe I need to specify a time zone, or something like that???
Any thoughts or suggestions would be greatly appreciated!

Thanks in Advance,
Matt

---------- Post updated at 01:58 PM ---------- Previous update was at 01:14 PM ----------

Humm.. I think this method will work for converting a future UNIX-Timestamp to a Readable Date format:
Code:
### $unix_start_time contains the UNIX-Timestamp of the "future" date/time 
#  Array contains -> sec[0] min[1] hour[2] mday[3] mon[4] year[5] wday[6] yday[7] isdst[8]
my (@my_time) = localtime($unix_start_time);

print "${BOLD}FUTURE TIME ==${OFF} $my_time[4]-$my_time[3]-$my_time[5] $my_time[2]:$my_time[1]";


____OUTPUT____

FUTURE TIME == 4-18-113 13:49

Not positive yet but it looks like that method works.
_________________

Getting the incorrect year though... I must be doing something wrong...



Thanks,
Matt

Last edited by mrm5102; 05-17-2013 at 04:30 PM..
# 2  
Old 05-18-2013
Use POSIX module and the strftime routine as shown below. To ensure that your perl code is portable, ensure that you use date formats that are ANSI C standard. %r is not one.

Code:
#! /usr/bin/perl

use warnings;
use strict;
use POSIX 'strftime';

my $current_time = time();

my $time_number = 1;
my $time_unit = "day";

my ($total_seconds, $START_TIME) = (0, 0);

if ($time_unit eq "day")
{
        # total_seconds == 86400 seconds (*86400 sec in 1 day)
        $total_seconds = $time_number * 86400; # Same as 24 * 60 * 60
        #  (1368808927  +  86400) == 1368895327
        $START_TIME = ($current_time + $total_seconds);
}

print "Future time: ", strftime("%m-%d-%Y %I:%M:%S %p", localtime($START_TIME)), "\n";

This User Gave Thanks to balajesuri 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

Convert a future date into epoch seconds on HPUX system

Hi All, I have scenario where i have to compare two dates. I thought of converting them to epoch seconds and do a numeric comparison. This works fine on Linux systems. $ date -d '2015/12/31' +%s 1451538000 $ date +%s 1449159121 But we don't have -d option in HPUX. What would be... (5 Replies)
Discussion started by: veeresh_15
5 Replies

2. Shell Programming and Scripting

Can anyone help me to print UNIX epoch time to days,hours,min,sec ?

I have unix epoch time 1441678454803, Can you please help me to print this time in below format ? DAY,HOUR,MIN,SEC Appreciate your help!!! Thanks, Prince (7 Replies)
Discussion started by: prince1987
7 Replies

3. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 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. Red Hat

How to check that a particular value is epoch seconds?

how to verify that the following is epoch time. that is if i have given some random value like 34600 , how can i know it is epoch seconds (5 Replies)
Discussion started by: ramsavi
5 Replies

6. Shell Programming and Scripting

Adding time to date time in UNIX shell scipting

I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as... (3 Replies)
Discussion started by: codehelp04
3 Replies

7. Shell Programming and Scripting

Date conversion from Standard/given format to seconds/epoch

I am trying get time difference of two dates in secs. Initially I want to convert a standard date format to epoch for two dates and then subtract the two epoch dates. Example : date -d "2007-09-01 17:30:40" '+%s' But this gives me below error date: illegal option -- d Usage: date OS: AIX... (6 Replies)
Discussion started by: bpaac
6 Replies

8. Shell Programming and Scripting

How to calculate unix epoch time with DC(Calculator)

Hi All, In our code we are using below code to calculate the UNIX Epoch Time from the time stamp present in the file. Can any one explain how the DC operates in below command and how we calculate the UNIX EPOCH time from this. Date incide the file is 20:26:51 Output we get is 1289943554... (3 Replies)
Discussion started by: mithunpatel
3 Replies

9. UNIX for Dummies Questions & Answers

Real time of a clock for every 60 seconds in unix

hi can any one guide me on how to display real time of a clock in unix for every 60 seconds (2 Replies)
Discussion started by: ramnadh_babu
2 Replies

10. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies
Login or Register to Ask a Question