Julia 圆朱丽亚';s毫秒类型,精确到秒或分钟

Julia 圆朱丽亚';s毫秒类型,精确到秒或分钟,julia,Julia,我想计算一对四舍五入到最接近的秒或分钟的日期时间之间的差值 initial = now() println(typeof(initial)) sleep(12) final = now() difference = final - initial println(typeof(difference)) julia> d = (now() - moment).value/60_000 3.9330833333333333 julia> e = round(d) 4.0 给予 后一

我想计算一对四舍五入到最接近的秒或分钟的日期时间之间的差值

initial = now()
println(typeof(initial))
sleep(12)
final = now()
difference = final - initial
println(typeof(difference))
julia> d = (now() - moment).value/60_000
3.9330833333333333

julia> e = round(d)
4.0
给予


后一种类型很难使用,因为几乎所有的便利类型都用于
DateTime
s。建议使用什么方法将差异转换为秒或分数分钟?这是否可能不降为整数?我更愿意避免这种情况,因为它更容易出错。

因为
差异
表示日期之间的持续时间,而不是特定的时间,所以以毫秒为单位的持续时间是有意义的。此外,
DateTime
Base.Dates.millis秒
对象在内部用
Int64
表示,因此所有对象都已经是整数了

julia> moment = now()
2016-12-22T22:54:57.393

julia> dump(moment)
DateTime
  instant: Base.Dates.UTInstant{Base.Dates.Millisecond}
    periods: Base.Dates.Millisecond
      value: Int64 63618130497393

julia> dump(now()-moment)
Base.Dates.Millisecond
  value: Int64 29820
将以毫秒为单位的值除以1000得到秒,或除以60000得到分钟。使用
round()

initial = now()
println(typeof(initial))
sleep(12)
final = now()
difference = final - initial
println(typeof(difference))
julia> d = (now() - moment).value/60_000
3.9330833333333333

julia> e = round(d)
4.0
然后乘以1000或60000,并将其反馈到
日期中。毫秒
将舍入的数字转换回相应的对象:

julia> Dates.Millisecond(60_000e)
240000 milliseconds
Date
DateTime
对象四舍五入到给定的时间间隔要简单得多,因为您可以根据使用
round()
,并将其分派到相关方法:


是否要将毫秒转换为整数,如:
convert(Int64,difference)
?或者是一个
DateTime
?这是我问题中的一个输入错误。如果可能的话,我希望避免使用整数。我不确定,但我认为您正在查找
日期。epochs2datetime(difference)
。将毫秒转换为
DateTime
类型(例如问题中的示例,它将给出大约
0000-01-01T00:00:21.608
),因此您可以使用