Design Pattern Examples
Overview of object-oriented design patterns
DateTime.cpp
Go to the documentation of this file.
1
5
6#include <chrono>
7
8#include "DateTime.h"
9#include "makelocaltime.h"
10
11namespace Helpers
12{
13
15 // ToString() method
17 std::string DateTime::ToString()
18 {
19 std::string time_as_string;
20 struct tm local_time = { };
21 struct tm* retval = Helpers::makelocaltime(&timestamp, &local_time);
22 if (retval != nullptr)
23 {
24 char str[128]{ '\0' };
25 strftime(str, sizeof str, "%m/%d/%Y %r", &local_time);
26 time_as_string = str;
27 }
28
29 return time_as_string;
30 }
31
33 // Now() method
36 {
37 auto now_time = std::chrono::system_clock::now();
38 auto now_time_t = std::chrono::system_clock::to_time_t(now_time);
39 return DateTime(now_time_t);
40 }
41
42} // end namespace
Represents a timestamp composed of a date and a time encoded in a time_t value. Provides ways of gett...
static DateTime Now()
Return the current date and time.
Definition: DateTime.cpp:35
DateTime()
Default constructor.
std::string ToString()
Format the DateTime as a string. The format is "standard" (in this case, preset to 02/22/2023 10:26:1...
Definition: DateTime.cpp:17
Declaration of the DateTime class to simplify getting the local time as a string (modeled after the C...
Declaration of the makelocaltime() function to convert a time_t to a struct tm containing the local t...
The namespace containing all the "helper" functions in the C++ code.
struct tm * makelocaltime(const time_t *time, struct tm *timestruct)
Convert the given time to local time.