UTC Optimized UTC implementation. It unpickles using the single module global instance defined beneath this class declaration.
| 211 | |
| 212 | |
| 213 | class UTC(BaseTzInfo): |
| 214 | """UTC |
| 215 | |
| 216 | Optimized UTC implementation. It unpickles using the single module global |
| 217 | instance defined beneath this class declaration. |
| 218 | """ |
| 219 | zone = "UTC" |
| 220 | |
| 221 | _utcoffset = ZERO |
| 222 | _dst = ZERO |
| 223 | _tzname = zone |
| 224 | |
| 225 | def fromutc(self, dt): |
| 226 | if dt.tzinfo is None: |
| 227 | return self.localize(dt) |
| 228 | return super(utc.__class__, self).fromutc(dt) |
| 229 | |
| 230 | def utcoffset(self, dt): |
| 231 | return ZERO |
| 232 | |
| 233 | def tzname(self, dt): |
| 234 | return "UTC" |
| 235 | |
| 236 | def dst(self, dt): |
| 237 | return ZERO |
| 238 | |
| 239 | def __reduce__(self): |
| 240 | return _UTC, () |
| 241 | |
| 242 | def localize(self, dt, is_dst=False): |
| 243 | '''Convert naive time to local time''' |
| 244 | if dt.tzinfo is not None: |
| 245 | raise ValueError('Not naive datetime (tzinfo is already set)') |
| 246 | return dt.replace(tzinfo=self) |
| 247 | |
| 248 | def normalize(self, dt, is_dst=False): |
| 249 | '''Correct the timezone information on the given datetime''' |
| 250 | if dt.tzinfo is self: |
| 251 | return dt |
| 252 | if dt.tzinfo is None: |
| 253 | raise ValueError('Naive time - no tzinfo set') |
| 254 | return dt.astimezone(self) |
| 255 | |
| 256 | def __repr__(self): |
| 257 | return "<UTC>" |
| 258 | |
| 259 | def __str__(self): |
| 260 | return "UTC" |
| 261 | |
| 262 | |
| 263 | UTC = utc = UTC() # UTC is a singleton |