Skip to content

Commit 87c070d

Browse files
committed
date and time temp update
1 parent 68b2640 commit 87c070d

File tree

3 files changed

+198
-5
lines changed

3 files changed

+198
-5
lines changed

docs/date_time.md

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Date and Time in C
1+
# 1. Date and Time in C
22
1. `std::time()`: It returns the current time (seconds since 00:00:00 GMT, Jan 1, 1970) as an object of type `time_t`. `time_t` is defined as `int64` under the hood.
33

44
2. `gmtime (time_t *timer )`: converts given time since epoch into calendar time, expressed in Coordinated Universal Time (UTC) in the `struct tm` format.
@@ -24,10 +24,79 @@ int main()
2424
}
2525
```
2626

27-
# Date and Time in C++
27+
# 2. Date and Time in C++
2828

29-
`std::chrono::time_point`
29+
`template <class Clock, class Duration> class time_point;`: This class expresses a point in time relative to a clock's epoch.
3030

31-
`class system_clock`: It is the only C++ clock that has the ability to map its time points to C-style time
31+
## 2.1 Clock class:
32+
1. `system_clock`: is a system-wide realtime clock.
33+
2. `steady_clock`: is specifically designed to calculate time intervals
34+
3. `high_resolution_clock`: is the clock with the shortest tick period. It may be a synonym for `system_clock` or `steady_clock`.
35+
36+
## 2.2 Duration class
37+
38+
`template <class Rep, class Period = ratio<1> >class duration;`
39+
A duration object expresses a time span by means of a **count** (stored as an object of member type `rep`) and a **period**.
40+
41+
42+
43+
44+
```
45+
template<
46+
class Rep,
47+
class Period = std::ratio<1>
48+
> class duration;
49+
```
50+
Consists of the type of the tick `Rep` and the length of a tick `Period`.
51+
52+
1. Rep: an arithmetic type representing the number of ticks
53+
2. Period: a `std::ratio` representing the tick period (number of second's fractions per tick)
54+
- `std::ratio<1,1>` stands for a second.
55+
- `std::ratio<1,60>` is a minute.
56+
- `std::ratio<1,1000>` is a millisecond.
57+
58+
Predefined durations:
59+
60+
```
61+
std::chrono::nanoseconds = typedef duration<signed int, std::nano> nanoseconds;
62+
std::chrono::microseconds = typedef duration<signed int, std::micro> microseconds;
63+
std::chrono::hours = typedef duration<signed int, ratio<3600>> hours;
64+
```
65+
66+
67+
68+
## 2.3. std::chrono::time_point_cast
69+
70+
Examples:
71+
72+
`std::chrono::system_clock::now()` returns `std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>`
73+
74+
75+
76+
77+
Different durations are not implicitly convertible to one another, You can have to convert time points with different durations using `std::chrono::time_point_cast`
78+
79+
```cpp
80+
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> timePoint =
81+
std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now());
82+
```
83+
84+
Refs: [1](https://stackoverflow.com/questions/15777073/how-do-you-print-a-c11-time-point), [2](https://www.modernescpp.com/index.php/time-duration),
85+
[3](https://en.cppreference.com/w/cpp/chrono/duration)
86+
87+
88+
## 2.4. std::chrono::duration_cast
89+
90+
```cpp
91+
const auto now = std::chrono::system_clock::now();
92+
time_t time = std::chrono::system_clock::to_time_t(now);
93+
std::cout << time << "\n";
94+
std::cout << std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() << "\n";
95+
std::cout << std::chrono::system_clock::from_time_t(time).time_since_epoch().count() << "\n";
96+
std::cout << std::chrono::system_clock::time_point(std::chrono::seconds(time)).time_since_epoch().count() << "\n";
97+
```
98+
99+
Refs: [1](https://stackoverflow.com/questions/66346389/stdchronosystem-clock-and-c-time), [2](https://en.cppreference.com/w/cpp/chrono/duration/duration_cast)
100+
,[3](https://stackoverflow.com/questions/51538022/how-the-time-point-created-with-different-durationstdchronomilliseconds-and)
32101

33102
[src](../src/date_time.cpp)

src/date_time.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void cTime() {
3434
time_t epoch = std::time(NULL);
3535

3636
std::cout << "seconds since the epoch began: " << epoch << std::endl;
37+
3738
tm *t = gmtime(&epoch);
3839

3940
std::cout << "current date and time: " << 1900 + t->tm_year << "/"
@@ -78,5 +79,40 @@ int main() {
7879

7980
// std::cout << t_c << std::endl;
8081

82+
// std::chrono::nanoseconds is : typedef duration<signed int, std::nano>
83+
// nanoseconds;
84+
85+
// timePoint = std::chrono::system_clock::now();
86+
87+
// std::chrono::system_clock::to_time_t(timePoint);
88+
89+
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
90+
timePoint = std::chrono::time_point_cast<std::chrono::nanoseconds>(
91+
std::chrono::system_clock::now());
92+
93+
std::cout << timePoint << std::endl;
94+
// std::cout << std::chrono::system_clock::to_time_t(timePoint) << std::endl;
95+
96+
std::cout << timePoint.time_since_epoch() << std::endl;
97+
98+
{
99+
const auto now = std::chrono::system_clock::now();
100+
time_t time = std::chrono::system_clock::to_time_t(now);
101+
std::cout << time << "\n";
102+
std::cout << std::chrono::duration_cast<std::chrono::seconds>(
103+
now.time_since_epoch())
104+
.count()
105+
<< "\n";
106+
std::cout << std::chrono::system_clock::from_time_t(time)
107+
.time_since_epoch()
108+
.count()
109+
<< "\n";
110+
std::cout << std::chrono::system_clock::time_point(
111+
std::chrono::seconds(time))
112+
.time_since_epoch()
113+
.count()
114+
<< "\n";
115+
}
116+
81117
return 0;
82118
}

src/regex_mathch_search.cpp

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,92 @@ int main(int argc, char **argv) {
102102
std::cout << "match: " << match.suffix().str() << '\n';
103103
std::cout << "match: " << match.prefix().str() << '\n';
104104
}
105-
}
105+
106+
using namespace std::regex_constants;
107+
108+
std::cmatch m;
109+
110+
// std::regex_match("subject", m, std::regex("(sub)(.*)"));
111+
112+
std::regex_match(
113+
"00000-2023-02-23_16-13-23.ulg_"
114+
"06531353AC6AFC26C7633316B317B58A680E5D59039EE71E6D09B0A950A5F087.hash",
115+
m, std::regex("00000-2023-02-23_16-13-23.ulg_[A-F0-9]*\\.hash"));
116+
117+
std::cout << "--------------"
118+
<< "\n";
119+
std::string output = "matches:\n";
120+
for (std::cmatch::iterator it = m.begin(); it != m.end(); ++it) {
121+
std::cout << it->str() << "\n";
122+
std::cout << "************"
123+
<< "\n";
124+
output += it->str() + "\n";
125+
}
126+
std::cout << "......................"
127+
<< "\n";
128+
std::cout << output << std::endl;
129+
130+
std::regex rx("00000-2023-02-23_16-13-23.ulg_[A-F0-9]*\\.hash");
131+
std::cmatch mr;
132+
133+
std::regex_search(
134+
"00000-2023-02-23_16-13-23.ulg_"
135+
"06531353AC6AFC26C7633316B317B58A680E5D59039EE71E6D09B0A950A5F087.hash",
136+
mr, rx);
137+
138+
std::csub_match sub = mr[1];
139+
std::cout << "matched == " << std::boolalpha << sub.matched << std::endl;
140+
std::cout << "length == " << sub.length() << std::endl;
141+
142+
std::csub_match::difference_type dif = std::distance(sub.first, sub.second);
143+
std::cout << "difference == " << dif << std::endl;
144+
145+
std::csub_match::iterator first = sub.first;
146+
std::csub_match::iterator last = sub.second;
147+
std::cout << "range == " << std::string(first, last) << std::endl;
148+
std::cout << "string == " << sub << std::endl;
149+
150+
std::csub_match::value_type const *ptr = "aab";
151+
std::cout << "compare(\"aab\") == " << sub.compare(ptr) << std::endl;
152+
std::cout << "compare(string) == " << sub.compare(std::string("AAA"))
153+
<< std::endl;
154+
std::cout << "compare(sub) == " << sub.compare(sub) << std::endl;
155+
156+
std::string mainStr =
157+
"00000-2023-02-23_16-13-23.ulg_"
158+
"06531353AC6AFC26C7633316B317B58A680E5D59039EE71E6D09B0A950A5F087.hash";
159+
std::string toErase = "00000-2023-02-23_16-13-23.ulg_";
160+
size_t pos = mainStr.find(toErase);
161+
if (pos != std::string::npos) {
162+
// If found then erase it from string
163+
mainStr.erase(pos, toErase.length());
164+
}
165+
166+
toErase = ".hash";
167+
pos = mainStr.find(toErase);
168+
169+
if (pos != std::string::npos) {
170+
// If found then erase it from string
171+
mainStr.erase(pos, toErase.length());
172+
}
173+
174+
std::cout << mainStr;
175+
}
176+
177+
// https://www.regular-expressions.info/dot.html
178+
// https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html#:~:text=The%20%5C%20is%20known%20as%20the,have%20special%20meaning%20in%20regex.
179+
180+
/*
181+
To match a string that contains only those characters (or an empty string), try
182+
"^[a-zA-Z0-9_]*$"
183+
184+
^ : start of string
185+
[ : beginning of character group
186+
a-z : any lowercase letter
187+
A-Z : any uppercase letter
188+
0-9 : any digit
189+
_ : underscore
190+
] : end of character group
191+
* : zero or more of the given characters
192+
$ : end of string
193+
*/

0 commit comments

Comments
 (0)