.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
}
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
2
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以下为localtime()的范例程序
1
/**/
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![None.gif](/Images/OutliningIndicators/None.gif)
12
//
Time-wasting function
13
void
foo();
14
//
Get specified floating point number from double
15
int
getDigitFromDouble(
double
,
int
);
16![None.gif](/Images/OutliningIndicators/None.gif)
17![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
main()
{
18
clock_t t1 = clock();
19
// Time-wasting function
20
foo();
21
clock_t t2 = clock();
22![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
return 0;
39
}
40![None.gif](/Images/OutliningIndicators/None.gif)
41![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
void
foo()
{
42
for(int i = 0; i != 1000000; ++i)
43
for(int j = 0; j != 100000; ++j);
44
}
45![None.gif](/Images/OutliningIndicators/None.gif)
46
//
get specified floating point number from double
47![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
getDigitFromDouble(
double
d,
int
n)
{
48
int t = 1;
49![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int j = 0; j != n-1; ++j)
{
50
t *= 10;
51
}
52![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
d = d * t;
54
int i = (int)d;
55![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
56
return (int)((double)(d-i)*10);
57
}
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
2
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![None.gif](/Images/OutliningIndicators/None.gif)
10
![None.gif](/Images/OutliningIndicators/None.gif)
11
![None.gif](/Images/OutliningIndicators/None.gif)
12
![None.gif](/Images/OutliningIndicators/None.gif)
13
![None.gif](/Images/OutliningIndicators/None.gif)
14
![None.gif](/Images/OutliningIndicators/None.gif)
15
![None.gif](/Images/OutliningIndicators/None.gif)
16
![None.gif](/Images/OutliningIndicators/None.gif)
17
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
23
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
35
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
37
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
40
![None.gif](/Images/OutliningIndicators/None.gif)
41
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
42
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
45
![None.gif](/Images/OutliningIndicators/None.gif)
46
![None.gif](/Images/OutliningIndicators/None.gif)
47
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
48
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
49
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
50
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
54
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
56
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
57
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
本范例为了抓到0.1秒,所以才用了clock(),一般若只想抓到sec.,用time()即可。
See Also
(原創) 何抓出小数部份第n位数字? (C/C++)
Reference
C程序设计 500个应用范例技巧大全集 P.473, 平田 丰 着/ 黄正凯 译, 博硕文化