Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Date Conversion
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Richard O'Keefe  
View profile  
 More options Nov 14 2012, 5:04 pm
From: "Richard O'Keefe" <o...@cs.otago.ac.nz>
Date: Thu, 15 Nov 2012 11:04:13 +1300
Local: Wed, Nov 14 2012 5:04 pm
Subject: Re: [erlang-questions] Date Conversion

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.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.