C
C
cnaize2013-12-17 17:50:11
Erlang
cnaize, 2013-12-17 17:50:11

Error in string:substring in date from base?

I have this code:

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('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.

And I get this error:
{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,

Please help, I'm in so much pain. At least to know in which direction to dig.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pfi79, 2014-01-03
@pfi79

You use string:substr, which expects a list as input, and you feed a binary string as input - <<"2013-12-17 18:36:42">>.
Use binary pattern matching:
Or maybe you should not use lists in your case, maybe binaries are better?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question