Beginning my journey with elixir Part III. Time, Mix Tasks, IEx, Errors, Executables.
Tags: elixir, beginners, tutorial
Published: 2021-02-20 03:42:50

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mtxl60i8y51wlcadln4.jpg)

####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]
```
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
```

**-Date**

Get actual date:
```
iex> Date.utc_today
~D[2021-02-20]

```

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
```

**-NaiveDateTime**

The NaiveDateTime struct contains the fields year, month, day, hour, minute, second, microsecond and calendar. You can read more about this [here](https://hexdocs.pm/elixir/master/NaiveDateTime.html).

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

```

**-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]}
```
More: [link1](https://elixirschool.com/en/lessons/basics/date-time/#naivedatetime), [link2](https://hexdocs.pm/elixir/master/DateTime.html)

####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](https://elixirschool.com/en/lessons/basics/mix-tasks/)**
**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()

```

####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
```
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"
```
**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"
```

**Create own Error**
```
defmodule NewError do
  defexception message: "an error has occurred"
end
```
####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](https://elixirschool.com/en/lessons/advanced/escripts/).

See you 👋