如何在python中将日期时间转换为整数

2022-01-24 00:00:00 python datetime converters integer

问题描述

如何在 python 中将 YYYY-MM-DD hh:mm:ss 格式转换为整数?例如 2014-02-12 20:51:14 ->为整数.

How can I convert YYYY-MM-DD hh:mm:ss format to integer in python? for example 2014-02-12 20:51:14 -> to integer.

我只知道如何转换 hh:mm:ss 而不是 yyyy-mm-dd hh:mm:ss

I only know how to convert hh:mm:ss but not yyyy-mm-dd hh:mm:ss

def time_to_num(time_str):
    hh, mm , ss = map(int, time_str.split(':'))
    return ss + 60*(mm + 60*hh)


解决方案

这取决于整数应该编码的内容.您可以将日期转换为以前某个时间的毫秒数.人们经常在 1970 年 1 月 1 日凌晨 12:00 或 1900 年等时间执行此操作,并将时间测量为从该点开始的整数毫秒数.datetime 模块(或其他类似模块)将具有为您执行此操作的函数:例如,您可以使用 int(datetime.datetime.utcnow().timestamp()).

It depends on what the integer is supposed to encode. You could convert the date to a number of milliseconds from some previous time. People often do this affixed to 12:00 am January 1 1970, or 1900, etc., and measure time as an integer number of milliseconds from that point. The datetime module (or others like it) will have functions that do this for you: for example, you can use int(datetime.datetime.utcnow().timestamp()).

如果您想对年、月和日进行语义编码,一种方法是将这些组件乘以足够大的数量级值,以将它们并列在整数位内:

If you want to semantically encode the year, month, and day, one way to do it is to multiply those components by order-of-magnitude values large enough to juxtapose them within the integer digits:

2012-06-13 --> 20120613 = 10,000 * (2012) + 100 * (6) + 1*(13)

2012-06-13 --> 20120613 = 10,000 * (2012) + 100 * (6) + 1*(13)

def to_integer(dt_time):
    return 10000*dt_time.year + 100*dt_time.month + dt_time.day

例如

In [1]: import datetime

In [2]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def to_integer(dt_time):
:    return 10000*dt_time.year + 100*dt_time.month + dt_time.day
:    # Or take the appropriate chars from a string date representation.
:--

In [3]: to_integer(datetime.date(2012, 6, 13))
Out[3]: 20120613

如果您还需要分钟和秒,则只需根据需要包含更多数量级以显示数字.

If you also want minutes and seconds, then just include further orders of magnitude as needed to display the digits.

我在遗留系统中经常遇到第二种方法,尤其是从遗留 SQL 数据库中提取基于日期的数据的系统.

I've encountered this second method very often in legacy systems, especially systems that pull date-based data out of legacy SQL databases.

非常糟糕.您最终会编写大量用于对齐日期、计算月份或日期偏移量的 hacky 代码,因为它们将以整数格式出现(例如,当您经过 12 月时将月份重置回 1,然后增加年份值),以及用于整数格式之间的相互转换.

It is very bad. You end up writing a lot of hacky code for aligning dates, computing month or day offsets as they would appear in the integer format (e.g. resetting the month back to 1 as you pass December, then incrementing the year value), and boiler plate for converting to and from the integer format all over.

除非这样的约定存在于您正在处理的 API 的深层、低级且经过全面测试的部分中,否则曾经使用过数据的每个人都可以真正依靠这种整数表示及其所有辅助函数,那么你最终会导致很多人到处重写基本的日期处理例程.

Unless such a convention lives in a deep, low-level, and thoroughly tested section of the API you're working on, such that everyone who ever consumes the data really can count on this integer representation and all of its helper functions, then you end up with lots of people re-writing basic date-handling routines all over the place.

通常最好将值保留在日期上下文中,如 datetime.date,尽可能长,以便对其进行的操作以自然的日期表示 -基于上下文,而不是某个单独的开发人员个人破解整数.

It's generally much better to leave the value in a date context, like datetime.date, for as long as you possibly can, so that the operations upon it are expressed in a natural, date-based context, and not some lone developer's personal hack into an integer.

相关文章