Mark As Completed Discussion

Welcome to the "STL Time Library" section of the "STL Library" lesson!

As a senior engineer interested in networking and engineering in C++ as it pertains to finance, the time library in the STL provides functionality for working with time and dates in C++.

Let's take a look at an example:

TEXT/X-C++SRC
1#include <iostream>
2#include <ctime>
3
4int main() {
5  // Get current system time
6  time_t now = time(0);
7
8  // Convert now to string form
9  char* dt = ctime(&now);
10
11  // Print the current time
12  std::cout << "The current date and time is: " << dt;
13
14  // Convert now to tm struct for UTC
15  tm* gmtm = gmtime(&now);
16  dt = asctime(gmtm);
17
18  // Print the current UTC time
19  std::cout << "The current UTC date and time is: " << dt;
20
21  // Convert now to tm struct for local timezone
22  tm* ltm = localtime(&now);
23  dt = asctime(ltm);
24
25  // Print the current local time
26  std::cout << "The current local date and time is: " << dt;
27
28  return 0;
29}
CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment