Engee documentation
Notebook

Date and time

In this demo, we will take a detailed look at the functionality of working with dates and times in Engee. We will get acquainted with the main tools and functions available in the Julia language for working with time data, including creating, formatting, comparing, and performing various operations on dates and times.

In addition, we will pay attention to the process of converting temporary data between different formats. This includes converting strings to date and time objects, converting between different time types (for example, Date and DateTime), and formatting to represent time data as strings that match specified patterns.

The demonstration will be useful both for beginners learning the basics of working with temporary data in Julia, and for experienced developers who want to deepen their knowledge in this area.

And the first thing we are looking at is the possibility of determining not the date, but the time. In Julia, duration objects represent time intervals (for example, hours, minutes, seconds) and belong to the Period family of types.

Period objects include:

  1. Hour(n) — time interval in hours,
  2. Minute(n) — time interval in minutes,
  3. Second(n) — time interval in seconds.

These objects allow you to perform arithmetic operations, and can also be used when working with date and time objects.

In [ ]:
d1 = Hour(3) + Minute(30) + Second(45)
println(d1)
d2 = Hour(1) + Minute(10) + Second(12)
println(d2)
3 hours, 30 minutes, 45 seconds
1 hour, 10 minutes, 12 seconds
In [ ]:
d1 + d2
Out[0]:
4 hours, 40 minutes, 57 seconds
In [ ]:
d = d1 - d2
Out[0]:
2 hours, 20 minutes, 33 seconds

These types of data can also be converted to strings.

In [ ]:
str = string(d)
Out[0]:
"2 hours, 20 minutes, 33 seconds"

Next, consider the Dates.now() function. It returns the current date and time as a DateTime object. This type is a combination of date and time.

In [ ]:
dnow = Dates.now()
Out[0]:
2025-02-17T07:23:03.059

DateTime can also be represented in string format.

In [ ]:
str = string(dnow)
Out[0]:
"2025-02-17T07:23:03.059"

Consider the DateTime(today()) function. The today() function returns a Date object representing the current date without taking time into account. The DateTime() constructor converts a Date object into a DateTime object by adding a zero time.

In [ ]:
dday = DateTime(today())
Out[0]:
2025-02-17T00:00:00

Arithmetic with such objects allows you to calculate the time difference between two points in time or find their sum.

In our case, the difference between dnow and dday is returned as an object of the CompoundPeriod type, which expresses the time interval in understandable units – hours, minutes, seconds, or milliseconds.

In [ ]:
difference = (dnow-dday)
Out[0]:
26583059 milliseconds

Convert milliseconds to hours. In this case, we will calculate this value mathematically, since the Hour constructor does not just convert the difference into hours, but tries to interpret the input value as a strict number of whole hours. If the result of the division is not an integer, an error occurs.

In [ ]:
Dates.value(difference) / (60 * 60 * 1000)
Out[0]:
7.384183055555556

Next, we will consider several more options for determining the date and time and their transformations.

We can set the values manually by specifying the date and time, for example, January 2, 2025, the time is 12:05:00.

In [ ]:
D = DateTime(2025, 1, 2, 12, 5, 0)
Out[0]:
2025-01-02T12:05:00

Or we can set a set of dates by changing one of the date parameters in the loop.

In [ ]:
D = [DateTime(2025, month, 2, 12, 5, 0) for month in 1:3]
Out[0]:
3-element Vector{DateTime}:
 2025-01-02T12:05:00
 2025-02-02T12:05:00
 2025-03-02T12:05:00

We can also change the order in which the values are displayed and display the months not as numeric values, but as a verbal description.

In [ ]:
str = "2021-09-15 09:12:34"
d = DateTime(str, "yyyy-mm-dd HH:MM:SS")
Out[0]:
2021-09-15T09:12:34
In [ ]:
d = now()  # Текущее дата и время
fmt = "dd U MMMM yyyy, HH:MM:SS AM"  # Формат строки (аналогичный)
str = Dates.format(d, fmt)
Out[0]:
"17 February 0023 2025, 07:23:04 A23"

We can also output dates in Russian by announcing an array of names in advance.

In [ ]:
Dates.LOCALES["ru"] = Dates.DateLocale(
["Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля", "Августа", "Сентября", "Октября", "Ноября", "Декабря"],
["Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"],
["Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье"],
["Пн", "Вт", "Ср", "Чг", "Пт", "Сб", "Вс"]);

Dates.format(d, "E dd u YYYY H:MM:SS", locale="ru" )
Out[0]:
"Понедельник 17 Фев 2025 7:23:04"

Conclusion

In this demo, we have reviewed the main features of working with dates and times in Engee.

These tools allow you to conveniently manage temporary data in analytics, accounting, or formatting.