Erlang:带有时区算法的时间戳

发布于 2024-10-27 05:22:43 字数 877 浏览 1 评论 0原文

在 Erlang 中相对于时区向特定时间戳添加/减去单位的最佳方法是什么?

据我所知,stdlib 的日历可以使用本地或 UTC 时区,仅此而已。此外,建议仅在 UTC 时区进行算术(原因很明显)。

例如,如果我需要在 CET(中欧时间)和本地(系统)时间中向 {{2011,3,24},{11,13,15}} 添加 1 个月,我该怎么办区不是CET吗?这甚至与将此时间戳转换为 UTC、添加 31 * 24 * 60 * 60 秒并转换回 CET 不同(这将给出 {{2011,4,24},{12,13,15}} 而不是{{2011,4,24},{11,13,15}})。顺便说一下,如果 CET 不是 stdlib 的本地时区,我们甚至不能做这样的事情。

我在谷歌上找到的答案是:

  1. setenv 使本地时区 = 所需时区(这首先非常难看;然后它只允许将所需时区转换为 utc 并针对 utc 进行算术,而不是所需时区)
  2. open_port 到 linux date util 并在那里做算术(不是那么难看;相当慢;需要一些解析,因为 erlang 和 date 之间的协议将是文本的)
  3. 端口驱动程序或 erl_interface 使用其标准库到 C (一点也不难看;但我没有找到现成的解决方案,而且我不太擅长用 C 语言编写一个)

理想的解决方案是使用操作系统时区信息在 Erlang 中编写的东西,但我没有找到任何解决方案。

现在我坚持解决方案 2(open_port to date util)。有更好的办法吗?

提前致谢。

PS 有一个类似的问题,但没有好的答案 时区列表问题

What is the best way to add/subtract units to/from specific timestamp with respect to time zone in Erlang?

From what I've found, calendar of stdlib can work with either local or UTC time zone, no more. Moreover, arithmetics is recommended to do in UTC time zone only (the reason is obvious).

What should I do if, for instance, I need to add 1 month to {{2011,3,24},{11,13,15}} in, let's say, CET (Central European Time) and local (system) time zone is not CET? That is not even the same as converting this timestamp to UTC, adding 31 * 24 * 60 * 60 seconds and converting back to CET (that will give {{2011,4,24},{12,13,15}} instead of {{2011,4,24},{11,13,15}}). By the way we can't do even such a thing if CET is not local time zone with stdlib.

The answers I found googling are:

  1. setenv to make local time zone = needed time zone (that is very ugly first of all; then it will only allow to convert needed time zone to utc and do arithmetics respective to utc, not the needed time zone)
  2. open_port to linux date util and do arithmetics there (not that ugly; rather slow; needs some parsing, because the protocol between erlang and date will be textual)
  3. port driver or erl_interface to C using its standard library (not ugly at all; but I didn't find ready to use solution and I'm not that good at C to write one)

The ideal solution would be something written in Erlang using OS timezone info, but I didn't find any.

Now I'm stuck to solution 2 (open_port to date util). Is there a better way?

Thanks in advance.

P. S. There was a similar issue, but no good answer there Time zone list issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-11-03 05:22:43

port_helper.erl

-module(port_helper).
-export([get_stdout/1]).
get_stdout(Port) ->
    loop(Port, []).
loop(Port, DataAcc) ->
    receive
        {Port, {data, Data}} ->
            loop(Port, DataAcc ++ Data);
        {Port, eof} ->
            DataAcc
    end.

timestamp_with_time_zone.erl

-module(timestamp_with_time_zone).
-export([to_time_zone/2, to_universal_time/1, modify/2]).
to_time_zone({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}, OutputTimeZone) ->
    InputPattern = "~4.10.0B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B",
    InputDeep = io_lib:format(InputPattern, [Year, Month, Day, Hour, Minute, Second]),
    Input = lists:flatten(InputDeep),
    {external_date(Input, TimeZone, OutputTimeZone), OutputTimeZone}.
