Skip to content

Commit 39d0390

Browse files
committed
Add an example for DateTime
1 parent ccab5b1 commit 39d0390

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ set(APP_SOURCES
172172
"src/core/overloading/AssignmentOperator.cpp"
173173
"src/core/overloading/ClassMemberAccessOperator.cpp"
174174
"src/core/overloading/AllocationOperator.cpp"
175+
## Date and Time
176+
"src/core/datetime/CTime.cpp"
175177
)
176178

177179
# Test files

src/core/datetime/CTime.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include "ExampleRegistry.h"
3+
4+
#include <ctime>
5+
6+
namespace {
7+
void run() {
8+
// **1. Get the current timestamp**
9+
std::time_t now = time(NULL);
10+
const auto* ts = ctime(&now);
11+
std::cout << "Current time: " << ts << '\n';
12+
13+
// **2. Create the dt structure**
14+
struct tm datetime = *localtime(&now);
15+
std::cout << "Year: " << datetime.tm_year + 1900 << '\n';
16+
std::cout << "Current time stamp: " << mktime(&datetime) << '\n';
17+
18+
// **3. Formatting**
19+
char output[50];
20+
strftime(output, 50, "%B %e, %Y", &datetime);
21+
std::cout << output << "\n";
22+
strftime(output, 50, "%I:%M:%S %p", &datetime);
23+
std::cout << output << "\n";
24+
strftime(output, 50, "%m/%d/%y", &datetime);
25+
std::cout << output << "\n";
26+
strftime(output, 50, "%a %b %e %H:%M:%S %Y", &datetime);
27+
std::cout << output << "\n";
28+
}
29+
} // namespace
30+
31+
class CTime : public IExample {
32+
public:
33+
std::string group() const override { return "core/datetime"; }
34+
std::string name() const override { return "CTime"; }
35+
std::string description() const override { return ""; }
36+
void execute() override { run(); }
37+
};
38+
39+
REGISTER_EXAMPLE(CTime, "core/datetime", "CTime");

src/core/datetime/REAME.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Date and Time
2+
- <ctime> : provides functions and types to work with date and time values including parsing and formatting (inherited from C)
3+
4+
- <chrono>: provides facilities to deal with the duration time points and clocks (std11), it is more modern and type-safe approach
5+
6+
## ctime
7+
- Data Types:
8+
- `time_t`: for timestamps
9+
- `struct tm`: for datetime structes
10+
- Use `mktime()` to convert struct tm to time_t, and `localtime()` and `gmtime()` for vice versa
11+
- `strftime()` format:
12+
>%a Short representation of the weekday Fri
13+
%b Short representation of the month name Dec
14+
%B Full representation of the month name December
15+
%d Day of the month with leading zero 09
16+
%e Day of the month with leading spaces 9
17+
%H 24-hour format of an hour 14
18+
%I 12-hour format of an hour 02
19+
%M Minutes within an hour 30
20+
%p AM or PM PM
21+
%S Seconds within a minute 01
22+
%y 2-digit year representation 23
23+
%Y 4-digit year representation 2023

0 commit comments

Comments
 (0)