diff options
author | Travis Oliphant <oliphant@enthought.com> | 2009-11-19 18:12:16 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2009-11-19 18:12:16 +0000 |
commit | 6ac92a83070a0f047b8dc990de31eac69c970ef3 (patch) | |
tree | 3eae9d824a93f748d210e5ad8626cb651300c55d | |
parent | b0e32f4064ecee2b66d787cb4b5a7bb49f4627c4 (diff) | |
download | numpy-6ac92a83070a0f047b8dc990de31eac69c970ef3.tar.gz |
Add function to get datetime information from a date-time dtype.
-rw-r--r-- | numpy/lib/type_check.py | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py index 4fc11bead..fb1470336 100644 --- a/numpy/lib/type_check.py +++ b/numpy/lib/type_check.py @@ -3,11 +3,12 @@ __all__ = ['iscomplexobj','isrealobj','imag','iscomplex', 'isreal','nan_to_num','real','real_if_close', 'typename','asfarray','mintypecode','asscalar', - 'common_type'] + 'common_type', 'datetime_data'] import numpy.core.numeric as _nx from numpy.core.numeric import asarray, asanyarray, array, isnan, \ obj2sctype, zeros +from numpy.core.multiarray import METADATA_DTSTR from ufunclike import isneginf, isposinf _typecodes_by_elsize = 'GDFgdfQqLlIiHhBb?' @@ -598,3 +599,34 @@ def common_type(*arrays): return array_type[1][precision] else: return array_type[0][precision] + +def datetime_data(dtype): + """Return (unit, numerator, denominator, events) from a datetime + """ + try: + import ctypes + except ImportError: + raise RuntimeError, "Cannot access date-time internals without ctypes installed." + + obj = dtype.metadata[METADATA_DTSTR] + class DATETIMEMETA(ctypes.Structure): + _fields_ = [('base', ctypes.c_int), + ('num', ctypes.c_int), + ('den', ctypes.c_int), + ('events', ctypes.c_int)] + + func = ctypes.pythonapi.PyCObject_AsVoidPtr + func.argtypes = [ctypes.py_object] + func.restype = ctypes.c_void_p + + result = func(ctypes.py_object(obj)) + result = ctypes.cast(ctypes.c_void_p(result), ctypes.POINTER(DATETIMEMETA)) + + struct = result[0] + base = struct.base + + # FIXME: This needs to be kept consistent with enum in ndarrayobject.h + _unitnum2name = ['Y', 'M', 'W', 'B', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', 'ps', 'fs', 'as'] + + return (_unitnum2name[base], struct.num, struct.den, struct.events) + |