如何在python中将datetime转换为整数 [英] How to convert datetime to integer in python

查看:2964
本文介绍了如何在python中将datetime转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将YYYY-MM-DD hh:mm:ss格式转换为python中的整数?
例如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 know how to convert only 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日中午1点00分或1900年,等等,并从那时起测量时间为整数毫秒。 datetime 模块(或其他类似的)将具有为您提供的功能;只需Google即可将其转换为毫秒。

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; just Google around for converting to milliseconds.

如果要对年,月和日进行语义编码,可以通过按顺序将这些组件相乘,值大到足以将它们并入整数数字:

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代码来对齐日期,计算月或日偏移量,因为它们将以整数格式出现(例如,当你通过十二月,然后递增年值时,将月​​份重新设置为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.

这篇关于如何在python中将datetime转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