Return a datetime with the same attributes, except for those attributes given new values by whichever keyword arguments are specified. Note that tzinfo=None can be specified to create a naive datetime from an aware datetime with no conversion of date
(self, *args, **kwargs)
| 66 | __slots__ = () |
| 67 | |
| 68 | def replace(self, *args, **kwargs): |
| 69 | """ |
| 70 | Return a datetime with the same attributes, except for those |
| 71 | attributes given new values by whichever keyword arguments are |
| 72 | specified. Note that tzinfo=None can be specified to create a naive |
| 73 | datetime from an aware datetime with no conversion of date and time |
| 74 | data. |
| 75 | |
| 76 | This is reimplemented in ``_DatetimeWithFold`` because pypy3 will |
| 77 | return a ``datetime.datetime`` even if ``fold`` is unchanged. |
| 78 | """ |
| 79 | argnames = ( |
| 80 | 'year', 'month', 'day', 'hour', 'minute', 'second', |
| 81 | 'microsecond', 'tzinfo' |
| 82 | ) |
| 83 | |
| 84 | for arg, argname in zip(args, argnames): |
| 85 | if argname in kwargs: |
| 86 | raise TypeError('Duplicate argument: {}'.format(argname)) |
| 87 | |
| 88 | kwargs[argname] = arg |
| 89 | |
| 90 | for argname in argnames: |
| 91 | if argname not in kwargs: |
| 92 | kwargs[argname] = getattr(self, argname) |
| 93 | |
| 94 | dt_class = self.__class__ if kwargs.get('fold', 1) else datetime |
| 95 | |
| 96 | return dt_class(**kwargs) |
| 97 | |
| 98 | @property |
| 99 | def fold(self): |
no test coverage detected