【问题标题】:How to filter ETS table without ets:select如何在没有 ets:select 的情况下过滤 ETS 表
【发布时间】:2017-05-27 12:37:33
【问题描述】:

我有在 ETS 表中存储一些数据的模块 现在我正在尝试过滤迭代 ETS 表的数据,但总是变空 [List]。 (这个每次都匹配——matching('$end_of_table', Acc) -> Acc;)

-module(t).
-export([matching/0]).


matching() -> matching(ets:first(auth), []).
matching('$end_of_table', Acc) -> Acc;
matching(Key, Acc) ->
            FromSec = calendar:datetime_to_gregorian_seconds({{2017,1,12}, {11,00,00}}),
            ToSec = calendar:datetime_to_gregorian_seconds({{2017,1,12}, {12,00,00}}), 
    case ets:lookup(auth, Key) of
  [{Login, Pass, TTL, Unix, Unix2}] when Unix2 >= FromSec, Unix2 =< ToSec -> NewAcc = [{Login, Pass, TTL, Unix, Unix2}|Acc],
            N = ets:next(auth, Key),
                    matching(N, NewAcc);
  _ -> N = ets:next(auth, Key),
                    matching(N, Acc)
         end.

可能是我错误地创建了 ETS 表吗?

【问题讨论】:

    标签: erlang erlang-otp ets


    【解决方案1】:

    找到答案了!

    unixtime 和 calendar:datetime_to_gregorian_seconds({{2017,1,12}, {11,00,00}}) 不同

    所以一切都匹配到matching('$end_of_table', Acc) -> Acc;

    【讨论】:

      【解决方案2】:

      变量名称UnixUnix2 表明您正在存储Unix 时间戳,即自1970 年以来经过的秒数,但函数calendar:datetime_to_gregorian_seconds 返回自0 年以来经过的秒数。(请参阅documentation。)因此您的比较 Unix2 &gt;= FromSec, Unix2 =&lt; ToSec 总是错误的。

      日历模块使用偏移量?DAYS_FROM_0_TO_1970 * ?SECONDS_PER_DAY在两者之间进行转换,宏定义为:

      -define(SECONDS_PER_DAY, 86400).
      -define(DAYS_FROM_0_TO_1970, 719528).
      

      参见例如the implementation of calendar:now_to_datetime/1

      【讨论】:

      • 是的。刚把这个整理出来。并发布答案。谢谢你的回答
      猜你喜欢
      • 2019-06-10
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多