Language: EN

programacion-tipos-tiempo

Time Types

Temporal variable types are types related to the measurement of time, in its different facets.

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

Examples of time measurement are meeting at a specific time, a date of birth, or the time that has elapsed between two specific moments.

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

The time types that we are going to 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 the day, the month, and the year
  • Hours, to represent the time of day without taking into account the date
  • DateTime (date and time), frequently the previous two are combined into a single type, which combines both the date and the time
  • Time interval, it is 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 existence of one or another type depends on each programming language. Some may implement all, or omit some. But in general, these are the types that we are going to find most frequently.

Example of types of time variables in different programming languages

Let’s see 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 use would be as follows.

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

// TimeSpan example
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 type for dates and times, and the Duration type for time intervals.

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

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

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

#include <chrono>

// std::chrono::system_clock example
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}));

// std::chrono::duration example
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

As 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 frequently 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 “get by” with DateTime for everything.

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

The syntax and the way of creating and manipulating time objects can vary between languages. Each language may have peculiarities in handling date and time formats, as well as in the specific functions available.

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.