1from __future__ import unicode_literals
2
3import datetime
4from django.conf import settings
5from django.utils import timezone
6
7
8def make_aware(value):
9    if settings.USE_TZ and timezone.is_naive(value):
10        default_tz = timezone.get_default_timezone()
11        value = timezone.make_aware(value, default_tz)
12    return value
13
14
15def make_naive(value):
16    if settings.USE_TZ and timezone.is_aware(value):
17        default_tz = timezone.get_default_timezone()
18        value = timezone.make_naive(value, default_tz)
19    return value
20
21
22def make_naive_utc(value):
23    """
24    Translate a datetime to UTC, then strip TZ info; useful as a last step before creating the
25    Retry-After header.
26    """
27    utc_value = timezone.localtime(value, timezone.utc)
28    return timezone.make_naive(utc_value)
29
30
31def now():
32    d = timezone.now()
33
34    if d.tzinfo:
35        return timezone.localtime(d)
36
37    return d
38
39
40def aware_date(*args, **kwargs):
41    return make_aware(datetime.date(*args, **kwargs))
42
43
44def aware_datetime(*args, **kwargs):
45    return make_aware(datetime.datetime(*args, **kwargs))
46