【问题标题】:Equivalent of gmtime in Julia?相当于 Julia 中的 gmtime?
【发布时间】:2015-05-17 09:47:06
【问题描述】:

Julia 内置了strftime,但没有gmtime

julia> strftime
strftime (generic function with 3 methods)

julia> gmtime
ERROR: gmtime not defined

gmtime 等效的 Julia 首选方法是什么?这个想法是将自纪元以来的秒数转换为 Z (+00:00) 时区中的时间结构。在洛杉矶,我看到了:

julia> strftime("%H:%M:%S", 0)
"16:00:00"

我想见"00:00:00"。我可以用 Python 做到这一点:

>>> from time import strftime, gmtime
>>> strftime("%H:%M:%S", gmtime(0))
'00:00:00'

我尝试在 Julia 中使用 ccall,但没有成功

julia> ccall( (:gmtime, "libc"), TmStruct, (Int64,), 0)
TmStruct(1590498096,32767,16041550,1,-1924564896,32744,1,0,0,0,0,0,1590498144,32767)

julia> strftime("%H:%M:%S", ans)
"16041550:32767:1590498096"

我的ccall 出了什么问题?更好的是,是否有更好的全 Julia 方式来获得 gmtime 的效果?

【问题讨论】:

  • 您查看过 Datetime 包吗?我想创建一个日期时间对象可以让她在大多数情况下完成。

标签: time timezone julia strftime


【解决方案1】:

看起来 gmtime 在 Julia 的 TODO 列表中。在它被包含之前,这样的东西对你有用吗?

julia> function gmtime(t::Real)
           t = floor(t)
           tm = TmStruct()
           ccall(:gmtime_r, Ptr{TmStruct}, (Ptr{Int}, Ptr{TmStruct}), &t, &tm)
           return tm
       end
gmtime (generic function with 1 method)

julia> strftime("%H:%M:%S", gmtime(0))
"00:00:00"

【讨论】:

  • 做得很好。使用_r 版本要好得多,因为从 C 调用常规的gmtime 会去谁知道在哪里。
猜你喜欢
  • 2018-06-24
  • 2021-09-14
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多