我有朱莉娅的这段代码,可以将秒转换为小时,分钟和秒,但是当我运行它时,我只是得到(0, 0, 0)
作为输出。有人可以告诉我这怎么了吗?
function convert_from_seconds(sec::Int64)
hours = 0
minutes = 0
seconds = 0
time = (hours, minutes, seconds)
if sec < 60
seconds = sec
elseif sec < 3600
minutes = floor(sec / 60)
seconds = sec % 60
elseif sec < 216000
hours = floor(sec / 3600)
minutes = floor(hours % 3600)
seconds = minutes % 60
end
return time
end
答案 0 :(得分:4)
这是您可能要考虑的另一种方法:
function convert_from_seconds(sec::Int)
x, seconds = divrem(sec, 60)
hours, minutes = divrem(x, 60)
hours, minutes, seconds
end
答案 1 :(得分:4)
考虑使用标准Dates
API及其算法-太好了!比编写自己的函数更优雅:
julia> using Dates
julia> s = Time(0) + Second(4000)
01:06:40
julia> hour(s), minute(s), second(s)
(1, 6, 40)
如果您考虑24小时或更长时间,上述代码将无法获得预期的结果
julia> s = DateTime(0) + Second(4*3600*24+4000)
0000-01-05T01:06:40
julia> year(s), (month(s)-1), (day(s)-1), hour(s), minute(s), second(s)
(0, 0, 4, 1, 6, 40)
没有零月或零日,因此我们需要减去1
。
答案 2 :(得分:0)
方法稍有不同,我是Julia Noob,但这似乎可以在我创建的测试用例上使用,并且看起来更简单。如评论中所述,我认为您的问题是(a)在小时,分钟和秒为0时创建from PIL import Image
import glob
image_list = []
for filename in glob.glob('yourpath/*.gif'): #assuming gif
im=Image.open(filename)
image_list.append(im)
而不是在计算它们之后,并且(b)在计算中出现一些不良逻辑。
time
我运行了这些测试,并且所有测试均按预期进行:
function convert_from_seconds(sec::Int64)
hours = 0
minutes = 0
seconds = sec % 60
if sec >= 60
minutes = floor(sec / 60)
end
if minutes >= 60
hours = floor(minutes / 60)
minutes = minutes % 60
end
time = (hours, minutes, seconds)
return time
end
答案 3 :(得分:0)
作为软件工程师,您必须能够 处理任何有效的输入
function convert_from_seconds(sec::Int64)
if sec == 0
return (0,0,0)
elseif sec < 0
positive_result = convert_from_seconds(-1 * sec)
return (-1 * positive_result[1],-1 * positive_result[2],-1 * positive_result[3])
else
seconds = sec % 60
mins = sec ÷ 60
minutes = mins % 60
hours = mins ÷ 60
return (hours,minutes,seconds)
end
end
println( 50 ," ",convert_from_seconds( 50 ))
println( 100 ," ",convert_from_seconds( 100 ))
println( 2050 ," ",convert_from_seconds( 2050 ))
println( 4050 ," ",convert_from_seconds( 4050 ))
println( -50 ," ",convert_from_seconds( -50 ))
println(-100 ," ",convert_from_seconds( -100 ))
println(-2050 ," ",convert_from_seconds(-2050 ))
println(-4050 ," ",convert_from_seconds(-4050 ))
这是示例运行
Starting Julia...
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.3 (2018-12-18)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
50 (0, 0, 50)
100 (0, 1, 40)
2050 (0, 34, 10)
4050 (1, 7, 30)
-50 (0, 0, -50)
-100 (0, -1, -40)
-2050 (0, -34, -10)
-4050 (-1, -7, -30)
现在,我将说明为什么我不是软件工程师。 Int64的最小值为-2 ^ 63(-1 * big(2)^ 63 == -9223372036854775808),Int64的最大值为2 ^ 63-1(big(2)^ 63-1 == 9223372036854775807)< / p>
julia> convert_from_seconds( -9223372036854775808 )
ERROR: StackOverflowError:
Stacktrace:
[1] convert_from_seconds(::Int64) at ./untitled-7674d416d9e0628b948f1f3731dd1ecc:5 (repeats 80000 times)