A datetime trait type. Converts the passed date into a string format that can be used to construct a JavaScript datetime.
| 58 | |
| 59 | |
| 60 | class Date(TraitType): |
| 61 | |
| 62 | """ |
| 63 | A datetime trait type. |
| 64 | |
| 65 | Converts the passed date into a string format that can be used to |
| 66 | construct a JavaScript datetime. |
| 67 | """ |
| 68 | |
| 69 | def validate(self, obj, value): |
| 70 | try: |
| 71 | if isinstance(value, dt.datetime): |
| 72 | return value |
| 73 | if isinstance(value, dt.date): |
| 74 | return dt.datetime(value.year, value.month, value.day) |
| 75 | if np.issubdtype(np.dtype(value), np.datetime64): |
| 76 | # TODO: Fix this. Right now, we have to limit the precision |
| 77 | # of time to microseconds because np.datetime64.astype(datetime) |
| 78 | # returns date values only for precision <= 'us' |
| 79 | value_truncated = np.datetime64(value, 'us') |
| 80 | return value_truncated.astype(dt.datetime) |
| 81 | except Exception: |
| 82 | self.error(obj, value) |
| 83 | self.error(obj, value) |
| 84 | |
| 85 | def __init__(self, default_value=dt.datetime.today(), **kwargs): |
| 86 | super(Date, self).__init__(default_value=default_value, **kwargs) |
| 87 | self.tag(**date_serialization) |
| 88 | |
| 89 | |
| 90 | def convert_to_date(array, fmt='%m-%d-%Y'): |