MCPcopy
hub / github.com/django/django / TimeFormat

Class TimeFormat

django/utils/dateformat.py:55–195  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

53
54
55class TimeFormat(Formatter):
56 def __init__(self, obj):
57 self.data = obj
58 self.timezone = None
59
60 if isinstance(obj, datetime):
61 # Timezone is only supported when formatting datetime objects, not
62 # date objects (timezone information not appropriate), or time
63 # objects (against established django policy).
64 if is_naive(obj):
65 timezone = get_default_timezone()
66 else:
67 timezone = obj.tzinfo
68 if not _datetime_ambiguous_or_imaginary(obj, timezone):
69 self.timezone = timezone
70
71 def a(self):
72 "'a.m.' or 'p.m.'"
73 if self.data.hour > 11:
74 return _("p.m.")
75 return _("a.m.")
76
77 def A(self):
78 "'AM' or 'PM'"
79 if self.data.hour > 11:
80 return _("PM")
81 return _("AM")
82
83 def e(self):
84 """
85 Timezone name.
86
87 If timezone information is not available, return an empty string.
88 """
89 if not self.timezone:
90 return ""
91
92 try:
93 if getattr(self.data, "tzinfo", None):
94 return self.data.tzname() or ""
95 except NotImplementedError:
96 pass
97 return ""
98
99 def f(self):
100 """
101 Time, in 12-hour hours and minutes, with minutes left off if they're
102 zero.
103 Examples: '1', '1:30', '2:05', '2'
104 Proprietary extension.
105 """
106 hour = self.data.hour % 12 or 12
107 minute = self.data.minute
108 return "%d:%02d" % (hour, minute) if minute else hour
109
110 def g(self):
111 "Hour, 12-hour format without leading zeros; i.e. '1' to '12'"
112 return self.data.hour % 12 or 12

Callers 1

time_formatFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…