Decimal representation of year¶
Convert between decimal year representation and gregorian date.
Note
Due to the nature of the gregorian calendar, the decimal year does not represent a uniform measure of time, because some years are longer than others.
- PyAstronomy.pyasl.decimalYear(date)¶
Calculates the decimal representation of a date, e.g., 2013.12.
The code uses Python’s datetime package to determine the fractional year. Thus, leap years are taken into account. There may still be issues with time zones or daylight saving times etc..
Code from: http://stackoverflow.com/questions/6451655/python-how-to-convert-datetime-dates-to-decimal-years
- Parameters
- datepython date instance
The input date (and time).
- Returns
- Decimal yearfloat
Decimal representation of the date.
- PyAstronomy.pyasl.decimalYearGregorianDate(date, form='datetime')¶
Convert decimal year into gregorian date.
Formally, the precision of the result is one microsecond.
- Parameters
- datefloat
The input date (and time).
- formstr, optional
Output format for the date. Either one of the following strings defining a format: “dd-mm-yyyy [hh:mm:ss]”, “yyyy-mm-dd [hh:mm:ss]”, where the term in braces is optional, or “tuple” or “datetime”. If ‘tuple’ is specified, the result will be a tuple holding (year, month, day, hour, minute, second, microseconds). In the case of “datetime” (default), the result will be a datetime object.
- Returns
- Gregorian datestr, tuple, or datetime instance
The gregorian representation of the input in the specified format. In case of an invalid format, None is returned.
Example:¶
from __future__ import print_function, division
import datetime as dt
from PyAstronomy import pyasl
# Convert July 2nd, 1998, 12:30:59 into decimal
# representation
d = dt.datetime(1998, 7, 2, 12, 30, 59)
# Obtain float representation of decimal year
decy = pyasl.decimalYear(d)
print("Decimal representation: ", decy)
# Convert back into gregorian date first
print("The decimal year %10.5f correspond to " % decy +
pyasl.decimalYearGregorianDate(decy, "yyyy-mm-dd hh:mm:ss"))
print(" ... or equivalently (y, m, d, h, m, s, ms): ",
pyasl.decimalYearGregorianDate(decy, "tuple"))