mirror of
https://github.com/valitydev/rfc3339.git
synced 2024-11-06 00:25:16 +00:00
initial commit
This commit is contained in:
commit
5284ae23b6
14
.travis.yml
Normal file
14
.travis.yml
Normal file
@ -0,0 +1,14 @@
|
||||
language: elixir
|
||||
elixir:
|
||||
- 1.2.1
|
||||
otp_release:
|
||||
- 18.2
|
||||
matrix:
|
||||
include:
|
||||
- elixir: 1.0.5
|
||||
otp_release: 17.5
|
||||
- elixir: 1.1.0
|
||||
otp_release: 17.5
|
||||
notifications:
|
||||
recipients:
|
||||
- alisdairsullivan@yahoo.ca
|
201
LICENSE-APACHE
Normal file
201
LICENSE-APACHE
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2016 alisdair sullivan
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
9
LICENSE-MIT
Normal file
9
LICENSE-MIT
Normal file
@ -0,0 +1,9 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 alisdair sullivan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# iso8601
|
||||
|
||||
parse and format iso8601 strings in elixir and erlang
|
45
lib/rfc3339.ex
Normal file
45
lib/rfc3339.ex
Normal file
@ -0,0 +1,45 @@
|
||||
defmodule RFC3339 do
|
||||
defstruct year: nil, month: nil, day: nil,
|
||||
hour: nil, min: nil, sec: nil, usec: nil,
|
||||
tz_offset: nil
|
||||
|
||||
def parse(dt) do
|
||||
case :rfc3339.parse(dt) do
|
||||
{:ok, {date, time, tz}} ->
|
||||
dt = %RFC3339{}
|
||||
dt = insert_date(dt, date)
|
||||
dt = insert_time(dt, time)
|
||||
insert_tz(dt, tz)
|
||||
{:error, error} ->
|
||||
RFC3339.Error.from_erl(error)
|
||||
end
|
||||
end
|
||||
|
||||
def format(dt), do: :rfc3339.format(dt)
|
||||
|
||||
defp insert_date(dt, {year, month, day}) do
|
||||
%{ dt | :year => year, :month => month, :day => day }
|
||||
end
|
||||
defp insert_date(dt, :undefined), do: dt
|
||||
|
||||
defp insert_time(dt, {hour, min, sec, :undefined}) do
|
||||
%{ dt | :hour => hour, :min => min, :sec => sec }
|
||||
end
|
||||
defp insert_time(dt, {hour, min, sec, usec}) do
|
||||
%{ dt | :hour => hour, :min => min, :sec => sec, :usec => usec }
|
||||
end
|
||||
defp insert_time(dt, :undefined), do: dt
|
||||
|
||||
defp insert_tz(dt, :undefined), do: dt
|
||||
defp insert_tz(dt, offset), do: %{ dt | :tz_offset => offset }
|
||||
end
|
||||
|
||||
defmodule RFC3339.Error do
|
||||
defstruct message: nil, reason: nil
|
||||
|
||||
def from_erl(error) do
|
||||
%RFC3339.Error{message: error_to_message(error), reason: error}
|
||||
end
|
||||
|
||||
defp error_to_message(_), do: ""
|
||||
end
|
35
mix.exs
Normal file
35
mix.exs
Normal file
@ -0,0 +1,35 @@
|
||||
defmodule RFC3339.Mixfile do
|
||||
use Mix.Project
|
||||
|
||||
@version "0.0.1"
|
||||
|
||||
def project do
|
||||
[app: :rfc3339,
|
||||
version: @version,
|
||||
description: description,
|
||||
elixir: "~> 1.2",
|
||||
package: package,
|
||||
build_embedded: Mix.env == :prod,
|
||||
start_permanent: Mix.env == :prod,
|
||||
deps: deps]
|
||||
end
|
||||
|
||||
def application do
|
||||
[applications: []]
|
||||
end
|
||||
|
||||
defp description do
|
||||
"an rfc3339 parser and formatter"
|
||||
end
|
||||
|
||||
defp package do
|
||||
[maintainers: ["alisdair sullivan"],
|
||||
licenses: ["Apache 2.0", "MIT"],
|
||||
links: %{"Github" => "https://github.com/talentdeficit/rfc3339"},
|
||||
files: ["src", "lib", "README.md", "LICENSE*", "mix.exs", "rebar.config"]]
|
||||
end
|
||||
|
||||
defp deps do
|
||||
[]
|
||||
end
|
||||
end
|
0
rebar.config
Normal file
0
rebar.config
Normal file
1
rebar.lock
Normal file
1
rebar.lock
Normal file
@ -0,0 +1 @@
|
||||
[].
|
12
src/rfc3339.app.src
Normal file
12
src/rfc3339.app.src
Normal file
@ -0,0 +1,12 @@
|
||||
{application, rfc3339,
|
||||
[
|
||||
{description, "an rfc3339 parser and formatter"},
|
||||
{vsn, "0.0.1"},
|
||||
{modules, [rfc3339]},
|
||||
{registered, []},
|
||||
{applications, [
|
||||
kernel,
|
||||
stdlib
|
||||
]},
|
||||
{env, []}
|
||||
]}.
|
254
src/rfc3339.erl
Normal file
254
src/rfc3339.erl
Normal file
@ -0,0 +1,254 @@
|
||||
-module(rfc3339).
|
||||
|
||||
-export([parse/1]).
|
||||
-export([format/1]).
|
||||
-export([to_tuple/1]).
|
||||
-export([to_map/1]).
|
||||
-export_type([datetime/0, date/0, time/0, tz/0]).
|
||||
-export_type([year/0, month/0, day/0]).
|
||||
-export_type([hour/0, min/0, sec/0, usec/0]).
|
||||
-export_type([tz_offset/0]).
|
||||
-export_type([(error/0)]).
|
||||
|
||||
-type datetime() :: {date(), time(), tz()}.
|
||||
|
||||
-type date() :: {year(), month(), day()}.
|
||||
-type year() :: 0..9999.
|
||||
-type month() :: 0..12.
|
||||
-type day() :: 0..31.
|
||||
|
||||
-type time() :: {hour(), min(), sec(), usec()}.
|
||||
-type hour() :: 0..24.
|
||||
-type min() :: 0..59.
|
||||
-type sec() :: 0..60.
|
||||
-type usec() :: 0..999999.
|
||||
|
||||
-type tz() :: tz_offset().
|
||||
-type tz_offset() :: -1439..1439.
|
||||
|
||||
-type error() :: map().
|
||||
|
||||
|
||||
-spec parse(binary()) -> {ok, datetime()} | {error, error()}.
|
||||
parse(Bin) -> time_or_date(Bin, {undefined, undefined, undefined}).
|
||||
|
||||
-spec format(map() | datetime()) -> {ok, binary()} | {error, error()}.
|
||||
format({{Year, Month, Day}, {Hour, Min, Sec, USec}, Offset}) ->
|
||||
DT = #{year => Year, month => Month, day => Day,
|
||||
hour => Hour, min => Min, sec => Sec, usec => USec,
|
||||
tz_offset => Offset},
|
||||
format(DT);
|
||||
format(DT) ->
|
||||
Date = format_date(DT),
|
||||
Time = format_time(DT),
|
||||
format(Date, Time).
|
||||
|
||||
-spec to_tuple(binary()) -> {ok, datetime()} | {error, error()}.
|
||||
to_tuple(Bin) -> parse(Bin).
|
||||
|
||||
-spec to_map(binary()) -> {ok, map()} | {error, error()}.
|
||||
to_map(Bin) ->
|
||||
case parse(Bin) of
|
||||
{ok, {Date, Time, Offset}} ->
|
||||
mapify(Date, Time, Offset, #{});
|
||||
{error, Error} -> {error, Error}
|
||||
end.
|
||||
|
||||
mapify(undefined, Time, Offset, Result) -> mapify(Time, Offset, Result);
|
||||
mapify({Year, Month, Day}, Time, Offset, Result) ->
|
||||
mapify(Time, Offset, maps:merge(Result, #{year => Year, month => Month, day => Day})).
|
||||
|
||||
mapify(undefined, _, Result) -> Result;
|
||||
mapify({Hour, Min, Sec, undefined}, Offset, Result) ->
|
||||
mapify(Offset, maps:merge(Result, #{hour => Hour, min => Min, sec => Sec}));
|
||||
mapify({Hour, Min, Sec, USec}, Offset, Result) ->
|
||||
mapify(Offset, maps:merge(Result, #{hour => Hour, min => Min, sec => Sec, usec => USec})).
|
||||
|
||||
mapify(undefined, Result) -> Result;
|
||||
mapify(Offset, Result) ->
|
||||
maps:merge(Result, #{tz_offset => Offset}).
|
||||
|
||||
time_or_date(<<H1, H2, $:, M1, M2, $:, S1, S2, Rest/binary>>, _Result) ->
|
||||
Hour = to_hour(H1, H2),
|
||||
Min = to_minute(M1, M2),
|
||||
Sec = to_second(S1, S2),
|
||||
usec_or_tz(Rest, {undefined, {Hour, Min, Sec, undefined}, undefined});
|
||||
time_or_date(<<Y1, Y2, Y3, Y4, $-, M1, M2, $-, D1, D2, Rest/binary>>, _Result) ->
|
||||
Year = to_year(Y1, Y2, Y3, Y4),
|
||||
Month = to_month(M1, M2),
|
||||
Day = to_day(D1, D2, Year, Month),
|
||||
time_or_end(Rest, {{Year, Month, Day}, undefined, undefined});
|
||||
time_or_date(_, _Result) ->
|
||||
{error, badarg}.
|
||||
|
||||
|
||||
usec_or_tz(<<$., Rest/binary>>, Result) ->
|
||||
usec(Rest, Result, 0, 100000);
|
||||
usec_or_tz(Rest, Result) -> tz(Rest, Result).
|
||||
|
||||
%% next two clauses burn off fractional seconds beyond microsecond precision
|
||||
usec(<<X, Rest/binary>>, Result, undefined, undefined)
|
||||
when X >= $0 andalso X =< $9 ->
|
||||
usec(Rest, Result, undefined, undefined);
|
||||
usec(Bin, Result, undefined, undefined) ->
|
||||
tz(Bin, Result);
|
||||
%% keep a running acc of usecs
|
||||
usec(<<X, Rest/binary>>, Result, Acc, Multiplier)
|
||||
when X >= $0 andalso X =< $9 ->
|
||||
try list_to_integer([X]) of
|
||||
N -> usec(Rest, Result, Acc + (N * Multiplier), Multiplier div 10)
|
||||
catch
|
||||
error:badarg -> {error, badusec}
|
||||
end;
|
||||
%% not a digit, insert usecs into time and proceed to tz
|
||||
usec(Bin, {Date, {Hour, Min, Sec, undefined}, undefined}, Acc, _) ->
|
||||
tz(Bin, {Date, {Hour, Min, Sec, Acc}, undefined});
|
||||
usec(_, _, _, _) -> {error, badusec}.
|
||||
|
||||
tz(<<$+, H1, H2, $:, M1, M2>>, {Date, Time, undefined}) ->
|
||||
Hour = to_hour(H1, H2),
|
||||
Min = to_minute(M1, M2),
|
||||
{ok, {Date, Time, (Hour * 60) + Min}};
|
||||
tz(<<$-, H1, H2, $:, M1, M2>>, {Date, Time, undefined}) ->
|
||||
Hour = to_hour(H1, H2),
|
||||
Min = to_minute(M1, M2),
|
||||
{ok, {Date, Time, (Hour * -60) - Min}};
|
||||
tz(<<$Z>>, Result) ->
|
||||
{ok, Result};
|
||||
tz(<<$z>>, Result) ->
|
||||
{ok, Result};
|
||||
tz(_, _) -> {error, badtimezone}.
|
||||
|
||||
%% space
|
||||
time_or_end(<<16#20, H1, H2, $:, M1, M2, $:, S1, S2, Rest/binary>>, {Date, undefined, undefined}) ->
|
||||
Hour = to_hour(H1, H2),
|
||||
Min = to_minute(M1, M2),
|
||||
Sec = to_second(S1, S2),
|
||||
usec_or_tz(Rest, {Date, {Hour, Min, Sec, undefined}, undefined});
|
||||
time_or_end(<<$t, H1, H2, $:, M1, M2, $:, S1, S2, Rest/binary>>, {Date, undefined, undefined}) ->
|
||||
Hour = to_hour(H1, H2),
|
||||
Min = to_minute(M1, M2),
|
||||
Sec = to_second(S1, S2),
|
||||
usec_or_tz(Rest, {Date, {Hour, Min, Sec, undefined}, undefined});
|
||||
time_or_end(<<$T, H1, H2, $:, M1, M2, $:, S1, S2, Rest/binary>>, {Date, undefined, undefined}) ->
|
||||
Hour = to_hour(H1, H2),
|
||||
Min = to_minute(M1, M2),
|
||||
Sec = to_second(S1, S2),
|
||||
usec_or_tz(Rest, {Date, {Hour, Min, Sec, undefined}, undefined});
|
||||
time_or_end(<<>>, Result) -> {ok, Result};
|
||||
time_or_end(_, _) -> {error, badtime}.
|
||||
|
||||
to_hour(H1, H2) ->
|
||||
try list_to_integer([H1, H2]) of
|
||||
X when X >= 0 andalso X =< 23 -> X;
|
||||
_ -> {error, badhour}
|
||||
catch
|
||||
error:badarg -> {error, badhour}
|
||||
end.
|
||||
|
||||
to_minute(M1, M2) ->
|
||||
try list_to_integer([M1, M2]) of
|
||||
X when X >= 0 andalso X =< 59 -> X;
|
||||
_ -> {error, badminute}
|
||||
catch
|
||||
error:badarg -> {error, badminute}
|
||||
end.
|
||||
|
||||
to_second(S1, S2) ->
|
||||
try list_to_integer([S1, S2]) of
|
||||
X when X >= 0 andalso X =< 60 -> X;
|
||||
_ -> {error, badsecond}
|
||||
catch
|
||||
error:badarg -> {error, badsecond}
|
||||
end.
|
||||
|
||||
to_year(Y1, Y2, Y3, Y4) ->
|
||||
try list_to_integer([Y1, Y2, Y3, Y4]) of
|
||||
X when X >= 0 andalso X =< 9999 -> X;
|
||||
_ -> {error, badyear}
|
||||
catch
|
||||
error:badarg -> {error, badyear}
|
||||
end.
|
||||
|
||||
to_month(M1, M2) ->
|
||||
try list_to_integer([M1, M2]) of
|
||||
X when X >= 1 andalso X =< 12 -> X;
|
||||
_ -> {error, badmonth}
|
||||
catch
|
||||
error:badarg -> {error, badmonth}
|
||||
end.
|
||||
|
||||
to_day(D1, D2, Year, Month) ->
|
||||
try list_to_integer([D1, D2]) of
|
||||
X when X >= 0 andalso X =< 28 -> X;
|
||||
X when X >= 29 andalso X =< 31 ->
|
||||
case day_in_month(Year, Month, X) of
|
||||
true -> X;
|
||||
false -> {error, badday}
|
||||
end;
|
||||
_ -> {error, badday}
|
||||
catch
|
||||
error:badarg -> {error, badday}
|
||||
end.
|
||||
|
||||
day_in_month({error, badyear}, _, _) -> {error, badyear};
|
||||
day_in_month(_, {error, badmonth}, _) -> {error, badmonth};
|
||||
day_in_month(Year, Month, Day) ->
|
||||
case Day of
|
||||
29 when Month == 2 -> ((Year rem 4 == 0) andalso not (Year rem 100 == 0)) orelse (Year rem 400 == 0);
|
||||
30 when Month == 2 -> false;
|
||||
31 when Month == 2; Month == 4; Month == 6; Month == 9; Month == 11 -> false;
|
||||
_ -> true
|
||||
end.
|
||||
|
||||
format_date(DT) ->
|
||||
Year = maps:get(year, DT, undefined),
|
||||
Month = maps:get(month, DT, undefined),
|
||||
Day = maps:get(day, DT, undefined),
|
||||
format_date(Year, Month, Day).
|
||||
|
||||
format_date(Y, M, D) when is_integer(Y), is_integer(M), is_integer(D) ->
|
||||
io_lib:format("~4.10.0B-~2.10.0B-~2.10.0B", [Y, M, D]);
|
||||
format_date(undefined, undefined, undefined) -> {error, nodate};
|
||||
format_date(nil, nil, nil) -> {error, nodate};
|
||||
format_date(_, _, _) -> {error, baddate}.
|
||||
|
||||
format_time(DT) ->
|
||||
Hour = maps:get(hour, DT, undefined),
|
||||
Min = maps:get(min, DT, undefined),
|
||||
Sec = maps:get(sec, DT, undefined),
|
||||
USec = maps:get(usec, DT, undefined),
|
||||
Time = format_time(Hour, Min, Sec, USec),
|
||||
Offset = maps:get(tz_offset, DT, undefined),
|
||||
TZ = format_offset(Offset),
|
||||
format_time(Time, TZ).
|
||||
|
||||
format_time(H, M, S, undefined) when is_integer(H), is_integer(M), is_integer(S) ->
|
||||
io_lib:format("~2.10.0B:~2.10.0B:~2.10.0B", [H, M, S]);
|
||||
format_time(H, M, S, nil) when is_integer(H), is_integer(M), is_integer(S) ->
|
||||
io_lib:format("~2.10.0B:~2.10.0B:~2.10.0B", [H, M, S]);
|
||||
format_time(H, M, S, U) when is_integer(H), is_integer(M), is_integer(S), is_integer(U) ->
|
||||
SU = (S / 1) + (U / 1000000),
|
||||
io_lib:format("~2.10.0B:~2.10.0B:~9.6.0f", [H, M, SU]);
|
||||
format_time(undefined, undefined, undefined, undefined) -> {error, notime};
|
||||
format_time(nil, nil, nil, nil) -> {error, notime};
|
||||
format_time(_, _, _, _) -> {error, badtime}.
|
||||
|
||||
format_offset(undefined) -> "Z";
|
||||
format_offset(nil) -> "Z";
|
||||
format_offset(0) -> "Z";
|
||||
format_offset(M) when is_integer(M) ->
|
||||
Sign = case M >= 0 of true -> "+"; false -> "-" end,
|
||||
Hour = abs(M) div 60,
|
||||
Min = abs(M) rem 60,
|
||||
Sign ++ io_lib:format("~2.10.0B:~2.10.0B", [Hour, Min]).
|
||||
|
||||
format_time({error, Error}, _) -> {error, Error};
|
||||
format_time(Time, TZ) -> [Time, TZ].
|
||||
|
||||
format({error, baddate}, _) -> {error, badarg};
|
||||
format(_, {error, badtime}) -> {error, badtime};
|
||||
format({error, nodate}, {error, notime}) -> {error, badarg};
|
||||
format({error, nodate}, Time) -> unicode:characters_to_binary(Time);
|
||||
format(Date, {error, notime}) -> unicode:characters_to_binary(Date);
|
||||
format(Date, Time) -> unicode:characters_to_binary([Date, "T", Time]).
|
132
test/rfc3339_test.exs
Normal file
132
test/rfc3339_test.exs
Normal file
@ -0,0 +1,132 @@
|
||||
defmodule RFC3339Test do
|
||||
use ExUnit.Case
|
||||
|
||||
test "decode 0000-01-01" do
|
||||
assert %RFC3339{year: 0, month: 1, day: 1} == RFC3339.parse("0000-01-01")
|
||||
end
|
||||
|
||||
test "decode 9999-12-31" do
|
||||
assert %RFC3339{year: 9999, month: 12, day: 31} == RFC3339.parse("9999-12-31")
|
||||
end
|
||||
|
||||
test "decode 1584-03-04" do
|
||||
assert %RFC3339{year: 1584, month: 3, day: 4} == RFC3339.parse("1584-03-04")
|
||||
end
|
||||
|
||||
test "decode 1900-01-01" do
|
||||
assert %RFC3339{year: 1900, month: 1, day: 1} == RFC3339.parse("1900-01-01")
|
||||
end
|
||||
|
||||
test "decode 2016-01-24" do
|
||||
assert %RFC3339{year: 2016, month: 1, day: 24} == RFC3339.parse("2016-01-24")
|
||||
end
|
||||
|
||||
test "decode 00:00:00Z" do
|
||||
assert %RFC3339{hour: 0, min: 0, sec: 0} == RFC3339.parse("00:00:00Z")
|
||||
end
|
||||
|
||||
test "decode 23:59:60Z" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60} == RFC3339.parse("23:59:60Z")
|
||||
end
|
||||
|
||||
test "decode 23:59:60.5Z" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, usec: 500000} == RFC3339.parse("23:59:60.5Z")
|
||||
end
|
||||
|
||||
test "decode 23:59:60.55Z" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, usec: 550000} == RFC3339.parse("23:59:60.55Z")
|
||||
end
|
||||
|
||||
test "decode 23:59:60.555555Z" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, usec: 555555} == RFC3339.parse("23:59:60.555555Z")
|
||||
end
|
||||
|
||||
test "decode 23:59:60.5555554Z" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, usec: 555555} == RFC3339.parse("23:59:60.5555554Z")
|
||||
end
|
||||
|
||||
test "decode 23:59:60.999999Z" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, usec: 999999} == RFC3339.parse("23:59:60.999999Z")
|
||||
end
|
||||
|
||||
test "decode 23:59:60.9999999Z" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, usec: 999999} == RFC3339.parse("23:59:60.9999999Z")
|
||||
end
|
||||
|
||||
test "decode 00:00:00+00:00" do
|
||||
assert %RFC3339{hour: 0, min: 0, sec: 0, tz_offset: 0} == RFC3339.parse("00:00:00+00:00")
|
||||
end
|
||||
|
||||
test "decode 00:00:00-00:00" do
|
||||
assert %RFC3339{hour: 0, min: 0, sec: 0, tz_offset: 0} == RFC3339.parse("00:00:00-00:00")
|
||||
end
|
||||
|
||||
test "decode 00:00:00+23:59" do
|
||||
assert %RFC3339{hour: 0, min: 0, sec: 0, tz_offset: 1439} == RFC3339.parse("00:00:00+23:59")
|
||||
end
|
||||
|
||||
test "decode 23:59:60+00:00" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, tz_offset: 0} == RFC3339.parse("23:59:60+00:00")
|
||||
end
|
||||
|
||||
test "decode 23:59:60+23:59" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, tz_offset: 1439} == RFC3339.parse("23:59:60+23:59")
|
||||
end
|
||||
|
||||
test "decode 23:59:60-23:59" do
|
||||
assert %RFC3339{hour: 23, min: 59, sec: 60, tz_offset: -1439} == RFC3339.parse("23:59:60-23:59")
|
||||
end
|
||||
|
||||
test "decode 1979-06-21T22:20:03Z" do
|
||||
assert %RFC3339{year: 1979, month: 6, day: 21, hour: 22, min: 20, sec: 3} == RFC3339.parse("1979-06-21T22:20:03Z")
|
||||
end
|
||||
|
||||
test "decode 1979-06-21t22:20:03z" do
|
||||
assert %RFC3339{year: 1979, month: 6, day: 21, hour: 22, min: 20, sec: 3} == RFC3339.parse("1979-06-21t22:20:03z")
|
||||
end
|
||||
|
||||
test "decode 1979-06-21 22:20:03Z" do
|
||||
assert %RFC3339{year: 1979, month: 6, day: 21, hour: 22, min: 20, sec: 3} == RFC3339.parse("1979-06-21 22:20:03Z")
|
||||
end
|
||||
|
||||
test "decode 1979-06-21T22:20:03.9876543Z" do
|
||||
assert %RFC3339{year: 1979, month: 6, day: 21, hour: 22, min: 20, sec: 3, usec: 987654} == RFC3339.parse("1979-06-21T22:20:03.9876543Z")
|
||||
end
|
||||
|
||||
test "decode 1979-06-21T22:20:03+02:00" do
|
||||
assert %RFC3339{year: 1979, month: 6, day: 21, hour: 22, min: 20, sec: 3, tz_offset: 120} == RFC3339.parse("1979-06-21T22:20:03+02:00")
|
||||
end
|
||||
|
||||
test "decode 1979-06-21T22:20:03.9876543+02:00" do
|
||||
assert %RFC3339{year: 1979, month: 6, day: 21, hour: 22, min: 20, sec: 3, usec: 987654, tz_offset: 120} == RFC3339.parse("1979-06-21T22:20:03.9876543+02:00")
|
||||
end
|
||||
|
||||
test "encode 1979-06-21" do
|
||||
assert "1979-06-21" == RFC3339.format(%RFC3339{year: 1979, month: 6, day: 21})
|
||||
end
|
||||
|
||||
test "encode 12:12:12Z" do
|
||||
assert "12:12:12Z" == RFC3339.format(%RFC3339{hour: 12, min: 12, sec: 12})
|
||||
end
|
||||
|
||||
test "encode 12:12:12.120000Z" do
|
||||
assert "12:12:12.120000Z" == RFC3339.format(%RFC3339{hour: 12, min: 12, sec: 12, usec: 120000})
|
||||
end
|
||||
|
||||
test "encode 12:12:12.000012Z" do
|
||||
assert "12:12:12.000012Z" == RFC3339.format(%RFC3339{hour: 12, min: 12, sec: 12, usec: 12})
|
||||
end
|
||||
|
||||
test "encode 12:12:12+12:12" do
|
||||
assert "12:12:12+12:12" == RFC3339.format(%RFC3339{hour: 12, min: 12, sec: 12, tz_offset: 732})
|
||||
end
|
||||
|
||||
test "encode 12:12:12-12:12" do
|
||||
assert "12:12:12-12:12" == RFC3339.format(%RFC3339{hour: 12, min: 12, sec: 12, tz_offset: -732})
|
||||
end
|
||||
|
||||
test "encode 1979-06-21T12:12:12Z" do
|
||||
assert "1979-06-21T12:12:12Z" == RFC3339.format(%RFC3339{year: 1979, month: 6, day: 21, hour: 12, min: 12, sec: 12})
|
||||
end
|
||||
|
||||
end
|
1
test/test_helper.exs
Normal file
1
test/test_helper.exs
Normal file
@ -0,0 +1 @@
|
||||
ExUnit.start()
|
Loading…
Reference in New Issue
Block a user