Programming languages - C++

Back to Course

Lesson Description

Lession - #50 C++ Date & Time

The C++ standard library doesn't give an appropriate date type. C++ acquires the structs and functions for date and time control from C. To get to date and time related functions and structures, you would have to incorporate header file in your C++ program. There are four time-related types: **clock\_t, time\_t, size\_t,** and **tm**. The sorts - clock\_t, size\_t and time\_t are fit for addressing the system time and date as an integer of some kind. The structure type **tm** holds the date and time as a C structure having the accompanying components −   ```plaintext 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 } ``` Following are the significant functions, which we use while working with date and time in C or C++. This multitude of functions are essential for standard C and C++ library and you can check their detail utilizing reference to C++ standard library given underneath. | Sr.No | Function and Purpose | | --- | --- | | 1 | **time\_t time(time\_t \*time>
;** This profits the ongoing schedule calendar of the system in number of seconds passed since January 1, 1970. Assuming that the system has no time, .1 is returned. | | 2 | **singe \*ctime(const time\_t \*time>
;** This profits a pointer to a string of the structure day month year hours:minutes:seconds year\\n\\0. | | 3 | **struct tm \*localtime(const time\_t \*time>
;** This profits a pointer to the tm structure addressing local time. | | 4 | **clock\_t clock(void>
;** This profits a value that approximates how much time the calling program has been running. A value of .1 is returned in the event that the time isn't accessible. | | 5 | **singe \* asctime ( const struct tm \* time >
;** This profits a pointer to a string that contains the information put away in the structure highlighted by time changed over into the form: day month date hours:minutes:seconds year\\n\\0 | | 6 | **struct tm \*gmtime(const time\_t \*time>
;** This profits a pointer to the time as a tm structure. The time is addressed in Coordinated Universal Time (UTC>
, which is basically Greenwich Mean Time (GMT>
. | | 7 | **time\_t mktime(struct tm \*time>
;** This profits the calendar-time likeness the time found in the structure highlighted by time. | | 8 | **twofold difftime ( time\_t time2, time\_t time1 >
;** This function calculates out the distinction in seconds somewhere in the range of time1 and time2. | | 9 | **size\_t strftime(>
;** This function can be utilized to arrange date and time in a particular format. | #### Current Date and Time Following is the example to retrieve the current date and time ```plaintext #include #include using namespace std; int main(>
{ // current date/time based on current system time_t now = time(0>
; // convert now to string form char* dt = ctime(&now>
; cout << "The local date and time is: " << dt << endl; // convert now to tm struct for UTC tm *gmtm = gmtime(&now>
; dt = asctime(gmtm>
; cout << "The UTC date and time is:"<< dt << endl; } ``` The c++ & symbol is used as an operator in C++. geeksforgeeks C++ is a general-purpose programming language and is widely used nowadays for competitive programming. The major purpose of oops in C++ programming is to introduce the concept of object orientation to the C programming language. turbo c++ download for windows 10 and Windows 11 (32-64 bit>
with full-screen mode and many more extra features.