Navigating Time and Date Manipulation in C++
Within the domain of C++ programming, time and date manipulation emerges as a nuanced art. While C++ inherits its date and time structures and functions from C, these tools allow for
the orchestration of chronological feats. To unveil this realm, the header file is your key, encompassing an array of structures and functions. Date and time libraries in C++ provide essential tools for working with temporal data, allowing precise handling of scheduling, calculations, and synchronization.The Quartet of Time-related Types
C++ presents four remarkable time-related types: clock_t, time_t, size_t, and tm. These types, reminiscent of integers, serve as gateways to representing system time and date.
Unveiling the Structure of Time
At the heart of time manipulation lies the struct tm, a complex entity that holds time and date elements in harmony:
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since Sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
The Symphony of Time Functions
The stage of time manipulation introduces an ensemble of functions. Each function, with its unique role, adds melody to the symphony of date and time:
- time_t time(time_t *time): This function returns the current calendar time of the system as seconds elapsed since January 1, 1970.
- char *ctime(const time_t *time): Releasing a string that narrates the tale of the day, month, year, and time.
- struct tm *localtime(const time_t *time): Unfolding a pointer to the tm structure, representing local time.
- clock_t clock(void): Offering an approximation of the program’s running time.
- char *asctime(const struct tm *time): Creating a string that encapsulates the time structure’s content.
- struct tm *gmtime(const time_t *time): Presenting a pointer to the time in the form of a tm structure, represented in Coordinated Universal Time (UTC).
- time_t mktime(struct tm *time): A masterstroke that transforms the time structure into its calendar-time equivalent.
- double difftime(time_t time2, time_t time1): Measuring the time difference between two moments.
- size_t strftime(): A versatile function, formatting date and time according to specific needs.
Embracing the Current Date and Time
To embrace the current system date and time—whether in the local time or Coordinated Universal Time (UTC)—one must embark on a journey with the following script:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
time_t now = time(0);
char* dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "The UTC date and time is: " << dt << endl;
}
As the script takes its bow on the stage of execution, the reveal is magnificent:
The local date and time is: Sat Jan 8 20:07:41 2011
The UTC date and time is: Sun Jan 9 03:07:41 2011
Shaping Time with the tm Structure
Within the fabric of date and time manipulation, the tm structure takes center stage. This structure’s members, including seconds, minutes, hours, days, months, years, days since Sunday, days since January 1st, and daylight saving hours, facilitate the intricate dance of time:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
time_t now = time(0);
cout << "Number of sec since January 1, 1970 is: " << now << endl;
tm *ltm = localtime(&now);
cout << "Year: " << 1900 + ltm->tm_year << endl;
cout << "Month: " << 1 + ltm->tm_mon << endl;
cout << "Day: " << ltm->tm_mday << endl;
cout << "Time: " << 5 + ltm->tm_hour << ":";
cout << 30 + ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
As the code takes its curtain call, the revelations unveil:
Number of sec since January 1, 1970 is: 1588485717
Year: 2020
Month: 5
Day: 3
Time: 11:31:57
In the intricate tapestry of C++ programming, the manipulation of time and date forms a timeless art, empowering coders to navigate the currents of chronology.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.