Syntax:
The localtime function has the following syntax:
The “time” is a pointer to a “time_t” value that represents the time to be converted to local time.
A “time_t” value is sent to the localtime function which outputs a reference to a “tm” structure with the associated local time. The “tm” structure contains the various fields such as “tm_sec”, “tm_min”, “tm_hour”, “tm_mday”, “tm_mon”, “tm_year”, and more which represent the broken-down components of the local time.
It’s important to note that the localtime function uses the system’s time zone and daylight saving time settings to perform the conversion.
Here are a few C++ examples that show how to use the localtime function.
Example 1: Get the Current Local Time
Let’s demonstrate an example to comprehend the use of the localtime() function in C++:
In this program, the <iostream> header file is included for input/output operations, while when you include <ctime> in your C++ program using #include <ctime>, you gain access to various functions and types related to time and date such as time(), ctime(), localtime(), gmtime(), etc.
The main() function begins where the time() function is called with “nullptr” as the argument to obtain the current time as a “time_t” value. The “time_t” is a type that represents the calendar time in seconds since the Unix epoch.
The localtime() function is invoked to convert the “time_t” value (currentT) to a “tm” structure that represents the local time. It gives back a pointer to the “tm” structure. The “tm” structure contains various fields that represent the different components of the local time such as year, month, day, hour, minute, and second.
Then, the program uses the int hour = localT->tm_hour;, int minute = localT->tm_min;, and int second = localT->tm_sec; to access the individual components of the “tm” structure. The “tm_hour”, “tm_min”, and “tm_sec” members represent the hour (0-23), minute (0-59), and second (0-59) of the local time, respectively. The values of these components are assigned to the corresponding variables (hr, mn, and sc).
We utilize the “std::cout” to print the current local time to the console. The hour, minute, and second values are combined or linked together using the stream insertion (<<) operator and displayed in the “hour:minute:second” format. The “std::endl” manipulator is used to insert a newline character.
The “return 0” indicates the successful program execution if main() returns 0.
When you run this code, the output displays the current local time in the specified format.
Example 2: Get the Local Time for a Specific Timestamp
Let’s learn this through a practical example:
#include <ctime>
int main() {
time_t Tstamp = 1625604000;
tm* localT = localtime(&Tstamp);
int y = localT->tm_year + 1990;
int m = localT->tm_mon + 1;
int d = localT->tm_mday;
int hr = localT->tm_hour;
int mn = localT->tm_min;
int sc = localT->tm_sec;
std::cout << "Local time for timestamp 1625604000: "
<< y << "-" << m << "-" << d<< " "
<< hr << ":" << mn << ":" << sc << std::endl;
return 0;
}
The necessary header files are included initially. Then, in the main() function, we declare and initialize the “Tstamp” variable with a specific value of 1625604000. This value represents a timestamp in UNIX time which is the count of seconds that passed since January 1, 1970 and is also known as the UNIX epoch.
The localtime() function is utilized to transform the provided “time_t” value (Tstamp) into a “tm” structure that represents the local time. This function returns a pointer to the “tm” structure which encompasses several fields that represent the distinct components of the local time including the year, month, day, hour, minute, and second.
In the next lines of the code, the individual components of the “tm” structure are accessed by dereferencing the “localT” pointer. Each component is represented by a specific field within the “tm” structure. For example, the “tm_year” field denotes the year since 1990, the “tm_mon” field represents the month (in the range of 0-11), and the “tm_mday” field indicates the day of the month (in the range of 1-31). Following that, the values of these elements are allocated to the respective variables (y, m, d, hr, mn, and sc).
Using the “std::cout” object, the program outputs the local time components to the console. The values of “y”, “m”, “d”, “hr”, “mn”, and “sc” are combined using the stream insertion (`<<`) operator which results in a formatted display of the local time in the “YYYY-MM-DD HH:MM:SS” format.
When you run this code, the output displays the local time that corresponds to the given timestamp.
Keep in mind that based on your system’s time zone settings, the local time that is shown may change.
Example 3: Set the Local Time Manually
To set the local time manually in C++, you can modify the values of the individual components of the “tm” structure.
Let’s go through an example that demonstrates how to manually set the local time.
#include <ctime>
int main() {
time_t currentT = time(nullptr); // Get the current time as a time_t value
tm* localT = localtime(¤tT);
localT->tm_year = 123;
localT->tm_mon = 5;
localT->tm_mday = 07;
localT->tm_hour = 16;
localT->tm_min = 20;
localT->tm_sec = 8;
std::cout << "Modified local time: "
<< localT->tm_year + 1900 << "-" << localT->tm_mon + 1 << "-" << localT->tm_mday << " " << localT->tm_hour << ":" << localT->tm_min << ":" << localT->tm_sec << std::endl;
return 0;
}
In this example, we first obtain the current local time and convert it to a “tm” structure using the localtime() function.
These lines set the individual components of the “tm” structure (localT) to specific values which effectively modifies the local time. The year is set to 2021 (tm_year = 123 since it counts from 1900), the month is set to June (tm_mon = 5), the day of the month is set to 03 (tm_mday = 03), the hour is set to 16, the minute is set to 20, and the second is set to 8.
Finally, we print the modified local time components using the “std::cout”. The values of the modified “tm” structure (localT) are accessed and concatenated using the stream insertion (<<) operator. The year is adjusted by adding 1900 to “tm_year” and the month is adjusted by adding 1 to “tm_mon” to represent the expected values.
The output that we get from this program is presented in the following:
Please note that this code modifies the local time components in memory but does not change the system’s time. Modifying the system time requires the appropriate privileges and system-specific functions.
Conclusion
In C++, there are several uses for the localtime() method. In this guide, we explained to you the three examples of the utilization of the localtime() function. The first illustration demonstrated the utilization of the localtime() function to get the current local time. The second example discussed the implementation of the localtime() function to get the local time for a particular timestamp. Whereas, the last example is about setting the local time manually on our machine using the localtime() function.