(原創) 如何将秒数转换成day?hour?min?...(localtime()) (C/C++) (C)
.NET Framework的DateTime型别本身就有转Day,Hour,Minute等Method,所以要转十分方便。但C/C++呢?
C/C++的<time.h>(<ctime>)提供了localtime(),可将sec一次转成所要的信息,该struct定义如下
1
struct tm {
2
int tm_sec; // sec.
3
int tm_min; // min.
4
int tm_hour; // hour
5
int tm_mday; // day
6
int tm_mon; // month
7
int tm_year; // year
8
int tm_wday; // day of week
9
int tm_yday; // day of year
10
int tm_isdst; // 夏令时间
11
}
struct tm {2
int tm_sec; // sec.3
int tm_min; // min.4
int tm_hour; // hour5
int tm_mday; // day6
int tm_mon; // month7
int tm_year; // year8
int tm_wday; // day of week9
int tm_yday; // day of year10
int tm_isdst; // 夏令时间 11
}以下为localtime()的范例程序
1
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : localtime.cpp
5
Compiler : Visual C++ 8.0
6
Description : Demo how to use localtime() to get min part & hour part
7
Release : 11/26/2006
8
*/
9
#include <iostream>
10
#include <ctime>
11
12
// Time-wasting function
13
void foo();
14
// Get specified floating point number from double
15
int getDigitFromDouble(double, int);
16
17
int main() {
18
clock_t t1 = clock();
19
// Time-wasting function
20
foo();
21
clock_t t2 = clock();
22
23
// Total sec., but including floating part, not only integer
24
double sec = (double)(t2 - t1) / CLOCKS_PER_SEC;
25
// 0.1 sec. part
26
int ssec = getDigitFromDouble(sec,1);
27
// Convert dobule to const time_t,
28
// Since localtime needs (const time_t*) parameter
29
const time_t time_t_sec = (time_t)sec;
30
// localtime() return (struct *tm)
31
struct tm* ts = localtime(&time_t_sec);
32
33
std::cout << "Total sec:" << sec << std::endl;
34
std::cout << "min:" << ts->tm_min << std::endl;
35
std::cout << "sec:" << ts->tm_sec << std::endl;
36
std::cout << "0.1 sec:" << ssec << std::endl;
37
38
return 0;
39
}
40
41
void foo() {
42
for(int i = 0; i != 1000000; ++i)
43
for(int j = 0; j != 100000; ++j);
44
}
45
46
// get specified floating point number from double
47
int getDigitFromDouble(double d, int n) {
48
int t = 1;
49
for(int j = 0; j != n-1; ++j) {
50
t *= 10;
51
}
52
53
d = d * t;
54
int i = (int)d;
55
56
return (int)((double)(d-i)*10);
57
}
/* 2
(C) OOMusou 2006 http://oomusou.cnblogs.com3

4
Filename : localtime.cpp5
Compiler : Visual C++ 8.06
Description : Demo how to use localtime() to get min part & hour part7
Release : 11/26/20068
*/9
#include <iostream>10
#include <ctime>11

12
// Time-wasting function13
void foo();14
// Get specified floating point number from double15
int getDigitFromDouble(double, int);16

17
int main() {18
clock_t t1 = clock();19
// Time-wasting function20
foo();21
clock_t t2 = clock();22

23
// Total sec., but including floating part, not only integer24
double sec = (double)(t2 - t1) / CLOCKS_PER_SEC;25
// 0.1 sec. part26
int ssec = getDigitFromDouble(sec,1);27
// Convert dobule to const time_t, 28
// Since localtime needs (const time_t*) parameter29
const time_t time_t_sec = (time_t)sec;30
// localtime() return (struct *tm)31
struct tm* ts = localtime(&time_t_sec);32

33
std::cout << "Total sec:" << sec << std::endl;34
std::cout << "min:" << ts->tm_min << std::endl;35
std::cout << "sec:" << ts->tm_sec << std::endl;36
std::cout << "0.1 sec:" << ssec << std::endl;37

38
return 0;39
}40

41
void foo() {42
for(int i = 0; i != 1000000; ++i) 43
for(int j = 0; j != 100000; ++j);44
}45

46
// get specified floating point number from double47
int getDigitFromDouble(double d, int n) {48
int t = 1;49
for(int j = 0; j != n-1; ++j) {50
t *= 10;51
}52

53
d = d * t;54
int i = (int)d;55

56
return (int)((double)(d-i)*10);57
}本范例为了抓到0.1秒,所以才用了clock(),一般若只想抓到sec.,用time()即可。
See Also
(原創) 何抓出小数部份第n位数字? (C/C++)
Reference
C程序设计 500个应用范例技巧大全集 P.473, 平田 丰 着/ 黄正凯 译, 博硕文化


浙公网安备 33010602011771号