我想转换2014-08-14 20:01:28.242成一个 unix 时间戳245293529385并用当前时间戳减去它,以便通过从 14 中减去这个值来确定已经过去了多少天并且最终剩余多少天。
场景:用户注册,我想倒计时剩余的试用天数。
我想将Python date对象转换为Unix时间戳.
我已经添加了一个time对象,所以它将是午夜的特定日期,但我不知道如何从那里继续.
d = date(2014, 10, 27)
t = time(0, 0, 0)
dt = datetime.combine(d, t)
#How to convert this to Unix timestamp?
Run Code Online (Sandbox Code Playgroud)
我使用的是Python 2.7
因为我完全迷失在你在 stackoverflow 上找到的几十种时间戳转换方法,所以我会在这里问完整的问题:
从 apache 日志(在 CEST 时区)转换此时间戳:
30/Aug/2015:05:13:53 +0200
Run Code Online (Sandbox Code Playgroud)
进入这个:
1440904433
Run Code Online (Sandbox Code Playgroud)
使用
$ python --version
Python 2.6.6
Run Code Online (Sandbox Code Playgroud)
确认:
$ date --date @1440904433
Sun Aug 30 05:13:53 CEST 2015
$ date -u --date @1440904433
Sun Aug 30 03:13:53 UTC 2015
Run Code Online (Sandbox Code Playgroud)
不好的结果是:
1440911633
1440908033
Run Code Online (Sandbox Code Playgroud)
我当前的代码到这里为止:
>>> from dateutil import parser
>>> parser.parse("30/Aug/2015:05:13:53 +0200".replace(':',' ',1))
datetime.datetime(2015, 8, 30, 5, 13, 53, tzinfo=tzoffset(None, 7200))
Run Code Online (Sandbox Code Playgroud)
请不要提出 pytz 模块,我没有它,我无法安装它。请不要为python3提出解决方案
我怎么有一个简单的问题。我需要将字符串格式的日期转换为数字:
time = '2014-03-05 07:22:26.976637+00:00'
type(time)
str
Run Code Online (Sandbox Code Playgroud)
我想将此日期转换为唯一数字
谢谢你。
来自http://docs.python.org/library/time.html:
time.mktime(t):这是localtime()的反函数.它的参数是struct_time或完整的9元组(因为需要dst标志;如果它是未知的,则使用-1作为dst标志),它以本地时间而不是UTC表示时间.它返回一个浮点数,以便与time()兼容.如果输入值不能表示为有效时间,则会引发OverflowError或ValueError(这取决于Python或底层C库是否捕获到无效值).它可以生成时间的最早日期取决于平台.
这表示您需要在当地时间而不是UTC指定时间元组.但是,我想用UTC指定; 我不想在盒子上使用本地时区.
有什么方法可以从datetime到时间戳,其中时间被视为UTC?当我转换为时间戳和从时间戳转换时,我希望能够将所有内容保存在规范化的UTC表单(datetime对象)中.
我希望能够做这样的事情,让x和y出现相同:
y = datetime.datetime.utcfromtimestamp(time.mktime(x.timetuple()))
x = dateutil.parser.parse('Wed, 27 Oct 2010 22:17:00 GMT')
stamp = time.mktime(x.timetuple())
y = datetime.datetime.utcfromtimestamp(stamp)
x
datetime.datetime(2010, 10, 27, 22, 17, tzinfo=tzutc())
y
datetime.datetime(2010, 10, 28, 6, 17)
Run Code Online (Sandbox Code Playgroud) 我想找到最后一个午夜时间戳(只输入当前时间戳).什么是最好的方法?
我正在为全球移动应用程序编写python脚本.具有当前时间戳的用户请求,并且在服务器端,我想找到没有影响时区参数的用户的最后一个午夜时间戳.
我搜索了它,我得到了一个解决方案
import time
etime = int(time.time())
midnight = (etime - (etime % 86400)) + time.altzon
Run Code Online (Sandbox Code Playgroud)
它对我有用.但我对time.altzon功能感到困惑,是否为不同时区的用户带来了任何问题.
因此,我有两个函数可以将python datetime.datetime()对象与毫秒之间相互转换。我无法弄清楚哪里出了问题。这是我正在使用的:
>>> import datetime
>>> def mil_to_date(mil):
"""date items from REST services are reported in milliseconds,
this function will convert milliseconds to datetime objects
Required:
mil -- time in milliseconds
"""
if mil == None:
return None
elif mil < 0:
return datetime.datetime.utcfromtimestamp(0) + datetime.timedelta(seconds=(mil/1000))
else:
return datetime.datetime.fromtimestamp(mil / 1000)
>>> def date_to_mil(date):
"""converts datetime.datetime() object to milliseconds
date -- datetime.datetime() object"""
if isinstance(date, datetime.datetime):
epoch = datetime.datetime.utcfromtimestamp(0)
return long((date - epoch).total_seconds() * 1000.0)
>>> mil = 1394462888000 …Run Code Online (Sandbox Code Playgroud) 我怎样才能为下一个6点创建一个时间戳,无论是今天还是明天?
我尝试使用datetime.datetime.today()并用+1和小时= 6替换日,但我无法将其转换为时间戳.
需要你的帮助
我有一个pandas.Series可能看起来像这样的对象:
import pandas as pd
myVar = pd.Series(["VLADIVOSTOK 690090", "MAHE", NaN, NaN, "VLADIVOSTOK 690090", "2000-07-01 00:00:00"])
Run Code Online (Sandbox Code Playgroud)
myVar[5]datetime.datetime当数据通过Python读入时,被解析为对象pandas.我假设将此值转换为自纪元(36708)以来的天数并不困难.我刚接触Python,不知道该怎么做.提前致谢!
我想将BoxAnnotation添加到具有日期时间x轴的绘图中.如何将BoxAnnotation的左右限制设置为日期时间或日期对象.这是我的目标,但它不起作用.
from bokeh.sampledata.glucose import data
from bokeh.models import BoxAnnotation
from datetime import *
# output_file("box_annotation.html", title="box_annotation.py example")
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
#reduce data size
data = data.ix['2010-10-06':'2010-10-13']
p = figure(x_axis_type="datetime", tools=TOOLS)
p.line(data.index.to_series(), data['glucose'],
line_color="gray", line_width=1, legend="glucose")
left_box = BoxAnnotation(plot=p, right=date(2010,10,7), fill_alpha=0.1, fill_color='blue')
mid_box = BoxAnnotation(plot=p, left=date(2010,10,8), right=date(2010,10,9), fill_alpha=0.1, fill_color='yellow')
right_box = BoxAnnotation(plot=p, left=date(2010,10,10), fill_alpha=0.1, fill_color='blue')
p.renderers.extend([left_box, mid_box, right_box])
p.title = "Glucose Range"
p.xgrid[0].grid_line_color=None
p.ygrid[0].grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'
show(p)
Run Code Online (Sandbox Code Playgroud) python ×8
datetime ×7
python-2.7 ×3
date ×2
epoch ×2
time ×2
apache ×1
bokeh ×1
char ×1
pandas ×1
python-2.5 ×1
python-2.6 ×1
string ×1
timestamp ×1
unix ×1