Python django.utils.datetime_safe.datetime() Examples
The following are 30
code examples of django.utils.datetime_safe.datetime().
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.datetime_safe
, or try the search function
.
Example #1
Source File: test_datetime_safe.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_compare_datetimes(self): self.assertEqual(original_datetime(*self.more_recent), datetime(*self.more_recent)) self.assertEqual(original_datetime(*self.really_old), datetime(*self.really_old)) self.assertEqual(original_date(*self.more_recent), date(*self.more_recent)) self.assertEqual(original_date(*self.really_old), date(*self.really_old)) self.assertEqual( original_date(*self.just_safe).strftime('%Y-%m-%d'), date(*self.just_safe).strftime('%Y-%m-%d') ) self.assertEqual( original_datetime(*self.just_safe).strftime('%Y-%m-%d'), datetime(*self.just_safe).strftime('%Y-%m-%d') ) self.assertEqual( original_time(*self.just_time).strftime('%H:%M:%S'), time(*self.just_time).strftime('%H:%M:%S') )
Example #2
Source File: test_writer.py From django-sqlserver with MIT License | 6 votes |
def test_migration_file_header_comments(self): """ Test comments at top of file. """ migration = type(str("Migration"), (migrations.Migration,), { "operations": [] }) dt = datetime.datetime(2015, 7, 31, 4, 40, 0, 0, tzinfo=utc) with mock.patch('django.db.migrations.writer.now', lambda: dt): writer = MigrationWriter(migration) output = writer.as_string() self.assertTrue( output.startswith( "# -*- coding: utf-8 -*-\n" "# Generated by Django %(version)s on 2015-07-31 04:40\n" % { 'version': get_version(), } ) )
Example #3
Source File: whoosh_cn_backend.py From izone with MIT License | 6 votes |
def _from_python(self, value): """ Converts Python values to a string for Whoosh. Code courtesy of pysolr. """ if hasattr(value, 'strftime'): if not hasattr(value, 'hour'): value = datetime(value.year, value.month, value.day, 0, 0, 0) elif isinstance(value, bool): if value: value = 'true' else: value = 'false' elif isinstance(value, (list, tuple)): value = u','.join([force_text(v) for v in value]) elif isinstance(value, (six.integer_types, float)): # Leave it alone. pass else: value = force_text(value) return value
Example #4
Source File: test_writer.py From django-sqlserver with MIT License | 6 votes |
def test_serialize_datetime(self): self.assertSerializedEqual(datetime.datetime.utcnow()) self.assertSerializedEqual(datetime.datetime.utcnow) self.assertSerializedEqual(datetime.datetime.today()) self.assertSerializedEqual(datetime.datetime.today) self.assertSerializedEqual(datetime.date.today()) self.assertSerializedEqual(datetime.date.today) self.assertSerializedEqual(datetime.datetime.now().time()) self.assertSerializedEqual(datetime.datetime(2014, 1, 1, 1, 1, tzinfo=get_default_timezone())) self.assertSerializedEqual(datetime.datetime(2013, 12, 31, 22, 1, tzinfo=FixedOffset(180))) self.assertSerializedResultEqual( datetime.datetime(2014, 1, 1, 1, 1), ("datetime.datetime(2014, 1, 1, 1, 1)", {'import datetime'}) ) self.assertSerializedResultEqual( datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc), ( "datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc)", {'import datetime', 'from django.utils.timezone import utc'}, ) )
Example #5
Source File: whoosh_cn_backend.py From website with MIT License | 6 votes |
def _from_python(self, value): """ Converts Python values to a string for Whoosh. Code courtesy of pysolr. """ if hasattr(value, 'strftime'): if not hasattr(value, 'hour'): value = datetime(value.year, value.month, value.day, 0, 0, 0) elif isinstance(value, bool): if value: value = 'true' else: value = 'false' elif isinstance(value, (list, tuple)): value = u','.join([force_text(v) for v in value]) elif isinstance(value, (six.integer_types, float)): # Leave it alone. pass else: value = force_text(value) return value
Example #6
Source File: whoosh_cn_backend.py From blog with Apache License 2.0 | 6 votes |
def _from_python(self, value): """ Converts Python values to a string for Whoosh. Code courtesy of pysolr. """ if hasattr(value, 'strftime'): if not hasattr(value, 'hour'): value = datetime(value.year, value.month, value.day, 0, 0, 0) elif isinstance(value, bool): if value: value = 'true' else: value = 'false' elif isinstance(value, (list, tuple)): value = u','.join([force_text(v) for v in value]) elif isinstance(value, (six.integer_types, float)): # Leave it alone. pass else: value = force_text(value) return value
Example #7
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_serialize_datetime(self): self.assertSerializedEqual(datetime.datetime.utcnow()) self.assertSerializedEqual(datetime.datetime.utcnow) self.assertSerializedEqual(datetime.datetime.today()) self.assertSerializedEqual(datetime.datetime.today) self.assertSerializedEqual(datetime.date.today()) self.assertSerializedEqual(datetime.date.today) self.assertSerializedEqual(datetime.datetime.now().time()) self.assertSerializedEqual(datetime.datetime(2014, 1, 1, 1, 1, tzinfo=get_default_timezone())) self.assertSerializedEqual(datetime.datetime(2013, 12, 31, 22, 1, tzinfo=FixedOffset(180))) self.assertSerializedResultEqual( datetime.datetime(2014, 1, 1, 1, 1), ("datetime.datetime(2014, 1, 1, 1, 1)", {'import datetime'}) ) self.assertSerializedResultEqual( datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc), ( "datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc)", {'import datetime', 'from django.utils.timezone import utc'}, ) )
Example #8
Source File: whoosh_cn_backend.py From Django-blog with MIT License | 6 votes |
def _from_python(self, value): """ Converts Python values to a string for Whoosh. Code courtesy of pysolr. """ if hasattr(value, 'strftime'): if not hasattr(value, 'hour'): value = datetime(value.year, value.month, value.day, 0, 0, 0) elif isinstance(value, bool): if value: value = 'true' else: value = 'false' elif isinstance(value, (list, tuple)): value = u','.join([force_text(v) for v in value]) elif isinstance(value, (six.integer_types, float)): # Leave it alone. pass else: value = force_text(value) return value
Example #9
Source File: whoosh_cn_backend.py From thirtylol with MIT License | 6 votes |
def _from_python(self, value): """ Converts Python values to a string for Whoosh. Code courtesy of pysolr. """ if hasattr(value, 'strftime'): if not hasattr(value, 'hour'): value = datetime(value.year, value.month, value.day, 0, 0, 0) elif isinstance(value, bool): if value: value = 'true' else: value = 'false' elif isinstance(value, (list, tuple)): value = u','.join([force_text(v) for v in value]) elif isinstance(value, (six.integer_types, float)): # Leave it alone. pass else: value = force_text(value) return value
Example #10
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_sorted_imports(self): """ #24155 - Tests ordering of imports. """ migration = type("Migration", (migrations.Migration,), { "operations": [ migrations.AddField("mymodel", "myfield", models.DateTimeField( default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc), )), ] }) writer = MigrationWriter(migration) output = writer.as_string() self.assertIn( "import datetime\n" "from django.db import migrations, models\n" "from django.utils.timezone import utc\n", output )
Example #11
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_migration_file_header_comments(self): """ Test comments at top of file. """ migration = type("Migration", (migrations.Migration,), { "operations": [] }) dt = datetime.datetime(2015, 7, 31, 4, 40, 0, 0, tzinfo=utc) with mock.patch('django.db.migrations.writer.now', lambda: dt): writer = MigrationWriter(migration) output = writer.as_string() self.assertTrue( output.startswith( "# Generated by Django %(version)s on 2015-07-31 04:40\n" % { 'version': get_version(), } ) )
Example #12
Source File: test_datetime_safe.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_compare_datetimes(self): self.assertEqual(original_datetime(*self.more_recent), datetime(*self.more_recent)) self.assertEqual(original_datetime(*self.really_old), datetime(*self.really_old)) self.assertEqual(original_date(*self.more_recent), date(*self.more_recent)) self.assertEqual(original_date(*self.really_old), date(*self.really_old)) self.assertEqual( original_date(*self.just_safe).strftime('%Y-%m-%d'), date(*self.just_safe).strftime('%Y-%m-%d') ) self.assertEqual( original_datetime(*self.just_safe).strftime('%Y-%m-%d'), datetime(*self.just_safe).strftime('%Y-%m-%d') ) self.assertEqual( original_time(*self.just_time).strftime('%H:%M:%S'), time(*self.just_time).strftime('%H:%M:%S') )
Example #13
Source File: test_datetime_safe.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_safe_strftime(self): self.assertEqual(date(*self.just_unsafe[:3]).strftime('%Y-%m-%d (weekday %w)'), '1899-12-31 (weekday 0)') self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1900-01-01 (weekday 1)') self.assertEqual( datetime(*self.just_unsafe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1899-12-31 23:59:59 (weekday 0)' ) self.assertEqual( datetime(*self.just_safe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1900-01-01 00:00:00 (weekday 1)' ) self.assertEqual(time(*self.just_time).strftime('%H:%M:%S AM'), '11:30:59 AM') # %y will error before this date self.assertEqual(date(*self.just_safe).strftime('%y'), '00') self.assertEqual(datetime(*self.just_safe).strftime('%y'), '00') self.assertEqual(date(1850, 8, 2).strftime("%Y/%m/%d was a %A"), '1850/08/02 was a Friday')
Example #14
Source File: tests.py From django-admin-easy with MIT License | 5 votes |
def test_format_field(self): question = baker.make( Question, pub_date=datetime(2016, 11, 22), question_text='Django admin easy is helpful?', ) custom_field = easy.FormatAdminField('{o.question_text} | {o.pub_date:%Y-%m-%d}', 'column') ret = custom_field(question) self.assertEqual(ret, 'Django admin easy is helpful? | 2016-11-22')
Example #15
Source File: serializer.py From python2017 with MIT License | 5 votes |
def serialize(self): return repr(self.value), {"import datetime"}
Example #16
Source File: tests.py From django-admin-easy with MIT License | 5 votes |
def test_decorators_with_args(self): @easy.filter('date', 'django', 'y-m-d') def field(self, obj): return datetime(2016, 6, 25) self.assertEquals(field(object(), 'asd'), '16-06-25')
Example #17
Source File: tests.py From django-admin-easy with MIT License | 5 votes |
def test_multidecorator(self): @easy.utils('safestring.mark_safe') @easy.filter('date', 'django', 'y-m-d') def field(self, obj): return datetime(2016, 6, 25) v = field(1, 1) self.assertEquals(v, '16-06-25') self.assertIsInstance(v, SafeData)
Example #18
Source File: serializer.py From python2017 with MIT License | 5 votes |
def serialize(self): value_repr = repr(self.value) if isinstance(self.value, datetime_safe.time): value_repr = "datetime.%s" % value_repr return value_repr, {"import datetime"}
Example #19
Source File: test_writer.py From django-sqlserver with MIT License | 5 votes |
def test_serialize_datetime_safe(self): self.assertSerializedResultEqual( datetime_safe.date(2014, 3, 31), ("datetime.date(2014, 3, 31)", {'import datetime'}) ) self.assertSerializedResultEqual( datetime_safe.time(10, 25), ("datetime.time(10, 25)", {'import datetime'}) ) self.assertSerializedResultEqual( datetime_safe.datetime(2014, 3, 31, 16, 4, 31), ("datetime.datetime(2014, 3, 31, 16, 4, 31)", {'import datetime'}) )
Example #20
Source File: test_writer.py From django-sqlserver with MIT License | 5 votes |
def test_serialize_timedelta(self): self.assertSerializedEqual(datetime.timedelta()) self.assertSerializedEqual(datetime.timedelta(minutes=42))
Example #21
Source File: test_writer.py From django-sqlserver with MIT License | 5 votes |
def test_serialize_functools_partial(self): value = functools.partial(datetime.timedelta, 1, seconds=2) result = self.serialize_round_trip(value) self.assertEqual(result.func, value.func) self.assertEqual(result.args, value.args) self.assertEqual(result.keywords, value.keywords)
Example #22
Source File: whoosh_cn_backend.py From izone with MIT License | 5 votes |
def _to_python(self, value): """ Converts values from Whoosh to native Python values. A port of the same method in pysolr, as they deal with data the same way. """ if value == 'true': return True elif value == 'false': return False if value and isinstance(value, six.string_types): possible_datetime = DATETIME_REGEX.search(value) if possible_datetime: date_values = possible_datetime.groupdict() for dk, dv in date_values.items(): date_values[dk] = int(dv) return datetime(date_values['year'], date_values['month'], date_values['day'], date_values['hour'], date_values['minute'], date_values['second']) try: # Attempt to use json to load the values. converted_value = json.loads(value) # Try to handle most built-in types. if isinstance(converted_value, (list, tuple, set, dict, six.integer_types, float, complex)): return converted_value except: # If it fails (SyntaxError or its ilk) or we don't trust it, # continue on. pass return value
Example #23
Source File: whoosh_cn_backend.py From website with MIT License | 5 votes |
def _to_python(self, value): """ Converts values from Whoosh to native Python values. A port of the same method in pysolr, as they deal with data the same way. """ if value == 'true': return True elif value == 'false': return False if value and isinstance(value, six.string_types): possible_datetime = DATETIME_REGEX.search(value) if possible_datetime: date_values = possible_datetime.groupdict() for dk, dv in date_values.items(): date_values[dk] = int(dv) return datetime(date_values['year'], date_values['month'], date_values['day'], date_values['hour'], date_values['minute'], date_values['second']) try: # Attempt to use json to load the values. converted_value = json.loads(value) # Try to handle most built-in types. if isinstance(converted_value, (list, tuple, set, dict, six.integer_types, float, complex)): return converted_value except: # If it fails (SyntaxError or its ilk) or we don't trust it, # continue on. pass return value
Example #24
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_serialize_datetime_safe(self): self.assertSerializedResultEqual( datetime_safe.date(2014, 3, 31), ("datetime.date(2014, 3, 31)", {'import datetime'}) ) self.assertSerializedResultEqual( datetime_safe.time(10, 25), ("datetime.time(10, 25)", {'import datetime'}) ) self.assertSerializedResultEqual( datetime_safe.datetime(2014, 3, 31, 16, 4, 31), ("datetime.datetime(2014, 3, 31, 16, 4, 31)", {'import datetime'}) )
Example #25
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_serialize_timedelta(self): self.assertSerializedEqual(datetime.timedelta()) self.assertSerializedEqual(datetime.timedelta(minutes=42))
Example #26
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_serialize_functools_partial(self): value = functools.partial(datetime.timedelta, 1, seconds=2) result = self.serialize_round_trip(value) self.assertEqual(result.func, value.func) self.assertEqual(result.args, value.args) self.assertEqual(result.keywords, value.keywords)
Example #27
Source File: test_writer.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_serialize_functools_partialmethod(self): value = functools.partialmethod(datetime.timedelta, 1, seconds=2) result = self.serialize_round_trip(value) self.assertIsInstance(result, functools.partialmethod) self.assertEqual(result.func, value.func) self.assertEqual(result.args, value.args) self.assertEqual(result.keywords, value.keywords)
Example #28
Source File: serializer.py From python2017 with MIT License | 5 votes |
def serialize(self): value_repr = repr(self.value) if isinstance(self.value, datetime_safe.date): value_repr = "datetime.%s" % value_repr return value_repr, {"import datetime"}
Example #29
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 #30
Source File: serializer.py From bioforum with MIT License | 5 votes |
def serialize(self): value_repr = repr(self.value) if isinstance(self.value, datetime_safe.date): value_repr = "datetime.%s" % value_repr return value_repr, {"import datetime"}