Python django.utils.timezone.utc() Examples
The following are 30
code examples of django.utils.timezone.utc().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.utils.timezone
, or try the search function
.
Example #1
Source File: seed.py From figures with MIT License | 6 votes |
def seed_course_overviews(data=None): if not data: data = cans.COURSE_OVERVIEW_DATA # append with randomly generated course overviews to test pagination new_courses = [generate_course_overview(i, org='FOO') for i in xrange(20)] data += new_courses for rec in data: course_id = rec['id'] defaults = dict( display_name=rec['display_name'], org=rec['org'], display_org_with_default=rec['org'], number=rec['number'], created=as_datetime(rec['created']).replace(tzinfo=utc), enrollment_start=as_datetime(rec['enrollment_start']).replace(tzinfo=utc), enrollment_end=as_datetime(rec['enrollment_end']).replace(tzinfo=utc), ) if RELEASE_LINE != 'ginkgo': defaults['version'] = CourseOverview.VERSION CourseOverview.objects.update_or_create( id=as_course_key(course_id), defaults=defaults, )
Example #2
Source File: test_site_monthly_metrics.py From figures with MIT License | 6 votes |
def smm_test_data(db): """ Minimal test data for very simple test case """ site = SiteFactory() mock_today = datetime(year=2020, month=2, day=1, tzinfo=utc) last_month = mock_today - relativedelta(months=1) month_before = last_month - relativedelta(months=1) month_before_sm = [StudentModuleFactory(created=month_before, modified=month_before)] last_month_sm = [StudentModuleFactory(created=last_month, modified=last_month) for i in range(2)] return dict(site=site, mock_today=mock_today, last_month=last_month, month_before=month_before, last_month_sm=last_month_sm, month_before_sm=month_before_sm)
Example #3
Source File: seed.py From figures with MIT License | 6 votes |
def seed_course_completions(): """ go over the dates """ for co in CourseOverview.objects.all(): # Note there is a performance hit for using '?' qs = CourseEnrollment.objects.filter(course_id=co.id) # we just want a few of the enrollments to have completed # first cut, have 25% of learners complete course sample = int(qs.count() * 0.25) for ce in qs.order_by('?')[:sample]: GeneratedCertificate.objects.create( user=ce.user, course_id=co.id, created_date=as_datetime(FAKE.date_between( ce.created, LAST_DAY)).replace(tzinfo=utc), )
Example #4
Source File: test_enrollment_metrics.py From figures with MIT License | 6 votes |
def setup(self, db): self.site = SiteFactory() self.date_1 = datetime(2020, 2, 2, tzinfo=utc) self.date_2 = self.date_1 + relativedelta(months=1) # future of date_1 self.course_enrollment = CourseEnrollmentFactory() self.student_modules = [ StudentModuleFactory(student=self.course_enrollment.user, course_id=self.course_enrollment.course_id, modified=self.date_1), StudentModuleFactory(student=self.course_enrollment.user, course_id=self.course_enrollment.course_id, modified=self.date_2)] self.progress_data = dict(points_possible=100, points_earned=25, sections_worked=4, count=5)
Example #5
Source File: test_learner_course_grades.py From figures with MIT License | 6 votes |
def test_certificate_and_completion(self): ''' We're testing both certificate completion because currently, the only way to tell if a user ''' # We shouldn't have any certificates for this # course enrollment assert not self.lcg.certificates() assert not self.lcg.learner_completed() # Create a certificate record for this user expected_cert = GeneratedCertificateFactory( user=self.lcg.learner, course_id=self.lcg.course.id, created_date=datetime.datetime(2018, 6, 1, tzinfo=utc)) assert expected_cert check_certs = self.lcg.certificates() assert check_certs.count() == 1 assert check_certs[0] == expected_cert assert self.lcg.learner_completed()
Example #6
Source File: helpers.py From figures with MIT License | 6 votes |
def as_datetime(val): ''' TODO: Add arg flag to say if caller wants end of day, beginning of day or a particular time of day if the param is a datetime.date obj ''' if isinstance(val, datetime.datetime): return val elif isinstance(val, datetime.date): # Return the end of the day, set timezone to be UTC return datetime.datetime( year=val.year, month=val.month, day=val.day, ).replace(tzinfo=utc) elif isinstance(val, basestring): # noqa: F821 return dateutil_parse(val).replace(tzinfo=utc) else: raise TypeError( 'value of type "{}" cannot be converted to a datetime object'.format( type(val)))
Example #7
Source File: backfill.py From figures with MIT License | 6 votes |
def backfill_monthly_metrics_for_site(site, overwrite=False): """Backfill all historical site metrics for the specified site """ site_sm = get_student_modules_for_site(site) if not site_sm: return None first_created = site_sm.order_by('created').first().created start_month = datetime(year=first_created.year, month=first_created.month, day=1, tzinfo=utc) last_month = datetime.utcnow().replace(tzinfo=utc) - relativedelta(months=1) backfilled = [] for dt in rrule(freq=MONTHLY, dtstart=start_month, until=last_month): obj, created = fill_month(site=site, month_for=dt, student_modules=site_sm, overwrite=overwrite) backfilled.append(dict(obj=obj, created=created, dt=dt)) return backfilled
Example #8
Source File: operations.py From python-mysql-pool with MIT License | 6 votes |
def value_to_db_datetime(self, value): if value is None: return None # MySQL doesn't support tz-aware times if timezone.is_aware(value): if settings.USE_TZ: value = value.astimezone(timezone.utc).replace(tzinfo=None) else: raise ValueError( "MySQL backend does not support timezone-aware times." ) if not self.connection.features.supports_microsecond_precision: value = value.replace(microsecond=0) if not self.connection.use_pure: return datetime_to_mysql(value) return self.connection.converter.to_mysql(value)
Example #9
Source File: operations.py From plugin.video.netflix with MIT License | 6 votes |
def value_to_db_datetime(self, value): if value is None: return None # MySQL doesn't support tz-aware times if timezone.is_aware(value): if settings.USE_TZ: value = value.astimezone(timezone.utc).replace(tzinfo=None) else: raise ValueError( "MySQL backend does not support timezone-aware times." ) if not self.connection.features.supports_microsecond_precision: value = value.replace(microsecond=0) if not self.connection.use_pure: return datetime_to_mysql(value) return self.connection.converter.to_mysql(value)
Example #10
Source File: feedgenerator.py From bioforum with MIT License | 6 votes |
def latest_post_date(self): """ Return the latest item's pubdate or updateddate. If no items have either of these attributes this return the current UTC date/time. """ latest_date = None date_keys = ('updateddate', 'pubdate') for item in self.items: for date_key in date_keys: item_date = item.get(date_key) if item_date: if latest_date is None or item_date > latest_date: latest_date = item_date # datetime.now(tz=utc) is slower, as documented in django.utils.timezone.now return latest_date or datetime.datetime.utcnow().replace(tzinfo=utc)
Example #11
Source File: tests.py From jinja2-django-tags with MIT License | 6 votes |
def test_localize(self): env = Environment(extensions=[DjangoL10n]) template = env.from_string("{{ foo }}") context1 = {'foo': 1.23} date = datetime.datetime(2000, 10, 1, 14, 10, 12, tzinfo=timezone.utc) context2 = {'foo': date} translation.activate('en') self.assertEqual('1.23', template.render(context1)) translation.activate('de') self.assertEqual('1,23', template.render(context1)) translation.activate('es') timezone.activate('America/Argentina/Buenos_Aires') self.assertEqual('1 de Octubre de 2000 a las 11:10', template.render(context2)) timezone.activate('Europe/Berlin') self.assertEqual('1 de Octubre de 2000 a las 16:10', template.render(context2)) translation.activate('de') self.assertEqual('1. Oktober 2000 16:10', template.render(context2)) timezone.activate('America/Argentina/Buenos_Aires') self.assertEqual('1. Oktober 2000 11:10', template.render(context2))
Example #12
Source File: operations.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def value_to_db_datetime(self, value): """ Transform a datetime value to an object compatible with what is expected by the backend driver for datetime columns. If naive datetime is passed assumes that is in UTC. Normally Django models.DateTimeField makes sure that if USE_TZ is True passed datetime is timezone aware. """ if value is None: return None # cx_Oracle doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = value.astimezone(timezone.utc).replace(tzinfo=None) else: raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.") return Oracle_datetime.from_datetime(value)
Example #13
Source File: base.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def adapt_datetime_with_timezone_support(value, conv): # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL. if settings.USE_TZ: if timezone.is_naive(value): warnings.warn("MySQL received a naive datetime (%s)" " while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) value = value.astimezone(timezone.utc).replace(tzinfo=None) return Thing2Literal(value.strftime("%Y-%m-%d %H:%M:%S.%f"), conv) # MySQLdb-1.2.1 returns TIME columns as timedelta -- they are more like # timedelta in terms of actual behavior as they are signed and include days -- # and Django expects time, so we still need to override that. We also need to # add special handling for SafeText and SafeBytes as MySQLdb's type # checking is too tight to catch those (see Django ticket #6052). # Finally, MySQLdb always returns naive datetime objects. However, when # timezone support is active, Django expects timezone-aware datetime objects.
Example #14
Source File: sync_deleted_instances_fix.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def handle(self, *args, **kwargs): # Reset all sql deletes to None Instance.objects.exclude( deleted_at=None, xform__downloadable=True).update(deleted_at=None) # Get all mongo deletes query = '{"$and": [{"_deleted_at": {"$exists": true}}, ' \ '{"_deleted_at": {"$ne": null}}]}' query = json.loads(query) xform_instances = settings.MONGO_DB.instances cursor = xform_instances.find(query) for record in cursor: # update sql instance with deleted_at datetime from mongo try: i = Instance.objects.get( uuid=record["_uuid"], xform__downloadable=True) except Instance.DoesNotExist: continue else: deleted_at = parse_datetime(record["_deleted_at"]) if not timezone.is_aware(deleted_at): deleted_at = timezone.make_aware( deleted_at, timezone.utc) i.set_deleted(deleted_at)
Example #15
Source File: test_tasks.py From opencraft with GNU Affero General Public License v3.0 | 5 votes |
def setUp(self): super().setUp() self.now = timezone.datetime(2018, 8, 1, 7, 20, 12, tzinfo=timezone.utc) self.before_cutoff = self.now - timezone.timedelta(days=settings.LOG_DELETION_DAYS + 1) self.log_deletion = self.now + timezone.timedelta(days=1) # Some Django start-up tasks produce logs which interfere with our tests. LogEntry.objects.all().delete() # huey produces logs when running tasks, that interfere with calculations logging.getLogger('huey').disabled = True
Example #16
Source File: utils.py From hawkpost with MIT License | 5 votes |
def key_state(key, gpg): if not key: return None, "invalid", -1 results = gpg.import_keys(key).results # Key data is present in the last element of the list if not results or not results[-1]["fingerprint"]: return None, "invalid", -1 key_fingerprint = results[-1]["fingerprint"] # Since the keyring is exclusive for this import # only the imported key exists in it. key = gpg.list_keys()[0] exp_timestamp = int(key["expires"]) if key["expires"] else 0 expires = datetime.fromtimestamp(exp_timestamp, timezone.utc) to_expire = expires - timezone.now() days_to_expire = to_expire.days if key["trust"] == "r": state = "revoked" elif exp_timestamp and expires < timezone.now(): state = "expired" else: state = "valid" return key_fingerprint, state, days_to_expire
Example #17
Source File: __init__.py From bioforum with MIT License | 5 votes |
def _check_fix_default_value(self): """ Warn that using an actual date or datetime value is probably wrong; it's only evaluated on server startup. """ if not self.has_default(): return [] now = timezone.now() if not timezone.is_naive(now): now = timezone.make_naive(now, timezone.utc) value = self.default if isinstance(value, datetime.datetime): if not timezone.is_naive(value): value = timezone.make_naive(value, timezone.utc) value = value.date() elif isinstance(value, datetime.date): # Nothing to do, as dates don't have tz information pass else: # No explicit date / datetime value -- no checks necessary return [] offset = datetime.timedelta(days=1) lower = (now - offset).date() upper = (now + offset).date() if lower <= value <= upper: return [ checks.Warning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=self, id='fields.W161', ) ] return []
Example #18
Source File: utils.py From bioforum with MIT License | 5 votes |
def typecast_timestamp(s): # does NOT store time zone information # "2005-07-29 15:48:00.590358-05" # "2005-07-29 09:56:00-05" if not s: return None if ' ' not in s: return typecast_date(s) d, t = s.split() # Extract timezone information, if it exists. Currently it's ignored. if '-' in t: t, tz = t.split('-', 1) tz = '-' + tz elif '+' in t: t, tz = t.split('+', 1) tz = '+' + tz else: tz = '' dates = d.split('-') times = t.split(':') seconds = times[2] if '.' in seconds: # check whether seconds have a fractional part seconds, microseconds = seconds.split('.') else: microseconds = '0' tzinfo = utc if settings.USE_TZ else None return datetime.datetime( int(dates[0]), int(dates[1]), int(dates[2]), int(times[0]), int(times[1]), int(seconds), int((microseconds + '000000')[:6]), tzinfo ) ############################################### # Converters from Python to database (string) # ###############################################
Example #19
Source File: serializer.py From bioforum with MIT License | 5 votes |
def serialize(self): if self.value.tzinfo is not None and self.value.tzinfo != utc: self.value = self.value.astimezone(utc) value_repr = repr(self.value).replace("<UTC>", "utc") if isinstance(self.value, datetime_safe.datetime): value_repr = "datetime.%s" % value_repr imports = ["import datetime"] if self.value.tzinfo is not None: imports.append("from django.utils.timezone import utc") return value_repr, set(imports)
Example #20
Source File: test_clock.py From scale with Apache License 2.0 | 5 votes |
def test_check_schedule_hour_recover(self): """Tests checking a schedule to recover after being down for several hours.""" last = job_test_utils.create_clock_event(occurred=datetime.datetime(2015, 1, 1, 5, tzinfo=utc)) self.assertTrue(clock._check_schedule(datetime.timedelta(hours=1), last))
Example #21
Source File: tz.py From bioforum with MIT License | 5 votes |
def utc(value): """ Convert a datetime to UTC. """ return do_timezone(value, timezone.utc)
Example #22
Source File: test_clock.py From scale with Apache License 2.0 | 5 votes |
def test_check_schedule_hour_drift_min(self): """Tests checking a schedule for once an hour without slowly drifting away from the target time.""" last = job_test_utils.create_clock_event(occurred=datetime.datetime(2015, 1, 10, 8, tzinfo=utc)) self.assertTrue(clock._check_schedule(datetime.timedelta(hours=1), last))
Example #23
Source File: test_instance.py From kobo-predict with BSD 2-Clause "Simplified" License | 5 votes |
def test_json_assigns_attributes(self, mock_time): mock_time.return_value = datetime.utcnow().replace(tzinfo=utc) self._publish_transportation_form_and_submit_instance() xform_id_string = XForm.objects.all()[0].id_string instances = Instance.objects.all() for instance in instances: self.assertEqual(instance.json[SUBMISSION_TIME], mock_time.return_value.strftime(MONGO_STRFTIME)) self.assertEqual(instance.json[XFORM_ID_STRING], xform_id_string)
Example #24
Source File: test_command_syncd_deleted_instances_fix.py From kobo-predict with BSD 2-Clause "Simplified" License | 5 votes |
def test_command(self): count = Instance.objects.filter(deleted_at=None).count() self.assertTrue(count > 0) deleted_at = timezone.now() deleted_at = datetime.strptime( deleted_at.strftime("%Y-%m-%dT%H:%M:%S"), "%Y-%m-%dT%H:%M:%S") deleted_at = timezone.make_aware(deleted_at, timezone.utc) instance = Instance.objects.filter(deleted_at=None)[0] instance.deleted_at = deleted_at instance.save() # ensure mongo has deleted_at by calling remongo call_command('remongo') # reset deleted_at to None instance.deleted_at = None instance.save() same_instance = Instance.objects.get(pk=instance.pk) self.assertIsNone(same_instance.deleted_at) # reset the deleted_at time from datetime in mongo call_command('sync_deleted_instances_fix') same_instance = Instance.objects.get(pk=instance.pk) # deleted_at should now have a datetime value self.assertIsNotNone(same_instance.deleted_at) self.assertTrue(isinstance(same_instance.deleted_at, datetime)) self.assertEqual(same_instance.deleted_at, deleted_at)
Example #25
Source File: test_clock.py From scale with Apache License 2.0 | 5 votes |
def test_check_schedule_day_last(self): """Tests checking a daily schedule that was triggered before and is due now.""" last = job_test_utils.create_clock_event(occurred=datetime.datetime(2015, 1, 9, tzinfo=utc)) self.assertTrue(clock._check_schedule(datetime.timedelta(hours=24), last))
Example #26
Source File: seed.py From figures with MIT License | 5 votes |
def seed_users(data=None): if not data: data = cans.USER_DATA first_date = days_from(LAST_DAY, DAYS_BACK * -1) created_users = [] for rec in data: try: profile_rec = rec.get('profile', None) user = get_user_model().objects.create_user( username=rec['username'], password=rec['password'], email=rec['email'], ) user.is_staff = rec.get('is_staff', False) user.is_superuser = rec.get('is_superuser', False) user.date_joined = as_datetime( FAKE.date_between(first_date, LAST_DAY)).replace(tzinfo=utc) user.save() created_users.append(user) if profile_rec: UserProfile.objects.create( user=user, name=profile_rec['fullname'], gender=profile_rec.get('gender', None), country=profile_rec.get('country', None), ) except IntegrityError as e: print('skipping duplicate user email {}'.format(e)) return created_users
Example #27
Source File: fieldsight_logger_tools.py From kobo-predict with BSD 2-Clause "Simplified" License | 5 votes |
def save_submission(xform, xml, media_files, new_uuid, submitted_by, status, date_created_override, fxid, site, fs_poj_id=None, project=None): if not date_created_override: date_created_override = get_submission_date_from_xml(xml) instance = _get_instance(xml, new_uuid, submitted_by, status, xform, fxid, fs_poj_id, site, project) for f in media_files: Attachment.objects.get_or_create( instance=instance, media_file=f, mimetype=f.content_type) # override date created if required if date_created_override: if not timezone.is_aware(date_created_override): # default to utc? date_created_override = timezone.make_aware( date_created_override, timezone.utc) instance.date_created = date_created_override instance.save() if instance.xform is not None: print("creationg Parsed Instance") if fs_poj_id: fs_poj_id = str(fs_poj_id) pi, created = FieldSightParsedInstance.get_or_create(instance, update_data={'fs_uuid': fxid, 'fs_status': 0, 'fs_site':site, 'fs_project':project, 'fs_project_uuid':fs_poj_id}) if not created: print("not created FPI") pi.save(async=False) return instance
Example #28
Source File: logger_tools.py From kobo-predict with BSD 2-Clause "Simplified" License | 5 votes |
def save_submission(xform, xml, media_files, new_uuid, submitted_by, status, date_created_override): if not date_created_override: date_created_override = get_submission_date_from_xml(xml) instance = _get_instance(xml, new_uuid, submitted_by, status, xform) for f in media_files: Attachment.objects.get_or_create( instance=instance, media_file=f, mimetype=f.content_type) # override date created if required if date_created_override: if not timezone.is_aware(date_created_override): # default to utc? date_created_override = timezone.make_aware( date_created_override, timezone.utc) instance.date_created = date_created_override instance.save() if instance.xform is not None: pi, created = ParsedInstance.objects.get_or_create( instance=instance) if not created: pi.save(async=False) return instance
Example #29
Source File: test_utils.py From django-csp-reports with MIT License | 5 votes |
def test_aware(self): with self.settings(USE_TZ=True, TIME_ZONE='Europe/Prague'): # 00:05 in CEST is 22:05 day before in UTC mock_now = datetime(2016, 4, 26, 22, 5, tzinfo=timezone.utc) with patch('cspreports.utils.now', return_value=mock_now): self.assertEqual(get_midnight(), datetime(2016, 4, 26, 22, 0, tzinfo=timezone.utc))
Example #30
Source File: utils.py From rankedftw with GNU Affero General Public License v3.0 | 5 votes |
def utcnow(**kwargs): """ Get a timezone aware datetime, UTC now. """ return datetime.utcnow().replace(tzinfo=timezone.utc) + timedelta(**kwargs)