0

我有一些领域的模型。我想从 mongodb 获取字符串字段并获取子字符串。我正在尝试:我的 get_items 代码。重要的是只有一行带有过滤器。

get_items('GET', []) ->
Items = boss_db:find(item, []),
S = Req:query_param("start"),
F = Req:query_param("finish"),
Start = string_to_datetime(S),
Finish = string_to_datetime(F),
Fitems = filter(Items, Start, Finish, []),
{json, [{items, Fitems}]}.

create_item 代码。我的日期时间使用我自己的功能。

create_item('POST', []) ->
Lastname = Req:post_param("lastname"),
User = hd(boss_db:find(consumer, [{lastname, Lastname}])),
DateTime = datetime_to_string(calendar:now_to_local_time(erlang:now())),
Type = Req:post_param("type"),
IdType = Req:post_param("idtype"),
Item = item:new(id, User:id(), DateTime, Type, IdType),
case Item:save() of
    {ok, SavedItem} -> {json, [{status, "ok"}]};
    {error, Reason} -> {json, [{error, Reason}]}
end.

我将日期时间转换为字符串的功能。

datetime_to_string({{YY, MM, DD}, {Hour, Min, Sec}}) -> 
io_lib:format("~4..0w-~2..0w-~2..0w ~2..0w:~2..0w:~2..0w",
    [YY, MM, DD, Hour, Min, Sec]).

我将字符串转换为日期时间的函数。

string_to_datetime(S) ->
{YY, _} = string:to_integer(string:substr(S, 1, 4)),
{MM, _} = string:to_integer(string:substr(S, 6, 2)),
{DD, _} = string:to_integer(string:substr(S, 9, 2)),
{Hour, _} = string:to_integer(string:substr(S, 12, 2)),
{Min, _} = string:to_integer(string:substr(S, 15, 2)),
{Sec, _} = string:to_integer(string:substr(S, 18, 2)),
{{YY, MM, DD}, {Hour, Min, Sec}}.

我的过滤方法。

filter([], Start, Finish, Acc) -> Acc;
filter([Model|Models], Start, Finish, Acc) ->
DateTime = string_to_datetime(Model:datetime()),
{D1, _} = calendar:time_difference(Start, DateTime),
{D2, _} = calendar:time_difference(DateTime, Finish),
if
    D1 >= 0, D2 >= 0 -> filter(Models, Start, Finish, [Model | Acc]);
    true -> filter(Models, Start, Finish, Acc)
end.

我有错误:

{function_clause,
    [{string,substr2,
         [<<"2013-12-17 18:36:42">>,1],
         [{file,"string.erl"},{line,213}]},
     {string,substr,3,[{file,"string.erl"},{line,208}]},
     {cb_tracker_main_controller,string_to_datetime,2,

请帮我。

4

2 回答 2

1

首先,您可以尝试使用tempo lib ( https://github.com/selectel/tempo ) 来解析和格式化日期。

你的问题呢: string:substr 适用于字符串- 整数列表。<<"2013-12-17 18:36:42">> - 二进制,您需要在处理之前使用 binary_to_list 函数将其转换为字符串。

于 2013-12-17T15:08:14.130 回答
0

好的,所以错误来自您的功能

string_to_datetime(S) ->
    {YY, _} = string:to_integer(string:substr(S, 1, 4)),
    {MM, _} = string:to_integer(string:substr(S, 6, 2)),
    {DD, _} = string:to_integer(string:substr(S, 9, 2)),
    {Hour, _} = string:to_integer(string:substr(S, 12, 2)),
    {Min, _} = string:to_integer(string:substr(S, 15, 2)),
    {Sec, _} = string:to_integer(string:substr(S, 18, 2)),
    {{YY, MM, DD}, {Hour, Min, Sec}}.

但是鉴于您的编码功能

datetime_to_string({{YY, MM, DD}, {Hour, Min, Sec}}) -> 
    io_lib:format("~4..0w-~2..0w-~2..0w ~2..0w:~2..0w:~2..0w",
        [YY, MM, DD, Hour, Min, Sec]).

可以使用模式匹配来实现解码器(将其用于二进制文件很简单,但语法上很尴尬):

string_to_datetime (S) ->
    [Y1,Y2,Y3,Y4, $-, M1,M2, $-, D1,D2, $ , H1,H2, $:, N1,N2, $:, S1,S2] = S,
    { {list_to_integer([Y1,Y2,Y3,Y4]),list_to_integer([M1,M2]),list_to_integer([D1,D2])}
    , {list_to_integer([H1,H2]),      list_to_integer([N1,N2]),list_to_integer([S1,S2])} }.

(代码仍未测试)

于 2013-12-17T21:46:17.903 回答