Date and time¶
In this demonstration, we will take an in-depth look at the functionality of working with dates and time in Engee. We will learn the basic tools and functions that Julia has for working with time data, including creating, formatting, comparing, and performing various operations on dates and times.
In addition, we will focus on the process of converting temporal data between different formats. This includes converting strings to date and time objects, converting between different time types (e.g. Date and DateTime), and formatting to represent temporal data as strings that match specified patterns.
The demo will be useful both for beginners learning the basics of working with temporal data in Julia and for experienced developers who want to deepen their knowledge in this area.
And the first thing we will consider is the possibilities of defining not date, but time. In Julia, duration objects represent time intervals (for example, hours, minutes, seconds) and belong to the Period type family.
Period objects include:
- Hour(n) - time interval in hours,
- Minute(n) - time interval in minutes,
- 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.
d1 = Hour(3) + Minute(30) + Second(45)
println(d1)
d2 = Hour(1) + Minute(10) + Second(12)
println(d2)
d1 + d2
d = d1 - d2
Also such data types can be converted to strings.
str = string(d)
Next, consider the Dates.now() function. It returns the current date and time as an object of type DateTime. This type is a combination of date and time.
dnow = Dates.now()
DateTime can also be represented in string format.
str = string(dnow)
Consider the DateTime(today()) function. The today() function returns an object of type Date representing the current date without time. The DateTime() constructor converts a Date object into a DateTime object by adding zero time.
dday = DateTime(today())
Arithmetic with such objects allows you to calculate the time difference between two moments of time or find their sum.
In our case, the difference between dnow and dday is returned as an object of type CompoundPeriod, which expresses the time interval in understandable units - hours, minutes, seconds or milliseconds.
difference = (dnow-dday)
Let's convert milliseconds into hours. In this case, let's calculate this value mathematically, because the Hour constructor doesn't just translate the difference into hours, it tries to interpret the input value as a strict number of whole hours. If the result of division is not an integer, an error occurs.
Dates.value(difference) / (60 * 60 * 1000)
Next, let's look at a few more variants of date and time definitions and their transformations.
We can set the values manually, specifying the date and time, for example, 2 January 2025, time 12:05:00.
D = DateTime(2025, 1, 2, 12, 5, 0)
Or we can set a set of dates by changing one of the date parameters in the loop.
D = [DateTime(2025, month, 2, 12, 5, 0) for month in 1:3]
We can also change the order of displaying values and display months not as numeric values, but as a verbal description.
str = "2021-09-15 09:12:34"
d = DateTime(str, "yyyy-mm-dd HH:MM:SS")
d = now() # Текущее дата и время
fmt = "dd U MMMM yyyy, HH:MM:SS AM" # Формат строки (аналогичный)
str = Dates.format(d, fmt)
We can also output dates in Russian by declaring an array of names in advance.
Dates.LOCALES["ru"] = Dates.DateLocale(
["Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля", "Августа", "Сентября", "Октября", "Ноября", "Декабря"],
["Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"],
["Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье"],
["Пн", "Вт", "Ср", "Чг", "Пт", "Сб", "Вс"]);
Dates.format(d, "E dd u YYYY H:MM:SS", locale="ru" )
Conclusion¶
In this demonstration, we have looked at the basic features of working with dates and time in Engee.
These tools allow you to conveniently manage time data in analytics, accounting or formatting.