我有一个模块,我在ETS表中存储了一些数据,现在我试图过滤数据,迭代ETS表,但总是得到空列表。(这是每次匹配-匹配(‘$end_of_table’,-> ) 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表?
发布于 2017-01-12 20:21:41
变量名Unix和Unix2表明您存储的是Unix时间戳,即自1970年以来经过的秒数,但函数calendar:datetime_to_gregorian_seconds返回自0年以来经过的秒数。(请参阅documentation。)因此,您的比较Unix2 >= FromSec, Unix2 =< ToSec将始终为false。
日历模块使用偏移量?DAYS_FROM_0_TO_1970 * ?SECONDS_PER_DAY在两者之间进行转换,宏定义如下:
-define(SECONDS_PER_DAY, 86400).
-define(DAYS_FROM_0_TO_1970, 719528).发布于 2017-01-12 20:28:42
找到答案了!
unixtime和calendar:datetime_to_gregorian_seconds({{2017,1,12},{11,00,00})的主要区别
因此,它将所有内容都匹配到匹配(‘$end_of_table’,-> ) Acc;
https://stackoverflow.com/questions/41612597
复制相似问题