Correcting for Drift between Two Dates

W

W. eWatson

I have two dates, ts1, ts2 as below in the sample program. I know the clock
drift in seconds per day. I would like to calculate the actual date of ts2.
See my question at the end of the program.

# time differences with addition of drift
from datetime import datetime, timedelta
import time

drift = 4.23 # seconds per day

format = '%Y%m%d_%H%M%S'
ts1 = "20080901_120000" # base date-time
ts2 = "20080904_180000"
d1 = datetime(*(time.strptime(ts1, format)[0:6]))
d2 = datetime(*(time.strptime(ts2, format)[0:6]))
#d += timedelta(seconds=sec)
delta = d2-d1
# delta format is nnn[n] days, hh:mm:ss
# delta is type 'datetime.timedelta'
print delta
# get back to ts2 as a check
d3 = d1+d
print d3
#OK, now I need to add the total drift time between
# d1 and d2, to get the true date-time of d2.

# How do I get at the nnn and hh:mm:ss of delta so that
# I can change nnn to nnn+(fraction of day in hh:mm:ss) to
# days + fraction of day, D. I want to multiple D by drift
# to get seconds of drift in period, then add it to d2.

Results
3 days, 6:00:00
2016-09-04 18:00:00
--
W. eWatson

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>
 
S

Steven D'Aprano

I have two dates, ts1, ts2 as below in the sample program. I know the
clock drift in seconds per day. I would like to calculate the actual
date of ts2. See my question at the end of the program.


When faced with a complicated task, break it down into simpler subtasks.
Functions are your friends. Here you go:



from __future__ import division

from datetime import datetime as DT
from datetime import timedelta

SITE_DRIFT = 4.23 # drift in seconds per day
# negative drift means the clock falls slow
SEC_PER_DAY = 60*60*24 # number of seconds per day


def calc_drift(when, base, drift=SITE_DRIFT):
"""Return the amount of drift at date when since date base."""
x = when - base
days = x.days + x.seconds/SEC_PER_DAY
return drift*days

def fix_date(when, base, drift=SITE_DRIFT):
"""Return date when adjusted to the correct time."""
d = calc_drift(when, base, drift)
delta = timedelta(seconds=-d)
return when + delta


And here it is in action:
datetime.datetime(2008, 9, 8, 23, 59, 55, 770000)



I leave it to you to convert date/time strings into datetime objects.
 
W

W. eWatson

Steven said:
When faced with a complicated task, break it down into simpler subtasks.
Functions are your friends. Here you go:



from __future__ import division

from datetime import datetime as DT
from datetime import timedelta

SITE_DRIFT = 4.23 # drift in seconds per day
# negative drift means the clock falls slow
SEC_PER_DAY = 60*60*24 # number of seconds per day


def calc_drift(when, base, drift=SITE_DRIFT):
"""Return the amount of drift at date when since date base."""
x = when - base
days = x.days + x.seconds/SEC_PER_DAY
return drift*days

def fix_date(when, base, drift=SITE_DRIFT):
"""Return date when adjusted to the correct time."""
d = calc_drift(when, base, drift)
delta = timedelta(seconds=-d)
return when + delta


And here it is in action:

datetime.datetime(2008, 9, 8, 23, 59, 55, 770000)



I leave it to you to convert date/time strings into datetime objects.
Ah, ha. x.days and x.seconds.

--
W. eWatson

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top