我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用psycopg2.extras.DateTimeTZRange()。
def test_cast_timestamptz(self): from psycopg2.extras import DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000, 12, 31, 23, 59, 59, 999, tzinfo=FixedOffsetTimezone(600)) cur.execute("select tstzrange(%s, %s, '[]')", (ts1, ts2)) r = cur.fetchone()[0] self.assertTrue(isinstance(r, DateTimeTZRange)) self.assertTrue(not r.isempty) self.assertEqual(r.lower, ts1) self.assertEqual(r.upper, ts2) self.assertTrue(not r.lower_inf) self.assertTrue(not r.upper_inf) self.assertTrue(r.lower_inc) self.assertTrue(r.upper_inc)
def test_cast_timestamptz(self): from psycopg2.extras import DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000, 12, 31, 23, 59, 59, 999, tzinfo=FixedOffsetTimezone(600)) cur.execute("select tstzrange(%s, %s, '[]')", (ts1, ts2)) r = cur.fetchone()[0] self.assert_(isinstance(r, DateTimeTZRange)) self.assert_(not r.isempty) self.assertEqual(r.lower, ts1) self.assertEqual(r.upper, ts2) self.assert_(not r.lower_inf) self.assert_(not r.upper_inf) self.assert_(r.lower_inc) self.assert_(r.upper_inc)
def test_cast_timestamptz(self): from psycopg2.extras import DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() ts1 = datetime(2000,1,1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000,12,31,23,59,59,999, tzinfo=FixedOffsetTimezone(600)) cur.execute("select tstzrange(%s, %s, '[]')", (ts1, ts2)) r = cur.fetchone()[0] self.assert_(isinstance(r, DateTimeTZRange)) self.assert_(not r.isempty) self.assertEqual(r.lower, ts1) self.assertEqual(r.upper, ts2) self.assert_(not r.lower_inf) self.assert_(not r.upper_inf) self.assert_(r.lower_inc) self.assert_(r.upper_inc)
def test_cast_timestamptz(self): from psycopg2.extras import DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() ts1 = datetime(2000,1,1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000,12,31,23,59,59,999, tzinfo=FixedOffsetTimezone(600)) cur.execute("select tstzrange(%s, %s, '[]')", (ts1, ts2)) r = cur.fetchone()[0] self.assertTrue(isinstance(r, DateTimeTZRange)) self.assertTrue(not r.isempty) self.assertEqual(r.lower, ts1) self.assertEqual(r.upper, ts2) self.assertTrue(not r.lower_inf) self.assertTrue(not r.upper_inf) self.assertTrue(r.lower_inc) self.assertTrue(r.upper_inc)
def test_adapt_date_range(self): from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() d1 = date(2012, 0o1, 0o1) d2 = date(2012, 12, 31) r = DateRange(d1, d2) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, DateRange)) self.assertEqual(r1.lower, d1) self.assertEqual(r1.upper, d2) self.assertTrue(r1.lower_inc) self.assertTrue(not r1.upper_inc) r = DateTimeRange(empty=True) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, DateTimeRange)) self.assertTrue(r1.isempty) ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000, 12, 31, 23, 59, 59, 999, tzinfo=FixedOffsetTimezone(600)) r = DateTimeTZRange(ts1, ts2, '(]') cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, DateTimeTZRange)) self.assertEqual(r1.lower, ts1) self.assertEqual(r1.upper, ts2) self.assertTrue(not r1.lower_inc) self.assertTrue(r1.upper_inc)
def test_adapt_date_range(self): from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() d1 = date(2012, 01, 01) d2 = date(2012, 12, 31) r = DateRange(d1, d2) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, DateRange)) self.assertEqual(r1.lower, d1) self.assertEqual(r1.upper, d2) self.assert_(r1.lower_inc) self.assert_(not r1.upper_inc) r = DateTimeRange(empty=True) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, DateTimeRange)) self.assert_(r1.isempty) ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000, 12, 31, 23, 59, 59, 999, tzinfo=FixedOffsetTimezone(600)) r = DateTimeTZRange(ts1, ts2, '(]') cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, DateTimeTZRange)) self.assertEqual(r1.lower, ts1) self.assertEqual(r1.upper, ts2) self.assert_(not r1.lower_inc) self.assert_(r1.upper_inc)
def test_adapt_date_range(self): from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() d1 = date(2012, 01, 01) d2 = date(2012, 12, 31) r = DateRange(d1, d2) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, DateRange)) self.assertEqual(r1.lower, d1) self.assertEqual(r1.upper, d2) self.assert_(r1.lower_inc) self.assert_(not r1.upper_inc) r = DateTimeRange(empty=True) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, DateTimeRange)) self.assert_(r1.isempty) ts1 = datetime(2000,1,1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000,12,31,23,59,59,999, tzinfo=FixedOffsetTimezone(600)) r = DateTimeTZRange(ts1, ts2, '(]') cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, DateTimeTZRange)) self.assertEqual(r1.lower, ts1) self.assertEqual(r1.upper, ts2) self.assert_(not r1.lower_inc) self.assert_(r1.upper_inc)
def validate(self, data): if 'start' in data and 'end' in data: if data['start'] > data['end']: raise serializers.ValidationError('Start date must be after end date') if not self.instance: date_range = DateTimeTZRange(data['start'], data['end']) overlaps = EquipmentReservation.objects.filter( reservation__overlap=date_range, equipment_reserved=data['equipment_reserved']).count() if overlaps > 0: raise serializers.ValidationError( 'Equipment has already been reserved during this time period') return data
def save(self, *args, **kwargs): # Staff are automatically confirmed. if self.reserved_by.groups.filter(name='staff').exists(): self.is_confirmed = True self.confirmed_by = self.reserved_by # Take the start and end dates to generate a timerange # to actually query on as solves issues just using start # and end dates. self.reservation = DateTimeTZRange(self.start, self.end) super(EquipmentReservation, self).save(*args, **kwargs)
def make_clock(effective_lower: dt.datetime, vclock_lower: int, **kwargs) -> _ClockSet: """construct a clock set tuple""" effective_upper = kwargs.get('effective_upper', None) vclock_upper = kwargs.get('vclock_upper', None) effective = psql_extras.DateTimeTZRange( effective_lower, effective_upper) vclock = psql_extras.NumericRange(vclock_lower, vclock_upper) return _ClockSet(effective, vclock)
def effective_now() -> psql_extras.DateTimeTZRange: utc_now = dt.datetime.now(tz=dt.timezone.utc) return psql_extras.DateTimeTZRange(utc_now, None)
def test_adapt_date_range(self): from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() d1 = date(2012, 0o1, 0o1) d2 = date(2012, 12, 31) r = DateRange(d1, d2) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, DateRange)) self.assertEqual(r1.lower, d1) self.assertEqual(r1.upper, d2) self.assertTrue(r1.lower_inc) self.assertTrue(not r1.upper_inc) r = DateTimeRange(empty=True) cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, DateTimeRange)) self.assertTrue(r1.isempty) ts1 = datetime(2000,1,1, tzinfo=FixedOffsetTimezone(600)) ts2 = datetime(2000,12,31,23,59,59,999, tzinfo=FixedOffsetTimezone(600)) r = DateTimeTZRange(ts1, ts2, '(]') cur.execute("select %s", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, DateTimeTZRange)) self.assertEqual(r1.lower, ts1) self.assertEqual(r1.upper, ts2) self.assertTrue(not r1.lower_inc) self.assertTrue(r1.upper_inc)