Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Epoch seconds...need it in days 1

Status
Not open for further replies.

ljsmith91

Programmer
May 28, 2003
305
US
I have no date modules as this routine will run on too many systems to install on so I am limited.

I am trying to convert two days(examples 03/07/2008 and 07/18/2008) to epoch date so I can subtract them and get the difference in seconds. The time is not a part of this.
O know how to convert to epoch and then subtract and get the difference. However, I want to determine the differnce in days between the two dates. When I get the resulting epoch in seconds, I want to determine what that is in days.

Can someone tell me how to best do this?

Thanks so much.

ljs
 
just divide seconds answer by 86400?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
travs69,

I tried that and it comes out to a decimal value that needs to be rounded up to be accurate. Like 10.958333333333 instead of 11 days as an example. I have no idea why since my timestamp passed is 00:00:00 for both dates. Is there a way of rounding it up ? WHat would the cause of this be.

Thanks.

ljs
 
lets see how you are converting the dates to epoch time.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That difference of 0.41666666667 days corresponds to 3600 seconds... one hour.

Possible culprits:

The timestamps are either side of a daylight saving time boundary, in which case the calculation is correct.

The timestamps are collected from machines that have their clocks wrong by an hour (usually caused by either incorrect timezone or again, not catering for daylight saving time).
 
Hi,

The dates are pulled from a SQL database. Here is the conversion to epoch.

my ($mon_impl,$day_impl,$year_impl) = split('/', $record{Target_Impl_Date});
my $mon_impl = $mon_impl - 1;
my $year_impl = $year_impl - 1900;
$date_impl_epoch = timelocal(00,00,00,$day_impl,$mon_impl,$year_impl);

my ($mon_ops,$day_ops,$year_ops) = split('/', $record{Target_OPS_Date});
my $mon_ops = $mon_ops - 1;
my $year_ops = $year_ops - 1900;
$date_ops_epoch = timelocal(00,00,00,$day_ops,$mon_ops,$year_ops);

Is there a boo-boo ?

-ljs

 
Or is there a way to ROUND-UP the value to non-decimal integer? -ljs
 
The POSIX module allows you to use the ceil() function, which round up.

use POSIX;
my $delta_days = 10.958333333333
my $days = ceil($delta);

although... the 1 hour offset may end up going the other way, meaning you'd be off by a day. ($delta would be 11.416666667, $days would be 12). Also negative deltas would appear to be rounded the wrong way.

Probably better to :
my $days = int(0.5 + abs($delta_days))
 
You get the decimal value for days over 36 or 37, maybe 38, I don't remember which but you could do a quick test to determine where it happens. Just something to do with the way computers handle numbers in binary. Just use int() to get the number of seconds in whole days.

$diff = $date_ops_epoch - $date_impl_epoch;
print int ($diff/86400);

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top