Added function parse_to_local_datetime/1

To utilize parse/1 but to give you {date(), time()} in the local timezone
This commit is contained in:
Stuart Thackray 2016-07-02 14:34:54 +02:00
parent 6bbdfe6c41
commit a539c15aaa
3 changed files with 219 additions and 147 deletions

18
.gitignore vendored Normal file
View File

@ -0,0 +1,18 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
*.log
erl_crash.dump
.rebar
_build
.project
.settings
rebar3.crashdump
rebar.lock

44
README.md Normal file → Executable file
View File

@ -2,7 +2,49 @@
parse and format rfc3339 strings in elixir and erlang
## Installing as a rebar depedancy (erlang)
You install it as a rebar dependency by adding the following in the deps section of rebar.config
```erlang
{rfc3339, {git, "git://github.com/talentdeficit/rfc3339.git", {branch, master}}}
```
## building
```bash
rebar3 compile
```
## running
```bash
rebar3 shell
```
## erlang usage
###### parse_to_local_datetime
```erlang
> rfc3339:parse_to_local_datetime(<<"1996-12-19T16:39:57-08:00">>).
{{1996,12,19},{10,39,57}}
```
###### parse
```erlang
> rfc3339:parse(<<"1937-01-01T12:00:27.87+00:20">>).
{ok,{{1937,1,1},{12,0,27},870000,20}}
```
###### format
```erlang
> rfc3339:format({date(), time()}).
{ok,<<"2016-07-02T14:04:39Z">>}
```
## todo
* everything
-[ ] everything

View File

@ -1,6 +1,7 @@
% vim: sw=4 ts=4 et ft=erlang
-module(rfc3339).
-export([parse/1]).
-export([parse/1,parse_to_local_datetime/1]).
-export([format/1, format/2]).
-export([to_map/1]).
-export([to_time/1, to_time/2]).
@ -25,6 +26,17 @@
-type error() :: badarg | baddate | badtime | badyear | badday | badhour | badminute | badsecond | badusec | badtimezone.
%% -spec parse_to_local_datetime(binary()) -> {date(), time()}
parse_to_local_datetime(Bin) ->
{ok, {Date, Time, _, TZ}} = parse(Bin),
TZSecs = calendar:datetime_to_gregorian_seconds({Date, Time}),
UTCDateTime = calendar:gregorian_seconds_to_datetime(case TZ of
_ when is_integer(TZ) ->
TZSecs + (60*TZ);
_ ->
TZSecs
end),
calendar:universal_time_to_local_time(UTCDateTime).
-spec parse(binary()) -> {ok, {date(), time(), usec(), tz()}} | {error, error()}.
parse(Bin) when is_binary(Bin) -> date(Bin, {undefined, undefined, undefined, undefined});