DEV Community

bbronek
bbronek

Posted on • Updated on

Beginning my journey with elixir Part III. Time, Mix Tasks, IEx, Errors, Executables.

As I wrote in the last part, this article will be about dates and times, custom mix tasks, more about iex, error handling, and executable files. In the next part, I will focus on the practical use of basic knowledge, but for now, I encourage you to familiarize yourself with the problems contained in my presentation 😉

Alt Text

1.Dates and times

-Time
Examples of how to use the module :

Get a current time:

iex> Time.utc_now
~T[00:57:02.826573]
Enter fullscreen mode Exit fullscreen mode

Take a look, that ~T is a sigil which I explained in Part II.

So maybe let's use our knowledge about sigils and create a Time struct.

iex> time = ~T[20:34:23.754335]
~T[20:34:23.754335]
iex> time.hour
20
iex> time.second
23
Enter fullscreen mode Exit fullscreen mode

-Date

Get actual date:

iex> Date.utc_today
~D[2021-02-20]

Enter fullscreen mode Exit fullscreen mode

So let's create a date structure and use it in practice

iex> {:ok, date} = Date.new(1023,5,24)
{:ok, ~D[1023-05-24]}
iex > Date.leap_year? date
false
Enter fullscreen mode Exit fullscreen mode

-NaiveDateTime

The NaiveDateTime struct contains the fields year, month, day, hour, minute, second, microsecond and calendar. You can read more about this here.

Examples :

iex> NaiveDateTime.utc_now
~N[2021-02-20 01:18:10.218303]
iex> NaiveDateTime.diff(~N[2018-04-17 14:00:00], ~N[1970-01-01 00:00:00])
1523973600 <- result in seconds

Enter fullscreen mode Exit fullscreen mode

-DateTime

The module supports date and timezones.

Example:

iex> DateTime.from_naive(~N[2013-04-21 11:25:02.0034], "Etc/UTC")
{:ok, ~U[2013-04-21 11:25:02.0034Z]}
Enter fullscreen mode Exit fullscreen mode

More: link1, link2

2.Custom Mix Tasks

1.Create a new app by - 'mix new app'
2.Create a new directory newTask/lib/mix/tasks/task.ex
3.In task.ex create module
4.Use it by 'mix task'

3.IEx

You can by h before the module gets documentation about it, by i information about data type, r - recompile a particular module, t- information about types available in a given module.

iex> h Enum
Provides a set of algorithms to work with enumerables.

In Elixir, an enumerable is any data type that implements the Enumerable
protocol. Lists ([1, 2, 3]), Maps (%{foo: 1, bar: 2}) and Ranges (1..3) are
common data types used as enumerables: ...
iex> i Map
Term
  Map
Data type
  Atom ...
iex> t Map
@type key() :: any()
@type value() :: any()

Enter fullscreen mode Exit fullscreen mode

4.Error Handling

When an error occurs Elixir returns the tuple {: error, reason}. The simplest way to make an error is by use raise:

iex> raise "Error"
** (RuntimeError) Error
Enter fullscreen mode Exit fullscreen mode

It is set by default to raise a RuntimeError but if you want to change it you can by the simple way: raise typeoferror, message: "lorem ipsum", example:

raise ArithmeticError, message: "bad argument in arithmetic expression"
Enter fullscreen mode Exit fullscreen mode

try-rescue-after
It is simple construction to handling errors, I hope that you understand it by reading this example:

iex> try do
...>   raise "err"
...> rescue
...>   e in RunTimeError -> IO.puts("Msg:" <> e.message)
...> after
...>   IO.puts "End"
Enter fullscreen mode Exit fullscreen mode

Create own Error

defmodule NewError do
  defexception message: "an error has occurred"
end
Enter fullscreen mode Exit fullscreen mode

5.Executable Files

You can make a file executable in Elixir by _File.chmod(pat_file,0o755) (755 is a permission for a file)_.

If you want to build executables projects, you can use escript.

See you 👋

Top comments (0)