MCPcopy
hub / github.com/myusuf3/delorean / truncate

Method truncate

delorean/dates.py:336–371  ·  view source on GitHub ↗

Truncate the delorian object to the nearest s (second, minute, hour, day, month, year) This is a destructive method, modifies the internal datetime object associated with the Delorean object. .. testsetup:: from datetime import datetime

(self, s)

Source from the content-addressed store, hash-verified

334 return self._tzinfo
335
336 def truncate(self, s):
337 """
338 Truncate the delorian object to the nearest s
339 (second, minute, hour, day, month, year)
340
341 This is a destructive method, modifies the internal datetime
342 object associated with the Delorean object.
343
344 .. testsetup::
345
346 from datetime import datetime
347 from delorean import Delorean
348
349 .. doctest::
350
351 >>> d = Delorean(datetime(2015, 1, 1, 12, 10), timezone='US/Pacific')
352 >>> d.truncate('hour')
353 Delorean(datetime=datetime.datetime(2015, 1, 1, 12, 0), timezone='US/Pacific')
354
355 """
356 if s == 'second':
357 self._dt = self._dt.replace(microsecond=0)
358 elif s == 'minute':
359 self._dt = self._dt.replace(second=0, microsecond=0)
360 elif s == 'hour':
361 self._dt = self._dt.replace(minute=0, second=0, microsecond=0)
362 elif s == 'day':
363 self._dt = self._dt.replace(hour=0, minute=0, second=0, microsecond=0)
364 elif s == 'month':
365 self._dt = self._dt.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
366 elif s == 'year':
367 self._dt = self._dt.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
368 else:
369 raise ValueError("Invalid truncation level")
370
371 return self
372
373 @property
374 def naive(self):

Callers 14

test_normalizeMethod · 0.95
test_truncation_hourMethod · 0.80
test_truncation_dayMethod · 0.80
test_truncation_monthMethod · 0.80
test_truncation_yearMethod · 0.80
test_naive_timezoneMethod · 0.80
test_range_with_startMethod · 0.80

Calls 1

replaceMethod · 0.80

Tested by 14

test_normalizeMethod · 0.76
test_truncation_hourMethod · 0.64
test_truncation_dayMethod · 0.64
test_truncation_monthMethod · 0.64
test_truncation_yearMethod · 0.64
test_naive_timezoneMethod · 0.64
test_range_with_startMethod · 0.64