Pyephem - Time difference between datetime.datetime and ephem.Date

2k views Asked by At

Trying to convert between standard python datetime routine datetime.datetime and Pyphem routine ephem.Date sometimes there are (except the expected truncation) peculiar differences in the range of one second.

Try the example code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ephem
import datetime

now_datetime = datetime.datetime.now()
now_ephem = ephem.Date(now_datetime)
print "Datetime: ", now_datetime
print "Ephem   : ", now_ephem

Repeated calls give an output similar to:

thl@thl-lap-001:$ ./timedifference.py
Datetime:  2013-12-20 08:28:11.536814
Ephem   :  2013/12/20 08:28:11
thl@thl-lap-001:$ ./timedifference.py
Datetime:  2013-12-20 08:28:16.088484
Ephem   :  2013/12/20 08:28:15

The last two lines show 16.somethig seconds in Datetim that converts to 15 Seconds to ephem.Date.

Is there an explanation?

1

There are 1 answers

1
mattexx On

From the PyEphem home page:

PyEphem uses a simple floating point number to represent the date and time inside of its astronomy routines.

Floating-point precision always brings some level of approximation. I think that is what you are seeing in your example.

EDIT

Upon more digging, the reason it doesn't catch tenths of seconds in this case is that ephem ignores the microseconds when creating a ephem.Date from a datetime. Looks like there is a fix for this coming in version 3.7.5.2

To answer the question

Isn't python double precision float precise enough to catch at least tenths of seconds in that case?

Most definitely!

import ephem
'%f' % ephem.Date('2013/12/20 12:34:56')
=> '41627.024259'
'%f' % ephem.Date('2013/12/20 12:34:56.1')
=> '41627.024260'