将字符串时间戳转换为Python日期时间(是:如何在Python中正确地将char转换为float或double)

发布于 2024-12-18 10:04:04 字数 477 浏览 0 评论 0原文

这里我有这样的字符串“1322485986.672901000”,数据类型是代表unixtime的字符串。我想在 Python 中将其转换为日期时间。

我使用的方式是 date = datetime.fromtimestamp(float(row[0])) row[0] 表示像这样的值 1322485986.672901000,因为我有几行。转换结果是错误的,它只计算点之前的数字。所以转换后的日期就像 2011-11-28 13:53:23.6729

我认为问题是 float(row[0]) ,但我不知道如何解决这个问题,或者有谁知道如何以更好的方式将unixtime转换为datetime?

非常感谢!

现在我得到的结果是 2011-11-28 14:13:06.672901

但是当使用在线转换器时,结果是 2011-11-28 13:13:06

Here I have string like this "1322485986.672901000", data type is string which represents unixtime. I want to convert it into datetime in Python.

The way I used is date = datetime.fromtimestamp(float(row[0])) row[0] represents value like this 1322485986.672901000, since I have several rows. The convert result is wrong, it only calculates the digits before the dot. So the date after conversion is like 2011-11-28 13:53:23.6729

I think the problem is float(row[0]), but I don't know how to solve this problem, or does anyone know how to convert unixtime to datetime in a better way?

Many thanks!

Now the result I got is 2011-11-28 14:13:06.672901

But when using an online converter, the result is 2011-11-28 13:13:06

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海的爱人是光 2024-12-25 10:04:04

当前结果中一小时的差异可能是时区应用的结果。

如果需要,您可以提供明确的时区 - tzinfo 对象上的文档< /a> 可以引导您直接创建时区对象,创建后,您只需传入参数即可:

from datetime import datetime, tzinfo, timedelta

timestamp = "1322485986.672901000"

class UTC(tzinfo):
    def utcoffset(self, dt):
         return timedelta(0)
    def tzname(self, dt):
        return "UTC"
    def dst(self, dt):
        return timedelta(0)


d1 = datetime.fromtimestamp(float(timestamp), UTC())
print d1

此代码打印 2011-11-28对我来说是 13:13:06.672901+00:00。

Your one-hour difference in the current result is likely the consequence of time zone application.

You can supply an explicit time zone if you need to -- the docs on tzinfo objects can steer you straight on creating your time zone object, and once it's created, you just pass in the argument:

from datetime import datetime, tzinfo, timedelta

timestamp = "1322485986.672901000"

class UTC(tzinfo):
    def utcoffset(self, dt):
         return timedelta(0)
    def tzname(self, dt):
        return "UTC"
    def dst(self, dt):
        return timedelta(0)


d1 = datetime.fromtimestamp(float(timestamp), UTC())
print d1

This code prints 2011-11-28 13:13:06.672901+00:00 for me.

白衬杉格子梦 2024-12-25 10:04:04
from datetime import datetime

timestamp = "1322485986.672901000"

d1 = datetime.fromtimestamp( float(timestamp) )
print d1

d2 = datetime.fromtimestamp( int(float(timestamp) ) )
print d2

产量:

2011-11-28 05:13:06.672901
2011-11-28 05:13:06
from datetime import datetime

timestamp = "1322485986.672901000"

d1 = datetime.fromtimestamp( float(timestamp) )
print d1

d2 = datetime.fromtimestamp( int(float(timestamp) ) )
print d2

Yields:

2011-11-28 05:13:06.672901
2011-11-28 05:13:06
仙女 2024-12-25 10:04:04
# timestamp to time tuple in UTC
    timestamp = 1226527167.595983
    time_tuple = time.gmtime(timestamp)
    print repr(time_tuple)

# timestamp to time tuple in local time
    timestamp = 1226527167.595983
    time_tuple = time.localtime(timestamp)
    print repr(time_tuple)
# timestamp to time tuple in UTC
    timestamp = 1226527167.595983
    time_tuple = time.gmtime(timestamp)
    print repr(time_tuple)

# timestamp to time tuple in local time
    timestamp = 1226527167.595983
    time_tuple = time.localtime(timestamp)
    print repr(time_tuple)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文