有这个示例代码,但它开始讨论毫秒/纳秒问题.
自从C#中的Unix纪元以来,同样的问题出现在MSDN上,秒.
这是我到目前为止所得到的:
public Double CreatedEpoch
{
get
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
TimeSpan span = (this.Created.ToLocalTime() - epoch);
return span.TotalSeconds;
}
set
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
this.Created = epoch.AddSeconds(value);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个Python datetime对象,我想转换为unix时间,或自1970年代以来的秒/毫秒.
我该怎么做呢?
我现在一直在与此斗争.我正在尝试将纪元转换为日期对象.这个时代以UTC的形式发送给我.每当你通过new Date()一个时代,它就会认为它是本地的时代.我尝试创建一个UTC对象,然后使用setTime()它调整到适当的纪元,但唯一有用的方法是toUTCString()和字符串不能帮助我.如果我将该字符串传递给新的日期,它应该注意到它是UTC,但事实并非如此.
new Date( new Date().toUTCString() ).toLocaleString()
Run Code Online (Sandbox Code Playgroud)
我的下一次尝试是尝试获取当地当前时期和UTC当前时期之间的差异,但我也无法得到它.
new Date( new Date().toUTCString() ).getTime() - new Date().getTime()
Run Code Online (Sandbox Code Playgroud)
它只给了我很小的差异,低于1000,这是毫秒.
有什么建议?
我想得到当前的时间戳:1320917972
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts = tsTemp.toString();
Run Code Online (Sandbox Code Playgroud) time从epoch开始,模块可以使用秒进行初始化:
>>> import time
>>> t1=time.gmtime(1284286794)
>>> t1
time.struct_time(tm_year=2010, tm_mon=9, tm_mday=12, tm_hour=10, tm_min=19,
tm_sec=54, tm_wday=6, tm_yday=255, tm_isdst=0)
Run Code Online (Sandbox Code Playgroud)
有一种优雅的方式以datetime.datetime相同的方式初始化对象吗?
我有一段时间在UTC,从那里我想要自纪元以来的秒数.
我使用strftime将其转换为秒数.以2012年4月1日为例.
>>>datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'
Run Code Online (Sandbox Code Playgroud)
2012年4月1日UTC的纪元是1333238400但是这个上面返回1333234800,相差1小时.
所以看起来strftime正在考虑我的系统时间并在某个地方应用时区转换.我以为datetime纯粹是天真的?
我怎么能绕过那个?如果可能的话,避免导入其他库,除非标准.(我有可移植性问题).
我试图将长值(从1970年1月1日(即大纪元)经过的毫秒数)转换为格式时间h:m:s:ms.
我用作时间戳的长值,timestamp来自log4j的日志事件字段.
到目前为止,我已经尝试了以下内容,但它失败了:
logEvent.timeStamp/ (1000*60*60)
TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
Run Code Online (Sandbox Code Playgroud)
但是我的价值不正确:
1289375173771 for logEvent.timeStamp
358159 for logEvent.timeStamp/ (1000*60*60)
21489586 for TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
Run Code Online (Sandbox Code Playgroud)
我该怎么做?