-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimelogic.cpp
More file actions
95 lines (75 loc) · 2.37 KB
/
timelogic.cpp
File metadata and controls
95 lines (75 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <ctime>
#include <iostream>
#include<fstream>
#include <QStandardPaths>
#include <stdio.h>
#include <time.h>
#include <chrono>
using namespace std;
// C++ Program to implement Date and Time Parsing using
// chrono
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
// function to parse a date or time string.
chrono::system_clock::time_point GFG(const string& datetimeString, const string& format)
{
tm tmStruct = {};
istringstream ss(datetimeString);
ss >> get_time(&tmStruct, format.c_str());
return chrono::system_clock::from_time_t(
mktime(&tmStruct));
}
// Function to format a time_t value into a date or time string.
string DateTime(const chrono::system_clock::time_point& timePoint,
const string& format)
{
time_t time
= chrono::system_clock::to_time_t(timePoint);
tm* timeinfo = localtime(&time);
char buffer[70];
strftime(buffer, sizeof(buffer), format.c_str(),
timeinfo);
return buffer;
}
void update_date()
{
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/neophyte/";
/*delete all problems */
std::ofstream ofs;
QString path_date = path +"date.txt";
std::string path_date_str = path_date.toUtf8().constData();
const char * c = path_date_str.c_str();
ofs.open(c, std::ofstream::out | std::ofstream::trunc);
ofs.close();
FILE * fp;
/* open the file for writing*/
fp = fopen(c,"w");
/* write time as text into the file stream*/
char cur_time[128];
time_t t;
struct tm* ptm;
t = time(NULL);
ptm = localtime(&t);
ptm->tm_mday+=1;
time_t t2=mktime(ptm);
ptm=localtime(&t2);
strftime(cur_time, 128, "%Y-%m-%d %H:%M:%S", ptm);
fprintf(fp, "%s\n", cur_time);
/* close the file*/
fclose(fp);
std::time_t result = std::time(nullptr);
std::cout << std::asctime(std::localtime(&result))
<< result << " seconds since the Epoch\n";
const string datetimeString = "2023-05-22 12:24:52";
const string format = "%Y-%m-%d %H:%M:%S";
chrono::system_clock::time_point parsedTime
= GFG(datetimeString, format);
string formattedTime = DateTime(parsedTime, format);
/*cout << "Parsed Time---> "
<< chrono::system_clock::to_time_t(parsedTime)
<< endl;*/
cout << "Formatted Time---> " << formattedTime << endl;
}