• Python Cookbook 3.1. Calculating Yesterday and Tomorrow


    Credit: Andrea Cavalcanti

    Problem

    问题

    You want to get today's date, then calculate yesterday's or tomorrow's.

    你想去获取今天的日期,然后计算昨天或者明天的日期

    Solution

    解决

    Whenever you have to deal with a "change" or "difference" in time, think timedelta:

    当你要处理时间的变更或时间的差异时,可以考虑使用timedelta:
    Code

    Discussion

    讨论

    This recipe's Problem has been a fairly frequent question on Python mailing lists since the datetime module arrived. When first confronted with this task, it's quite common for people to try to code it as yesterday = today - 1, which gives a TypeError: unsupported operand type(s) for -: 'datetime.date' and 'int'.

    这个配方中的问题在Python的邮件列表中经常被提到.很多人在首次面对这个问题时总是试图去编写这样的代码,yesterday = today - 1,Python对这样的代码将会给出TypeError: unsupported operand type(s) for -: 'datetime.date' and 'int'.

    Some people have called this a bug, implying that Python should guess what they mean. However, one of the guiding principles that gives Python its simplicity and power is: "in the face of ambiguity, refuse the temptation to guess." Trying to guess would clutter datetime with heuristics meant to guess that you "really meant 1 day", rather than 1 second (which timedelta also supports), or 1 year.

    一些人认为这是一个bug,认为Python应该猜测这些语句的含义.但是,一个给予Python的简明和力量的准则是"在面对歧义时,不要猜测."通过猜测很难确定你是要变更一天,一秒(timedelta支持变更秒)还是一年.

    Rather than trying to guess what you mean, Python, as usual, expects you to make your meaning explicit. If you want to subtract a time difference of one day, you code that explicitly. If, instead, you want to add a time difference of one second, you can use timedelta with a datetime.datetime object, and then you code the operation using exactly the same syntax. This way, for each task you might want to perform, there's only one obvious way of doing it. This approach also allows a fair amount of flexibility, without added complexity. Consider the following interactive snippet:

    Python通常希望你更精确地表达你的意思而不是去猜测你的意思.如果你想减去一天的时间,你要对其进行准确的编码.如果你想减去一秒的时间,你可以使用datetime.datetime对象的timedelta 方法,然后使用同样的语法来完成这个运算.这样,对每个任务,你只有唯一的方法来完成它.这种途径也在不增加复杂性的前提下尽量保证了灵活性.考虑如下的交互片段:

    Code

    Keep in mind that, if you want fancier control over date and time arithmetic, third-party packages, such as dateutil (which works together with the built-in datetime) and the classic mx.DateTime, are available. For example:

    如果你想像控制数字一样控制时间和日期,你可以使用第三方的包来完成.如dateutilmx.DateTime,代码如下:

    1from dateutil import relativedelta 
    2nextweek = today + relativedelta.relativedelta(weeks=1)
    3print nextweek
    4#emits: 2004-11-25

    However, "always do the simplest thing that can possibly work." For simple, straightforward tasks such as the ones in this recipe, datetime.timedelta works just fine.

    但是要记住"在能完成工作的前提下,始终做最简单的事".如在这个配方中的简单任务,datetime.timedelta 就能很好的完成.

    See Also

    参考

    dateutil documentation at https://moin.conectiva.com.br/DateUtil?action=highlight&value= DateUtil, and datetime documentation in the Library Reference. mx.DateTime can be found at http://www.egenix.com/files/python/mxDateTime.html

    dateutil的文档在https://moin.conectiva.com.br/DateUtil?action=highlight&value= DateUtil,datetime 的文档能在库参考中找到. mx.DateTime的文档在 http://www.egenix.com/files/python/mxDateTime.html.


  • 相关阅读:
    【pandas实战】时间差计算
    【pandas实战】数据分类整理
    海量数据处理方法整理记录
    Springboot中实现策略模式+工厂模式
    实现一个秒杀系统
    Redis实现的分布式锁和分布式限流
    实现分布式服务注册及简易的netty聊天
    聊聊数据库乐观锁和悲观锁,乐观锁失败后重试
    聊聊RPC原理二
    聊聊kafka结构
  • 原文地址:https://www.cnblogs.com/triStoneL/p/1562739.html
Copyright © 2020-2023  润新知