Created
July 1, 2016 08:39
-
-
Save momijiame/6f97fa151798f95dbbce1bc7963f71ee to your computer and use it in GitHub Desktop.
datetime オブジェクトの特定フィールド以下を 0 に切り捨てる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from datetime import datetime | |
SECOND = {'microsecond': 0} | |
MINUTE = dict({'second': 0}, **SECOND) | |
HOUR = dict({'minute': 0}, **MINUTE) | |
DAY = dict({'hour': 0}, **HOUR) | |
MONTH = dict({'day': 0}, **DAY) | |
YEAR = dict({'month': 0}, **MONTH) | |
def truncate(dt, field): | |
return dt.replace(**field) | |
def main(): | |
now = datetime.now() | |
# 使用例 => 2016-07-01 00:00:00 | |
print(truncate(now, DAY)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment