Check if value is a datetime and converts it to local time if necessary. If use_tz is provided and is not None, that will force the value to be converted (or not), overriding the value of settings.USE_TZ. This function is designed for use by the template engine.
(value, use_tz=None)
| 144 | |
| 145 | |
| 146 | def template_localtime(value, use_tz=None): |
| 147 | """ |
| 148 | Check if value is a datetime and converts it to local time if necessary. |
| 149 | |
| 150 | If use_tz is provided and is not None, that will force the value to |
| 151 | be converted (or not), overriding the value of settings.USE_TZ. |
| 152 | |
| 153 | This function is designed for use by the template engine. |
| 154 | """ |
| 155 | should_convert = ( |
| 156 | isinstance(value, datetime) |
| 157 | and (settings.USE_TZ if use_tz is None else use_tz) |
| 158 | and not is_naive(value) |
| 159 | and getattr(value, "convert_to_local_time", True) |
| 160 | ) |
| 161 | return localtime(value) if should_convert else value |
| 162 | |
| 163 | |
| 164 | # Utilities |
no test coverage detected