Created
May 11, 2012 17:18
-
-
Save guedes/2661107 to your computer and use it in GitHub Desktop.
Error testing some scenarios...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
refer Erlang.calendar, as: Calendar | |
defmodule Date do | |
@moduledoc """ | |
This module provides facilities to work with Date and Time | |
""" | |
defrecord Info, year: 1, month: 1, day: 1, hour: 0, minute: 0, second:0, milisecond: 0.0, as_tuple: {{0,0,0},{0,0,0}}, as_text: "0000-00-00 00:00:00" | |
@doc """ | |
Returns a Date record. | |
## Example | |
date = Date.new("2011-01-01 12:35:48") | |
date.year #=> "2011" | |
date.month #=> "01" | |
date.day #=> "01" | |
date.hour #=> "12" | |
date.minute #=> "35" | |
date.second #=> "48" | |
""" | |
def new(string) when is_binary(string) do | |
regex = %r/^(\d{4})-(\d{2})-(\d{2})( (\d{1,2}):(\d{2}):(\d{2}))?$/ | |
destructure [ _, year, month, day, _, hour, minute, second ], Regex.run(regex, string) | |
info = Info[ | |
year: to_year(year), | |
month: to_month(month), | |
day: to_day(day), | |
hour: to_hour(hour), | |
minute: to_minute(minute), | |
second: to_second(second), | |
as_tuple: {{to_year(year), to_month(month), to_day(day)},{to_hour(hour),to_minute(minute),to_second(second)}} #, | |
#as_text: string | |
] | |
info | |
end | |
def new({{year, month, day},{hour, minute, second}}) do | |
info = Info[ | |
year: to_year(year), | |
month: to_month(month), | |
day: to_day(day), | |
hour: to_hour(hour), | |
minute: to_minute(minute), | |
second: to_second(second), | |
as_tuple: {{to_year(year), to_month(month), to_day(day)},{to_hour(hour),to_minute(minute),to_second(second)}} #, | |
#as_text: "#{to_year(year)}-#{to_month(month)}-#{to_day(day)} #{to_hour(hour)}:#{to_minute(minute)}:#{to_second(second)}" | |
] | |
info | |
end | |
@doc """ | |
Adds an interval to a date-time | |
## Examples | |
date = Date.new("2011-11-13 10:12:58") | |
result = Date.add date, 30, :minutes | |
#=> "2011-11-13 10:42:58" | |
""" | |
def add(date, n, :seconds) do | |
seconds = Calendar.datetime_to_gregorian_seconds(date.as_tuple) + n | |
Date.new(Calendar.gregorian_seconds_to_datetime(seconds)) | |
end | |
def add(date, n, :minutes) do | |
add(date, n*60, :seconds) | |
end | |
def add(date, n, :hours) do | |
add(date, n*60, :minutes) | |
end | |
def add(date, n, :days) do | |
add(date, n*24, :hours) | |
end | |
defp to_year(s) when is_binary(s), do: to_integer(s) | |
defp to_year(x) when x >= 0, do: x | |
defp to_month(s) when is_binary(s), do: to_integer(s) | |
defp to_month(x) when x >= 1 and x <= 12, do: x | |
defp to_day(s) when is_binary(s), do: to_integer(s) | |
defp to_day(x) when x >= 1 and x <= 31, do: x | |
defp to_hour(s) when is_binary(s), do: to_integer(s) | |
defp to_hour(x) when x >= 0 and x <= 23, do: x | |
defp to_minute(s) when is_binary(s), do: to_integer(s) | |
defp to_minute(x) when x >= 0 and x <= 59, do: x | |
defp to_second(s) when is_binary(s), do: to_integer(s) | |
defp to_second(x) when x >= 0 and x <= 59, do: x | |
defp to_integer(s), do: list_to_integer(binary_to_list(s)) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code.require_file "../test_helper.exs", __FILE__ | |
defmodule DateUtils.AddTest do | |
use ExUnit.Case | |
test :store_each_date_part do | |
date = Date.new("2011-01-01 12:35:48") | |
assert 2011 == date.year | |
assert 1 == date.month | |
assert 1 == date.day | |
assert 12 == date.hour | |
assert 35 == date.minute | |
assert 48 == date.second | |
end | |
test :adds_3_seconds do | |
date = Date.new("2011-10-12 14:15:58") | |
date_expected = Date.new("2011-10-12 14:16:01") | |
assert date_expected == Date.add date, 3, :seconds | |
end | |
test :adds_30_minutes do | |
date = Date.new("2011-11-13 10:12:58") | |
date_expected = Date.new("2011-11-13 10:42:58") | |
assert date_expected == Date.add date, 30, :minutes | |
end | |
test :adds_5_hours do | |
date = Date.new("2012-10-13 20:12:58") | |
date_expected = Date.new("2012-10-14 1:12:58") | |
assert date_expected == Date.add date, 5, :hours | |
end | |
test :adds_2_days do | |
date = Date.new("2012-02-28 1:12:58") | |
date_expected = Date.new("2012-03-01 1:12:58") | |
assert date_expected == Date.add date, 2, :days | |
end | |
test :as_text_returns_string do | |
date = Date.new("2012-02-28 1:12:58") | |
expected_text = "2012-02-28 1:12:58" | |
assert expected_text == date.as_text | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
guedes@dba01:/disco/programas/exdate$ make clean test | |
rm -rf ebin | |
Compiling Elixir code ... | |
elixirc lib/*/*/*.ex lib/*/*.ex lib/*.ex -o ebin --docs | |
Compiled lib/date.ex | |
Running tests ... | |
time elixir -pa ebin -r "test/**/*_test.exs" | |
F.F... | |
1) test_adds_2_days (DateUtils.AddTest) | |
** (ExUnit.AssertionError) Expected Date.Info[year: 2012] to be equal to (==) Date.Info[year: 2012] | |
stacktrace: | |
/disco/programas/exdate/test/date_test.exs:43: DateUtils.AddTest."test_adds_2_days"/0 | |
/disco/programas/elixir/lib/ex_unit/runner.ex:78: ExUnit.Runner.run_test/3 | |
/disco/programas/elixir/lib/enum.ex:619: Enum.do_each/3 | |
/disco/programas/elixir/lib/enum.ex:187: Enum.each/2 | |
/disco/programas/elixir/lib/ex_unit/runner.ex:69: ExUnit.Runner.run_tests/2 | |
2) test_adds_3_seconds (DateUtils.AddTest) | |
** (ExUnit.AssertionError) Expected Date.Info[year: 2011] to be equal to (==) Date.Info[year: 2011] | |
stacktrace: | |
/disco/programas/exdate/test/date_test.exs:20: DateUtils.AddTest."test_adds_3_seconds"/0 | |
/disco/programas/elixir/lib/ex_unit/runner.ex:78: ExUnit.Runner.run_test/3 | |
/disco/programas/elixir/lib/enum.ex:619: Enum.do_each/3 | |
/disco/programas/elixir/lib/enum.ex:187: Enum.each/2 | |
/disco/programas/elixir/lib/ex_unit/runner.ex:69: ExUnit.Runner.run_tests/2 | |
6 tests, 2 failures. | |
Command exited with non-zero status 1 | |
0.20user 0.04system 0:00.25elapsed 97%CPU (0avgtext+0avgdata 71824maxresident)k | |
0inputs+0outputs (0major+9177minor)pagefaults 0swaps | |
make: ** [test] Erro 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment