-
Notifications
You must be signed in to change notification settings - Fork 0
/
Defines.cpp
80 lines (71 loc) · 1.96 KB
/
Defines.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Defines.h"
QDir directoryOf(const QString &subdir) {
static bool firstdir = true;
static QDir dir(QApplication::applicationDirPath());
if (!firstdir){
return dir;
}
#if defined(Q_OS_WIN)
if (dir.dirName().toLower() == "debug"
|| dir.dirName().toLower() == "release"
|| dir.dirName().toLower() == "bin")
dir.cdUp();
#elif defined(Q_OS_MAC)
if (dir.dirName() == "MacOS"){
dir.cdUp();
dir.cdUp();
dir.cdUp();
}
#endif
dir.cd(subdir);
firstdir = false;
return dir;
}
QString GetFileDir(QString file){
#if !defined(WIN32)
file = file.replace("\\","//");
#endif
return directoryOf("").absoluteFilePath(file);
}
string GetStdFileDir(string file){
return GetFileDir(QString::fromStdString(file)).toStdString();
}
bool IsLeapYear(int year){
return (year%400==0||(year%100!=0&&year%4==0));
}
int YearHaveDay(int year){
if (IsLeapYear(year))return 366;
return 365;
}
int MonthHaveDay(int month,bool leap){
if (month==4||month==6||month==9||month==11)return 30;
if (month==2)return leap?29:28;
return 31;
}
Date GetDate(int offsetHour){
int t = time(0) + offsetHour * 60 *60;
Date d;
d.second = t % 60;
t /= 60;
d.minute = t % 60;
t /= 60;
d.hour = t % 24;
t /= 24;
//从1970年1月1日开始偏移日期
d.year = 1970;
d.month = 1;
d.day = 1;
int temp;
while(t>=(temp=YearHaveDay(d.year))){t-=temp;d.year++;}
while(t>=(temp=MonthHaveDay(d.month,IsLeapYear(d.year)))){t-=temp;d.month++;}
d.day += t;
return d;
}
int GetClock(){
return clock() * 1000 / CLOCKS_PER_SEC;
time_t ltime1;
struct TIMEB tstruct1;
ftime (&tstruct1); // start time ms
time (<ime1); // start time s
return ltime1 * 1000 + tstruct1.millitm;
}