Time 如何在“打印时间”中打印3600.125秒;H:M:S.S";朱莉娅的格式
我使用Time 如何在“打印时间”中打印3600.125秒;H:M:S.S";朱莉娅的格式,time,julia,Time,Julia,我使用tic()和toc()函数获得了一些以秒为单位的时间间隔。 假设我有一个时间间隔dt=3600.125s。如何使用Julia以“H:M:S.S”格式打印它?请查看 不确定这是否是最有效的方法,但这是有效的,例如: julia> Dates.format(DateTime("2017-10-01T01:02:03"), "H:M:S.s") "1:2:3.0" 你可以做你自己的功能。要知道的主要函数是divrem,它在一个方便的函数调用中提供除数和余数 dt=3600.125 f
tic()
和toc()
函数获得了一些以秒为单位的时间间隔。
假设我有一个时间间隔dt=3600.125
s。如何使用Julia以“H:M:S.S”格式打印它?请查看
不确定这是否是最有效的方法,但这是有效的,例如:
julia> Dates.format(DateTime("2017-10-01T01:02:03"), "H:M:S.s")
"1:2:3.0"
你可以做你自己的功能。要知道的主要函数是
divrem
,它在一个方便的函数调用中提供除数和余数
dt=3600.125
function hmss(dt)
(h,r) = divrem(dt,60*60)
(m,r) = divrem(r, 60)
#(s,r) = divrem(r, 60)
string(Int(h),":",Int(m),":",r)
end
hmss(dt)
hmss(3452.98)
如果将其转换为日期格式,则可以使用
tic
和toc
不推荐使用toc
可能带来舍入误差,因为它在内部使用时间,但纳秒转换为秒除以1e9 Julia中来自toc()
和time()
的日期使用UNIX时间戳,即自1970年1月1日起经过的秒数
从文件路径f
返回时间戳的函数ctime(f)
也会出现同样的情况
转换非常简单
tic()
....
dif = toc()
# My local is Brazil, one should replace for its local format
Dt = DateTime(LibC.strftime(dif),"d/m/y H:M:S")
LibC
不必显式导入我现在已经试过了,但是做结转有点麻烦。对不起:
using Dates
using Printf
function formatDuration(duration::Period, types::Vector{DataType}=[Hour, Minute, Second]; addUnit::Bool=false)
periods = canonicalize(Dates.CompoundPeriod(duration)).periods
missingTypes = Vector{DataType}(types)
newPeriods = vcat(periods, map(t->t(0), missingTypes))
sort!(newPeriods; rev=true)
newPeriods1 = Vector{Period}()
carryover = nothing
for (i, p) in enumerate(newPeriods)
typeofp = typeof(p)
if !isnothing(carryover)
newPeriod = convert(typeofp, floor(Millisecond(p) + Millisecond(carryover), typeofp(1)))
carryover = nothing
else
newPeriod = p
end
if (typeof(newPeriod) in types)
push!(newPeriods1, newPeriod)
filter!(e->e≠typeofp,missingTypes)
else
carryover = newPeriod
end
end
m = map(types) do t
f = findfirst(x -> typeof(x) == t, newPeriods1);
r = isnothing(f) ? 0 : newPeriods1[f].value;
@sprintf("%02d",r) * (addUnit ? lowercase("$t"[1]) : "")
end
join(m, ":")
end
用法示例:
@show formatDuration(Millisecond(3600.125*1000))
# returns "01:00:00"
@show formatDuration(Day(2), [Hour, Minute, Second])
# returns "48:00:00"
@show formatDuration(Second(100000000), [Hour, Minute, Second])
# returns "27777:46:40"
@show formatDuration(Second(100000001), [Day, Hour, Minute, Second, Millisecond])
# returns "1157:09:46:41:00"
@show formatDuration(Millisecond(10000000001),
[Week, Day, Hour, Minute, Second, Millisecond]; addUnit=true)
#returns "16w:03d:17h:46m:40s:01m"
对我强烈建议在Julia.@MichaelK.Borregaard中使用强大的内置工具进行日期时间操作。你能看看我的答案,或许能更好地使用“内置工具”吗?从我的观点来看,朱莉娅仍然有点复杂。例如,我希望Dates.format(Dates.Nanosecond(1e8),dateformat“S.S”)
可以正常工作。这个aswer可以在3600*24秒内解决问题。然而,假约会是必要的。既然朱莉娅的约会时间支持这么好,你为什么要这么做呢?因为教如何比什么更重要。学习我的方法的ppl可以转移到本机datetime,但其他方法也很难,因为在这种情况下使用datetime很麻烦。它需要调用一些函数、格式化和一些技巧。例如,参见下面Liso的回答。小戴的解决方案简单明了。
tic()
....
dif = toc()
# My local is Brazil, one should replace for its local format
Dt = DateTime(LibC.strftime(dif),"d/m/y H:M:S")
using Dates
using Printf
function formatDuration(duration::Period, types::Vector{DataType}=[Hour, Minute, Second]; addUnit::Bool=false)
periods = canonicalize(Dates.CompoundPeriod(duration)).periods
missingTypes = Vector{DataType}(types)
newPeriods = vcat(periods, map(t->t(0), missingTypes))
sort!(newPeriods; rev=true)
newPeriods1 = Vector{Period}()
carryover = nothing
for (i, p) in enumerate(newPeriods)
typeofp = typeof(p)
if !isnothing(carryover)
newPeriod = convert(typeofp, floor(Millisecond(p) + Millisecond(carryover), typeofp(1)))
carryover = nothing
else
newPeriod = p
end
if (typeof(newPeriod) in types)
push!(newPeriods1, newPeriod)
filter!(e->e≠typeofp,missingTypes)
else
carryover = newPeriod
end
end
m = map(types) do t
f = findfirst(x -> typeof(x) == t, newPeriods1);
r = isnothing(f) ? 0 : newPeriods1[f].value;
@sprintf("%02d",r) * (addUnit ? lowercase("$t"[1]) : "")
end
join(m, ":")
end
@show formatDuration(Millisecond(3600.125*1000))
# returns "01:00:00"
@show formatDuration(Day(2), [Hour, Minute, Second])
# returns "48:00:00"
@show formatDuration(Second(100000000), [Hour, Minute, Second])
# returns "27777:46:40"
@show formatDuration(Second(100000001), [Day, Hour, Minute, Second, Millisecond])
# returns "1157:09:46:41:00"
@show formatDuration(Millisecond(10000000001),
[Week, Day, Hour, Minute, Second, Millisecond]; addUnit=true)
#returns "16w:03d:17h:46m:40s:01m"