-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeUtils.cpp
executable file
·60 lines (53 loc) · 1.56 KB
/
TimeUtils.cpp
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
//Routines that get the system date and time and produce them as a formatted strings
#include "TimeUtils.h"
void getSystemTime(int& hrs, int& mins, int& secs)
{ //get time from system
//set to system time
time_t now(time(0));
struct tm t;
localtime_s(&t, &now);
hrs = t.tm_hour ;
mins = t.tm_min ;
secs = t.tm_sec ;
}
const string timeToString(int h, int m, int s)
{ //convert the time to a string in 24-h digital clock format (00:00:00)
ostringstream os;
const char prev(os.fill ('0'));
os << setw(2) << h << ":"
<< setw(2) << m << ":"
<< setw(2) << s;
os.fill(prev);
return os.str();
}
const string getTime()
{ //return the current time in a string format
int hrs, mins, secs; //hold the current time
getSystemTime(hrs, mins, secs);
return timeToString(hrs, mins, secs);
}
void getSystemDate(int& day, int& month, int& year)
{ //get date from system
time_t now(time(0));
struct tm t;
localtime_s(&t, &now);
day = t.tm_mday;
month = t.tm_mon + 1;
year = t.tm_year + 1900;
}
const string dateToString(int day, int month, int year)
{ //convert the date to a string in format (dd/mm/yyyy)
ostringstream os;
const char prev(os.fill ('0'));
os << setw(2) << day << "/"
<< setw(2) << month << "/"
<< setw(4) << year;
os.fill(prev);
return os.str();
}
const string getDate()
{ //return the current date in a string format
int day, month, year; //hold the current date
getSystemDate(day, month, year);
return dateToString(day, month, year);
}