gpt4 book ai didi

erlang:now 加时间戳并再次返回

转载 作者:行者123 更新时间:2023-12-04 22:07:07 25 4
gpt4 key购买 nike

在我的项目中 erlang:now 被转换为高精度时间戳 (bigint) 以存储在 MySQL 中:

timestamp({Mega, Secs, Micro}) ->
Mega*1000*1000*1000*1000 + Secs * 1000 * 1000 + Micro.

我现在使用以下方法将时间戳转换回原始 {Mega, Secs, Micro} 元组:
time_tuple(Timestamp) ->
TimeList = erlang:integer_to_list(Timestamp),
Mega = erlang:list_to_integer(string:substr(TimeList, 1, 4)),
Sec = erlang:list_to_integer(string:substr(TimeList, 5, 6)),
Micro = erlang:list_to_integer(string:substr(TimeList, 11, 6)),
{Mega, Sec, Micro}.

字符串转换/substr 感觉就像一个丑陋的,可能不正确的黑客。什么是更优雅的方式?

最佳答案

我可能会遗漏一些东西,但你为什么不直接使用除法和模数呢?

> {Mega, Sec, Micro} = now().
> Timestamp = Mega * 1000000 * 1000000 + Sec * 1000000 + Micro.
> {Mega1, Sec1, Micro1} = {Timestamp div 1000000000000,
Timestamp div 1000000 rem 1000000,
Timestamp rem 1000000}.

关于erlang:now 加时间戳并再次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9495320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com