def __convert__(self, value): if isinstance(value, py_datetime): return value elif hasattr(value, "year") and hasattr(value, "month") \ and hasattr(value, "day") and hasattr(value, "hour") \ and hasattr(value, "minute") and hasattr(value, "second"): if type(value.year) == MethodType: return py_datetime(value.year(), value.month(), value.day(), value.hour(), value.minute(), value.second()) else: second = int(value.second) fraction = value.second - float(second) milisecond = int(fraction * 1000) return py_datetime(value.year, value.month, value.day, value.hour, value.minute, second, milisecond) else: raise ValueError("This dbattribute may only be set to "+\ "datetime.datetime instances, not %s!" % \ repr(type(value)))
def test_from_ad_datetime(self): ad_dt = py_datetime(2020, 1, 13, 11, 23, 42, 123987) ne_dt = core.datetime.datetime.from_ad_datetime(ad_dt) self.assertEqual(ne_dt, core.datetime.datetime( 2076, 9, 28, 11, 23, 42, 123987)) looped_dt = ne_dt.to_ad_datetime() self.assertEqual(looped_dt, py_datetime( 2020, 1, 13, 11, 23, 42, 123987)) self.assertIsNone(core.datetime.datetime.from_ad_datetime(None))
def test(self): dob = py_datetime(1977, 7, 19) five_past_noon = py_timedelta(hours=12, minutes=5) dttm = py_datetime(1977, 7, 19, 12, 5) d = datetest(dt=dob, tm=five_past_noon, dttm=dttm) self.ds.insert(d) self.assert_("INSERT INTO datetest(tm, dttm, dt) VALUES ('12:05:00', '1977-07-19 12:05:00', '1977-07-19')" in sqllog.queries) result = self.ds.select(datetest) first = result.next() self.assert_(first.dt == dob) self.assert_(first.tm == five_past_noon) self.assert_(first.dttm == dttm)
def test(): dt = py_datetime(2012, 1, 2, 7, 56, 53) print('GMT: ', dt.strftime(fmt)) print('GMT to Local : ', GMT2Local(dt), type(GMT2Local(dt))) print('GMT: ', GMT(dt).strftime(fmt)) dt = GMT(dt) print('GMT to Local : ', GMT2Local(dt), type(GMT2Local(dt)))
def test(self): dob = py_datetime(1977, 7, 19) five_past_noon = py_timedelta(hours=12, minutes=5) dttm = py_datetime(1977, 7, 19, 12, 5) d = datetest(dt=dob, tm=five_past_noon, dttm=dttm) self.ds.insert(d) self.assertEqual(sqllog.queries[-2], "INSERT INTO datetest(tm, dttm, dt) VALUES ('12:05:00', '1977-07-19 12:05:00', '1977-07-19')") result = self.ds.select(datetest) first = result.next() self.assert_(first.dt == dob) self.assert_(first.tm == five_past_noon) self.assert_(first.dttm == dttm)
def find_corresponding_date(start_date): day = start_date.day month = start_date.month year = start_date.year next_month = month + 1 next_year = year if month == 12: next_month = 1 next_year = year + 1 try: new_date = py_datetime(year=next_year, month=next_month, day=day) except ValueError: next_month = next_month + 1 if next_month == 13: next_month = 1 next_year = next_year + 1 new_date = py_datetime(year=next_year, month=next_month, day=1) return new_date else: return new_date
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)
def resolve_module_configurations(self, info, **kwargs): validity = kwargs.get('validity') # configuration is loaded before even the core module # the datetime is ALWAYS a Gregorian one # (whatever datetime is used in business modules) if validity is None: validity = py_datetime.now() else: d = re.split('\D', validity) validity = py_datetime(*[int('0' + x) for x in d][:6]) # is_exposed indicates wherever a configuration # is safe to be accessible from api # DON'T EXPOSE (backend) configurations that contain credentials,... crits = (Q(is_disabled_until=None) | Q(is_disabled_until__lt=validity), Q(is_exposed=True)) layer = kwargs.get('layer') if layer is not None: crits = (*crits, Q(layer=layer)) return ModuleConfiguration.objects.prefetch_related('controls').filter( *crits)
class BoundedDatetime(Control): """ A base class for use with widgets that edit a Python datetime.datetime object bounded between minimum and maximum values. This class is not meant to be used directly. """ #: The minimum datetime available in the datetime edit. If not #: defined then the default value is midnight September 14, 1752. minimum = Property(Datetime, depends_on='_minimum') #: The internal minimum datetime storage _minimum = Datetime(py_datetime(1752, 9, 14, 0, 0, 0, 0)) #: The maximum datetime available in the datetime edit. If not #: defined then the default value is the second before midnight #: December 31, 7999. maximum = Property(Datetime, depends_on='_maximum') #: The internal maximum datetime storage _maximum = Datetime(py_datetime(7999, 12, 31, 23, 59, 59, 999000)) #: The currently selected date. Default is datetime.now(). The #: value is bounded between :attr:`minimum` and :attr:`maximum`. datetime = Bounded(Datetime(py_datetime.now()), low='minimum', high='maximum') #-------------------------------------------------------------------------- # Initialization #-------------------------------------------------------------------------- def snapshot(self): """ Return a dictionary which contains all the state necessary to initialize a client widget. """ snap = super(BoundedDatetime, self).snapshot() snap['minimum'] = self.minimum.isoformat() snap['maximum'] = self.maximum.isoformat() snap['datetime'] = self.datetime.isoformat() return snap def bind(self): """ A method called after initialization which allows the widget to bind any event handlers necessary. """ super(BoundedDatetime, self).bind() otc = self.on_trait_change otc(self._send_minimum, 'minimum') otc(self._send_maximum, 'maximum') otc(self._send_datetime, 'datetime') #-------------------------------------------------------------------------- # Message Handling #-------------------------------------------------------------------------- def on_action_datetime_changed(self, content): """ The handler for the 'datetime_changed' action sent from the client widget. """ datetime = parse_iso_dt(content['datetime']) self.set_guarded(datetime=datetime) def _send_minimum(self): """ Send the minimum datetime to the client widget. """ content = {'minimum': self.minimum.isoformat()} self.send_action('set_minimum', content) def _send_maximum(self): """ Send the maximum datetime to the client widget. """ content = {'maximum': self.maximum.isoformat()} self.send_action('set_maximum', content) def _send_datetime(self): """ Send the current datetime to the client widget. """ if 'datetime' not in self.loopback_guard: content = {'datetime': self.datetime.isoformat()} self.send_action('set_datetime', content) #-------------------------------------------------------------------------- # Properties #-------------------------------------------------------------------------- def _get_minimum(self): """ The property getter for the minimum datetime. """ return self._minimum def _set_minimum(self, datetime): """ The property setter for the minimum datetime. If the new minimum is greater than the current maximum, then the maximum will be adjusted up. """ if datetime > self._maximum: self._maximum = datetime self._minimum = datetime def _get_maximum(self): """ The property getter for the maximum datetime. """ return self._maximum def _set_maximum(self, datetime): """ The property setter for the maximum datetime. If the new maximum is less than the current minimum, then the minimum will be ajusted down. """ if datetime < self._minimum: self._minimum = datetime self._maximum = datetime #-------------------------------------------------------------------------- # Private API #-------------------------------------------------------------------------- @on_trait_change('minimum, maximum') def _adapt_datetime(self): """ Actively adapt the datetime to lie within the boundaries. """ self.datetime = min(max(self.datetime, self.minimum), self.maximum)
def test_to_ad_datetime(self): ad_dt = core.datetime.date(2020, 1, 13) db_dt = ad_dt.to_ad_datetime() self.assertEqual(db_dt, py_datetime(2020, 1, 13))
def test_from_ad_datetime(self): db_dt = core.datetime.date.from_ad_datetime(py_datetime(2020, 1, 13)) ad_dt = core.datetime.date(2020, 1, 13) self.assertEqual(db_dt, ad_dt) self.assertIsNone(core.datetime.date.from_ad_datetime(None))
def test_ad_datetime(self): py_dt = py_datetime(2020, 1, 13, 10, 9, 55, 728267) ad_dt = core.datetime.datetime(2020, 1, 13, 10, 9, 55, 728267) self.assertEqual(py_dt, ad_dt)
def test_min_max_date(self): self.assertEqual(core.datetime.datetime.min, core.datetime.datetime.from_ad_datetime(py_datetime(1, 1, 1))) self.assertEqual(core.datetime.datetime.max, core.datetime.datetime.from_ad_datetime( py_datetime(9999, 12, 31, 23, 59, 59, 999999)))
def test_to_ad_datetime(self): ne_dt = core.datetime.datetime(2076, 9, 28, 11, 22, 33, 444555) ad_dt = ne_dt.to_ad_datetime() py_dt = py_datetime(2020, 1, 13, 11, 22, 33, 444555) self.assertEqual(ad_dt, py_dt)
def test_to_ad_datetime(self): ne_dt = core.datetime.date(2076, 9, 28) ad_dt = ne_dt.to_ad_datetime() py_dt = py_datetime(2020, 1, 13, 0, 0, 0, 0) self.assertEqual(ad_dt, py_dt)
class BoundedDatetime(Control): """ A base class for use with widgets that edit a Python datetime.datetime object bounded between minimum and maximum values. This class is not meant to be used directly. """ #: The currently selected datetime. Default value is the current #: date and time in the machine. The value is bounded between #: :attr:`min_datetime` and :attr:`max_datetime`. Changing the #: boundary attributes might result in an update of :attr:`datetime` #: to fit in the new range. Attempts to assign a value outside of #: these bounds will result in a TraitError. datetime = Bounded(Datetime(py_datetime.now()), low='min_datetime', high='max_datetime') #: The minimum datetime available in the date edit. By default, this #: property contains a date that refers to September 14, 1752 and a #: time of 00:00:00 and 0 milliseconds. Extra checks take place to #: make sure that the user does not programmatically set #: :attr:`min_datetime` > :attr:`max_datetime`. min_datetime = Property(Datetime, depends_on='_min_datetime') #: The internal min datetime storage _min_datetime = Datetime(py_datetime(1752, 9, 14, 0, 0, 0, 0)) #: The maximum datetime available in the date edit. By default, this #: property contains a date that refers to 31 December, 7999 and a #: time of 23:59:59 and 999 milliseconds. Extra checks take place to #: make sure that the user does not programmatically set #: :attr:`min_datetime` > :attr:`max_datetime`. max_datetime = Property(Datetime, depends_on='_max_datetime') #: The internal max datetime storage _max_datetime = Datetime(py_datetime(7999, 12, 31, 23, 59, 59, 999000)) #: Overridden parent trait abstract_obj = Instance(AbstractTkBoundedDatetime) #-------------------------------------------------------------------------- # Properties methods #-------------------------------------------------------------------------- def _set_min_datetime(self, datetime): """ Set the min_datetime. Addtional checks are perfomed to make sure that :attr:`min_datetime` < :attr:`max_datetime` """ if datetime > self.max_datetime: msg = ("The minimum datetime of DatetimeEdit should be smaller " "than the current maximum datetime({0}), but a value of " "{1} was given ") msg = msg.format(self.max_datetime, datetime) raise TraitError(msg) self._min_datetime = datetime def _set_max_datetime(self, datetime): """ Set the max_datetime. Addtional checks are perfomed to make sure that :attr:`minimum_datetime` < :attr:`maximum_datetime` """ if datetime < self.min_datetime: msg = ("The maximum datetime of DatetimeEdit should be larger " "than the current minimum datetime({0}), but a value of " "{1} was given ") msg = msg.format(self.min_datetime, datetime) raise TraitError(msg) self._max_datetime = datetime def _get_max_datetime(self): """ The property getter for the max datetime. """ return self._max_datetime def _get_min_datetime(self): """ The property getter for the min datetime. """ return self._min_datetime @on_trait_change('min_datetime, max_datetime') def _adapt_datetime(self): """ Adapt the date to the bounderies """ if self.initialized: min_dt, max_dt = self.min_datetime, self.max_datetime self.datetime = min(max(self.datetime, min_dt), max_dt)