Skip to content
Commits on Source (4)
......@@ -4,3 +4,4 @@ build/
*.egg-info
_build/
__pycache__
.pytest_cache/
......@@ -3,16 +3,15 @@ Performs conversions of netCDF time coordinate data to/from datetime objects.
"""
from cpython.object cimport PyObject_RichCompare
import cython
import numpy as np
import math
import numpy
import re
import time
from datetime import datetime as real_datetime
from datetime import timedelta, MINYEAR
import time # strftime
try:
from itertools import izip as zip
except ImportError: # python 3.x
......@@ -28,7 +27,7 @@ _units = microsec_units+millisec_units+sec_units+min_units+hr_units+day_units
_calendars = ['standard', 'gregorian', 'proleptic_gregorian',
'noleap', 'julian', 'all_leap', '365_day', '366_day', '360_day']
__version__ = '1.0.0b1'
__version__ = '1.0.0'
# Adapted from http://delete.me.uk/2005/03/iso8601.html
# Note: This regex ensures that all ISO8601 timezone formats are accepted - but, due to legacy support for other timestrings, not all incorrect formats can be rejected.
......@@ -1495,6 +1494,7 @@ _converters = {}
for calendar in _calendars:
_converters[calendar] = utime("seconds since 1-1-1", calendar)
@cython.embedsignature(True)
cdef class datetime(object):
"""
The base class implementing most methods of datetime classes that
......@@ -1533,12 +1533,17 @@ Gregorial calendar.
return '%Y-%m-%d %H:%M:%S'
def strftime(self, format=None):
"""
Return a string representing the date, controlled by an explicit format
string. For a complete list of formatting directives, see section
'strftime() and strptime() Behavior' in the base Python documentation.
"""
if format is None:
format = self.format
return _strftime(self, format)
def replace(self, **kwargs):
"Return datetime with new specified fields."
"""Return datetime with new specified fields."""
args = {"year": self.year,
"month": self.month,
"day": self.day,
......@@ -1555,8 +1560,15 @@ Gregorial calendar.
return self.__class__(**args)
def timetuple(self):
return (self.year, self.month, self.day, self.hour,
self.minute, self.second, self.dayofwk, self.dayofyr, -1)
"""
Return a time.struct_time such as returned by time.localtime().
The DST flag is -1. d.timetuple() is equivalent to
time.struct_time((d.year, d.month, d.day, d.hour, d.minute,
d.second, d.weekday(), yday, dst)), where yday is the
day number within the current year starting with 1 for January 1st.
"""
return time.struct_time((self.year, self.month, self.day, self.hour,
self.minute, self.second, self.dayofwk, self.dayofyr, -1))
cpdef _to_real_datetime(self):
return real_datetime(self.year, self.month, self.day,
......@@ -1564,7 +1576,7 @@ Gregorial calendar.
self.microsecond)
def __repr__(self):
return "{0}.{1}{2}".format(self.__class__.__module__,
return "{0}.{1}{2}".format('cftime',
self.__class__.__name__,
self._getstate())
......@@ -1660,6 +1672,7 @@ Gregorial calendar.
else:
return NotImplemented
@cython.embedsignature(True)
cdef class DatetimeNoLeap(datetime):
"""
Phony datetime object which mimics the python datetime object,
......@@ -1674,6 +1687,7 @@ but uses the "noleap" ("365_day") calendar.
cdef _add_timedelta(self, delta):
return DatetimeNoLeap(*add_timedelta(self, delta, no_leap, False))
@cython.embedsignature(True)
cdef class DatetimeAllLeap(datetime):
"""
Phony datetime object which mimics the python datetime object,
......@@ -1688,6 +1702,7 @@ but uses the "all_leap" ("366_day") calendar.
cdef _add_timedelta(self, delta):
return DatetimeAllLeap(*add_timedelta(self, delta, all_leap, False))
@cython.embedsignature(True)
cdef class Datetime360Day(datetime):
"""
Phony datetime object which mimics the python datetime object,
......@@ -1702,6 +1717,7 @@ but uses the "360_day" calendar.
cdef _add_timedelta(self, delta):
return Datetime360Day(*add_timedelta_360_day(self, delta))
@cython.embedsignature(True)
cdef class DatetimeJulian(datetime):
"""
Phony datetime object which mimics the python datetime object,
......@@ -1716,6 +1732,7 @@ but uses the "julian" calendar.
cdef _add_timedelta(self, delta):
return DatetimeJulian(*add_timedelta(self, delta, is_leap_julian, False))
@cython.embedsignature(True)
cdef class DatetimeGregorian(datetime):
"""
Phony datetime object which mimics the python datetime object,
......@@ -1744,6 +1761,7 @@ a datetime.datetime instance or vice versa.
cdef _add_timedelta(self, delta):
return DatetimeGregorian(*add_timedelta(self, delta, is_leap_gregorian, True))
@cython.embedsignature(True)
cdef class DatetimeProlepticGregorian(datetime):
"""
Phony datetime object which mimics the python datetime object,
......
cftime (1.0.0~b1-2) UNRELEASED; urgency=medium
cftime (1.0.0-1) unstable; urgency=medium
* New upstream release.
* Drop ancient X-Python-version field.
* Strip trailing whitespace from control & rules.
-- Bas Couwenberg <sebastic@debian.org> Sun, 06 May 2018 08:56:48 +0200
-- Bas Couwenberg <sebastic@debian.org> Thu, 17 May 2018 07:06:15 +0200
cftime (1.0.0~b1-1) unstable; urgency=medium
......
API
===
.. automodule:: cftime
:members: datetime, date2num, date2index, num2date, JulianDayFromDate, DatetimeJulian, DatetimeProlepticGregorian, DatetimeNoLeap, DatetimeAllLeap, DatetimeGregorian
:show-inheritance:
:noindex:
\ No newline at end of file
......@@ -4,16 +4,17 @@ cftime
Python library for decoding time units and variable values in a netCDF file
conforming to the Climate and Forecasting (CF) netCDF conventions.
Contents
--------
.. toctree::
:maxdepth: 2
:caption: Contents:
.. automodule:: cftime
:members: datetime, date2num, date2index, num2date, JulianDayFromDate, DatetimeJulian, DatetimeProlepticGregorian, DatetimeNoLeap, DatetimeAllLeap, DatetimeGregorian
installing
api
Indices and tables
==================
------------------
* :ref:`genindex`
* :ref:`modindex`
......
Installation
============
Required dependencies
---------------------
- Python 2.7, 3.4, 3.5, or 3.6
- `numpy <http://www.numpy.org/>`__ (1.7 or later)
Instructions
------------
The easiest way to get everything installed is to use conda_ command line tool::
$ conda install cftime
.. _conda: http://conda.io/
We recommend using the community maintained `conda-forge <https://conda-forge.github.io/>`__ channel if you need difficult\-to\-build dependencies such as cartopy or pynio::
$ conda install -c conda-forge cftime
New releases may also appear in conda-forge before being updated in the default
channel.
If you don't use conda, be sure you have the required dependencies (numpy and
cython) installed first. Then, install cftime with pip::
$ pip install cftime
Developing
----------
When developing we recommend cloning the GitHub repository,
building the extension in-place with `cython <http://cython.org/>`__ 0.19 or later
``python setup.py build_ext --inplace``
and running the test suite to check if the changes are passing the tests
``pytest --pyargs test``
......@@ -1230,5 +1230,13 @@ def test_num2date_only_use_cftime_datetimes_post_gregorian(
assert isinstance(result, expected_date_type)
def test_repr():
expected = 'cftime.datetime(2000, 1, 1, 0, 0, 0, 0, -1, 1)'
assert repr(datetimex(2000, 1, 1)) == expected
expected = 'cftime.DatetimeGregorian(2000, 1, 1, 0, 0, 0, 0, -1, 1)'
assert repr(DatetimeGregorian(2000, 1, 1)) == expected
if __name__ == '__main__':
unittest.main()