to_universal_time({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}) ->
    {Timestamp, "UTC"} = to_time_zone({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}, "UTC"),
    Timestamp.
modify({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}, {Times, Unit}) ->
    if
        Times > 0 ->
            TimesModifier = "";
        Times < 0 ->
            TimesModifier = " ago"
    end,
    InputPattern = "~4.10.0B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B ~.10B ~s~s",
    InputDeep = io_lib:format(InputPattern, [Year, Month, Day, Hour, Minute, Second, abs(Times), Unit, TimesModifier]),
    Input = lists:flatten(InputDeep),
    external_date(Input, TimeZone, TimeZone).

external_date(Input, InputTimeZone, OutputTimeZone) ->
    CmdPattern = "date --date 'TZ=\"~s\" ~s' +%Y%m%d%H%M%S",
    CmdDeep = io_lib:format(CmdPattern, [InputTimeZone, Input]),
    Cmd = lists:flatten(CmdDeep),
    Port = open_port({spawn, Cmd}, [{env, [{"TZ", OutputTimeZone}]}, eof, stderr_to_stdout]),
    ResultString = port_helper:get_stdout(Port),
    case io_lib:fread("~4d~2d~2d~2d~2d~2d", ResultString) of
        {ok, [YearNew, MonthNew, DayNew, HourNew, MinuteNew, SecondNew], _LeftOverChars} ->
            {{YearNew, MonthNew, DayNew}, {HourNew, MinuteNew, SecondNew}}
    end.

port_helper.erl

-module(port_helper).
-export([get_stdout/1]).
get_stdout(Port) ->
    loop(Port, []).
loop(Port, DataAcc) ->
    receive
        {Port, {data, Data}} ->
            loop(Port, DataAcc ++ Data);
        {Port, eof} ->
            DataAcc
    end.

timestamp_with_time_zone.erl

-module(timestamp_with_time_zone).
-export([to_time_zone/2, to_universal_time/1, modify/2]).
to_time_zone({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}, OutputTimeZone) ->
    InputPattern = "~4.10.0B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B",
    InputDeep = io_lib:format(InputPattern, [Year, Month, Day, Hour, Minute, Second]),
    Input = lists:flatten(InputDeep),
    {external_date(Input, TimeZone, OutputTimeZone), OutputTimeZone}.
to_universal_time({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}) ->
    {Timestamp, "UTC"} = to_time_zone({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}, "UTC"),
    Timestamp.
modify({{{Year, Month, Day}, {Hour, Minute, Second}}, TimeZone}, {Times, Unit}) ->
    if
        Times > 0 ->
            TimesModifier = "";
        Times < 0 ->
            TimesModifier = " ago"
    end,
    InputPattern = "~4.10.0B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B ~.10B ~s~s",
    InputDeep = io_lib:format(InputPattern, [Year, Month, Day, Hour, Minute, Second, abs(Times), Unit, TimesModifier]),
    Input = lists:flatten(InputDeep),
    external_date(Input, TimeZone, TimeZone).

external_date(Input, InputTimeZone, OutputTimeZone) ->
    CmdPattern = "date --date 'TZ=\"~s\" ~s' +%Y%m%d%H%M%S",
    CmdDeep = io_lib:format(CmdPattern, [InputTimeZone, Input]),
    Cmd = lists:flatten(CmdDeep),
    Port = open_port({spawn, Cmd}, [{env, [{"TZ", OutputTimeZone}]}, eof, stderr_to_stdout]),
    ResultString = port_helper:get_stdout(Port),
    case io_lib:fread("~4d~2d~2d~2d~2d~2d", ResultString) of
        {ok, [YearNew, MonthNew, DayNew, HourNew, MinuteNew, SecondNew], _LeftOverChars} ->
            {{YearNew, MonthNew, DayNew}, {HourNew, MinuteNew, SecondNew}}
    end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文