Language: EN

programacion-tipos-tiempo

Time Types

The types of temporal variables are the types related to the measurement of time, in its various forms.

Similarly to how it happened with texts, time is something that “as little people” we are used to using habitually, practically every day.

Examples of measuring time include meeting at a specific hour, a birth date, or the time that has elapsed between two specific moments.

However, a computer “does not understand” anything about the passage of time, it only understands numbers. So we have had to invent ways to represent and measure the passage of time.

The types of time that we will find most frequently are,

  • Dates, to represent dates without including information about the time of day. This type of data generally includes components such as day, month, and year
  • Hours, to represent the time of day without considering the date
  • DateTime (date and time), often the two previous types are combined into a single type, which combines both the date and the time
  • Time interval, used to represent a time interval, or duration. For example, it is usually the result of calculating the difference between two dates or times

The availability of one type or another depends on each programming language. Some may implement all, or omit some. But in general, these are the types that we will find most frequently.

Example of time variable types in different languages

Let’s look at some examples of how these types are used in different programming languages.

For example, C# has the DateTime type for dates and times, and TimeSpan for time intervals. Its usage would be as follows.

// example DateTime
DateTime startDate = new DateTime(2023, 6, 1, 12, 0, 0);
DateTime endDate = new DateTime(2023, 6, 1, 14, 30, 0);

// example TimeSpan
TimeSpan duration = endDate - startDate;

Console.WriteLine(duration); // Output: 02:30:00

The code in Java would be very similar. Here we have the Date, LocalDateTime types for dates and times, and the Duration type for time intervals.

// example LocalDateTime
LocalDateTime startDate = LocalDateTime.of(2023, 6, 1, 12, 0, 0);
LocalDateTime endDate = LocalDateTime.of(2023, 6, 1, 14, 30, 0);

// example Duration
Duration duration = Duration.between(startDate, endDate);

In the case of C++, we will have to rely on libraries like ctime or chronos to manage time types. The syntax is going to be a bit horrible, but it would be something like this.

#include <chrono>

// example of std::chrono::system_clock
auto startDate = std::chrono::system_clock::from_time_t(std::mktime(&std::tm{0, 0, 12, 1, 5, 123}));
auto endDate = std::chrono::system_clock::from_time_t(std::mktime(&std::tm{0, 30, 14, 1, 5, 123}));

// example of std::chrono::duration
std::chrono::duration<double> duration = endDate - startDate;

In the case of JavaScript, all time-related operations are performed using the Date object.

let startDate = new Date(2023, 5, 1, 12, 0, 0);
let endDate = new Date(2023, 5, 1, 14, 30, 0);

let duration = endDate - startDate;

While in Python we will use the datetime object that we must import beforehand.

from datetime import datetime

startDate = datetime(2023, 6, 1, 12, 0, 0)
endDate = datetime(2023, 6, 1, 14, 30, 0)

duration = endDate - startDate

To give an example of a language that differentiates between Date, Time, and DateTime, for example, in SQL we have a plethora of time variable types.

-- Example of using parameters with date and time types
DECLARE @dateParam DATE;
DECLARE @timeParam TIME;
DECLARE @dateTimeParam DATETIME;

-- Assign values to the parameters
SET @dateParam = '2023-06-01';
SET @timeParam = '14:30:00';
SET @dateTimeParam = '2023-06-01 14:30:00';

As we can see, more or less the types related to time are similar in the different programming languages. Some distinguish between Date and Time, although they are often combined into a DateTime type.

On the other hand, for operations with times, some languages have specific types, such as TimeSpan in C# or Duration in Java. While other languages “manage” with DateTime for everything.

The syntax and the way to create and manipulate time objects will not be the same across all languages. Each language may have peculiarities in handling date and time formats, as well as in the specific functions available.

In addition, some languages incorporate certain functionalities natively. While in others, we will have to rely on additional libraries and modules.

But the underlying concepts are similar. All languages provide functions and methods to perform common operations on dates and times, such as comparing, adding, or subtracting time intervals.