[erlang-questions] Date Conversion

31 views
Skip to first unread message

Lucky Khoza

unread,
Nov 14, 2012, 4:26:05 AM11/14/12
to erlang-q...@erlang.org
Hi Erlang Developers,

How do i convert date string: "2012/02/15" to 2012/2/15, just to get rid of trailing Zero.


Kindest Regards
Lucky

Artem Teslenko

unread,
Nov 14, 2012, 4:46:31 AM11/14/12
to Lucky Khoza, erlang-q...@erlang.org
the dirty way:

1> string:join([integer_to_list(list_to_integer(N)) || N <- string:tokens("2012/02/15", "/")], "/").
"2012/2/15"
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Ian

unread,
Nov 14, 2012, 4:55:17 AM11/14/12
to erlang-q...@erlang.org
On 14/11/2012 09:26, Lucky Khoza wrote:
Hi Erlang Developers,

How do i convert date string: "2012/02/15" to 2012/2/15, just to get rid of trailing Zero.

Lucky, this is at least the third trivial problem you asked in recent days.

Programming is the challenge of discovering how to solve a continuous series of new problems. If you don't think you would like to do that, find something else to do. If you do, go to it.

How I would approach it:

1) Define the problem ACCURATELY - its leading zeroes you want to remove.

2) Find three ways to solve the problem.
e.g.
  a) Split it up in to its parts, chop off leading zeros, reassemble.
  b) Its a date, so convert to binary date and convert back to formatted date using library functions.
  c) Parse it to remove any zero that occurs after a slash.

3) Choose the best, implement it and test it properly.

Regards

Ian

Thomas Allen

unread,
Nov 14, 2012, 10:26:43 AM11/14/12
to Lucky Khoza, erlang-q...@erlang.org
On Wed, Nov 14, 2012 at 11:26:05AM +0200, Lucky Khoza wrote:
> Hi Erlang Developers,
>
> How do i convert date string: "2012/02/15" to 2012/2/15, just to get rid of
> trailing Zero.

The obligatory regular expression-based solution. Some might find this simple
look-behind assertion more direct than splitting and converting the components.

1> binary:list_to_bin(re:replace("2012/02/15", "(?<=/)0", "", [global])).
<<"2012/2/15">>

Thomas

Wes James

unread,
Nov 14, 2012, 11:12:38 AM11/14/12
to Lucky Khoza, erlang-q...@erlang.org
On Wed, Nov 14, 2012 at 2:26 AM, Lucky Khoza <mrk...@gmail.com> wrote:
Hi Erlang Developers,

How do i convert date string: "2012/02/15" to 2012/2/15, just to get rid of trailing Zero.




Some other examples:

-module(df).

-export([s/0]).

s() ->
 Date = "2012/05/01",
 DateS=string:tokens(Date,"/"),
 io:format("~n~s~n", [rmz(DateS)]),
 io:format("~n~s~n", [rmz2(DateS)]).

rmz([Year, Month, Day]) ->
 Year++"/"++chopz(Month)++"/"++chopz(Day).

rmz2(Date) ->
 io_lib:format("~n~s/~s/~s~n", Date).

chopz("0"++Num) ->
 Num;
chopz(Num) ->
 Num. 

Richard O'Keefe

unread,
Nov 14, 2012, 5:04:13 PM11/14/12
to Lucky Khoza, erlang-q...@erlang.org

On 14/11/2012, at 10:26 PM, Lucky Khoza wrote:

> Hi Erlang Developers,
>
> How do i convert date string: "2012/02/15" to 2012/2/15, just to get rid of trailing Zero.

(1) There is no trailing zero there.
(2) Date standards like ISO 8601 often *require* the leading zero.
(3)
{ok,[Y,M,D],[]} = io_lib:fread("~d/~d/~d", "2012/02/15"),
lists:flatten(io_lib:fwrite("~w/~w/~w", [Y,M,D]))
If you just want to write the result, you can use io:fwrite/[2,3]
instead of io_lib:fwrite/2.

In one sense, Erlang resembles C: formatted input and formatted
output use formats that _look_ similar at first glance but are
really quite different.

Or you could think in terms of a finite state automaton
with two fairly obvious states.

strip_leading_zeros([$0|Cs]) ->
strip_leading_zeros(Cs);
strip_leading_zeros([D|Cs]) when D =< $9, D >= $1 ->
[D|after_leading_zeros(Cs)];
strip_leading_zeros([C|Cs]) ->
[$0,C|strip_leading_zeros(Cs)];
strip_leading_zeros([]) ->
"0".

after_leading_zeros([D|Cs]) when D =< $9, D >= $0 ->
[D|after_leading_zeros(Cs)];
after_leading_zeros([C|Cs]) ->
[C|strip_leading_zeros(Cs)];
after_leading_zeros([]) ->
"".

Basically, in a block of digits, leading zeros are removed,
except that if all digits in the block are zero, one zero
remains. Non-digit characters are copied and separate blocks.

The version with the finite state automaton is almost
certainly more economical, but the version using formatting
is probably easier to think of and easier to debug.
Reply all
Reply to author
Forward
0 new messages