如何在没有 ets:select 的情况下过滤 ETS table

How to filter ETS table without ets:select

我有在 ETS 中存储一些数据的模块 table 现在我正在尝试过滤迭代 ETS table 的数据,但总是变得空 [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 table 不正确?

变量名 UnixUnix2 表明您正在存储 Unix 时间戳,即自 1970 年以来经过的秒数,但是函数 calendar:datetime_to_gregorian_seconds returns自 0 年以来经过的秒数。(请参阅 documentation。)因此您的比较 Unix2 >= FromSec, Unix2 =< 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

找到答案了!

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

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