def __convert__(self, value): if isinstance(value, py_date): return value elif isinstance(value, py_datetime): return py_date(value.year, value.month, value.day) elif hasattr(value, "year") and hasattr(value, "month") \ and hasattr(value, "day"): if type(value.year) == MethodType: return py_date(value.year(), value.month(), value.day()) else: return py_date(value.year, value.month, value.day) else: raise ValueError("This dbattribute may only be set to "+\ "datetime.date instances, not %s!" % \ repr(value))
def test_from_ad_datetime(self): ad_dt = py_datetime(2020, 1, 13, 11, 23, 42, 123987) ne_dt = core.datetime.date.from_ad_datetime(ad_dt) self.assertEqual(ne_dt, core.datetime.date(2076, 9, 28)) looped_dt = ne_dt.to_ad_date() self.assertEqual(looped_dt, py_date(2020, 1, 13)) self.assertIsNone(core.datetime.date.from_ad_datetime(None))
def __convert__(self, value): if isinstance(value, py_date): return value elif isinstance(value, py_datetime): return py_date(value.year, value.month, value.day) elif hasattr(value, "year") and hasattr(value, "month") \ and hasattr(value, "day"): if type(value.year) == MethodType: return py_date(value.year(), value.month(), value.day()) else: return py_date(value.year, value.month, value.day) else: raise ValueError("This dbattribute may only be set to "+\ "datetime.date instances, not %s!" % \ repr(value))
def to_date(date_as_string): months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] day = int(date_as_string[0:2]) month = date_as_string[2:5] month_number = months.index(month) year = int(date_as_string[5:9]) return py_date(year, month_number, day)
def test_from_ad_date(self): ad_dt = py_date(2020, 1, 13) ne_dt = core.datetime.date.from_ad_date(ad_dt) self.assertEqual(ne_dt, core.datetime.date(2076, 9, 28)) looped_dt = ne_dt.to_ad_date() self.assertEqual(ad_dt, looped_dt) self.assertIsNone(core.datetime.date.from_ad_date(None)) #it also work if you give a datetime instead of a date... ad_dt = py_datetime(2020, 1, 13) ne_dt = core.datetime.date.from_ad_date(ad_dt) self.assertEqual(ne_dt, core.datetime.date(2076, 9, 28)) looped_dt = ne_dt.to_ad_datetime() self.assertEqual(ad_dt, looped_dt)
class BoundedDate(Control): """ A base class for components which edit a Python datetime.date object bounded between minimum and maximum values. This class is not meant to be used directly. """ #: The minimum date available in the date edit. If not defined then #: the default value is September 14, 1752. minimum = Property(Date, depends_on ='_minimum') #: The internal minimum date storage _minimum = Date(py_date(1752, 9, 14)) #: The maximum date available in the date edit. If not defined then #: the default value is December 31, 7999. maximum = Property(Date, depends_on ='_maximum') #: The internal maximum date storage _maximum = Date(py_date(7999, 12, 31)) #: The currently selected date. Default is the current date. The #: value is bounded between :attr:`minimum` and :attr:`maximum`. date = Bounded(Date(py_date.today()), low='minimum', high='maximum') #-------------------------------------------------------------------------- # Initialization #-------------------------------------------------------------------------- def snapshot(self): """ Return a dictionary which contains all the state necessary to initialize a client widget. """ snap = super(BoundedDate, self).snapshot() snap['minimum'] = self.minimum.isoformat() snap['maximum'] = self.maximum.isoformat() snap['date'] = self.date.isoformat() return snap def bind(self): """ A method called after initialization which allows the widget to bind any event handlers necessary. """ super(BoundedDate, self).bind() otc = self.on_trait_change otc(self._send_minimum, 'minimum') otc(self._send_maximum, 'maximum') otc(self._send_date, 'date') #-------------------------------------------------------------------------- # Message Handling #-------------------------------------------------------------------------- def on_action_date_changed(self, content): """ Handle the 'date_changed' action from the UI control. """ date = parse_iso_dt(content['date']).date() self.set_guarded(date=date) def _send_minimum(self): """ Send the minimum date to the client widget. """ content = {'minimum': self.minimum.isoformat()} self.send_action('set_minimum', content) def _send_maximum(self): """ Send the maximum date to the client widget. """ content = {'maximum': self.maximum.isoformat()} self.send_action('set_maximum', content) def _send_date(self): """ Send the current date to the client widget. """ if 'date' not in self.loopback_guard: content = {'date': self.date.isoformat()} self.send_action('set_date', content) #-------------------------------------------------------------------------- # Property methods #-------------------------------------------------------------------------- def _get_minimum(self): """ The property getter for the minimum date. """ return self._minimum def _set_minimum(self, date): """ The property setter for the minimum date. If the new minimum is greater than the current maximum, then the maximum will be adjusted up. """ if date > self._maximum: self._maximum = date self._minimum = date def _get_maximum(self): """ The property getter for the maximum date. """ return self._maximum def _set_maximum(self, date): """ The property setter for the maximum date. If the new maximum is less than the current minimum, then the minimum will be ajusted down. """ if date < self._minimum: self._minimum = date self._maximum = date #-------------------------------------------------------------------------- # Private API #-------------------------------------------------------------------------- @on_trait_change('minimum, maximum') def _adapt_date(self): """ Actively adapt the date to lie within the boundaries. """ self.date = min(max(self.date, self.minimum), self.maximum)
def test_ad_date(self): py_dt = py_date(2020, 1, 13) ad_dt = core.datetime.date(2020, 1, 13) self.assertEqual(py_dt, ad_dt)
def test_from_ad_date(self): dt = core.datetime.date.from_ad_date(py_date(2020, 1, 13)) self.assertEqual(dt, core.datetime.date(2076, 9, 28))
def test_to_ad_date(self): ne_dt = core.datetime.date(2076, 9, 28) ad_dt = ne_dt.to_ad_date() py_dt = py_date(2020, 1, 13) self.assertEqual(ad_dt, py_dt)
def test_min_max_date(self): self.assertEqual(core.datetime.date.min, core.datetime.date.from_ad_date(py_date(1, 1, 1))) self.assertEqual(core.datetime.date.max, core.datetime.date.from_ad_date(py_date(9999, 12, 31)))