(原創) 如何将秒数转换成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 ExpandedBlockStart.gif ContractedBlock.gif struct  tm  dot.gif {
 2InBlock.gif  int tm_sec;    // sec.
 3InBlock.gif  int tm_min;   // min.
 4InBlock.gif  int tm_hour;  // hour
 5InBlock.gif  int tm_mday; // day
 6InBlock.gif  int tm_mon;  // month
 7InBlock.gif  int tm_year;  // year
 8InBlock.gif  int tm_wday; // day of week
 9InBlock.gif  int tm_yday;  // day of year
10InBlock.gif  int tm_isdst;  // 夏令时间 
11ExpandedBlockEnd.gif}

以下为localtime()的范例程序
 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : localtime.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0
 6InBlock.gifDescription : Demo how to use localtime() to get min part & hour part
 7InBlock.gifRelease     : 11/26/2006
 8ExpandedBlockEnd.gif*/

 9 None.gif#include  < iostream >
10 None.gif#include  < ctime >
11 None.gif
12 None.gif //  Time-wasting function
13 None.gif void  foo();
14 None.gif //  Get specified floating point number from double
15 None.gif int  getDigitFromDouble( double int );
16 None.gif
17 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
18InBlock.gif  clock_t t1 = clock();
19InBlock.gif  // Time-wasting function
20InBlock.gif  foo();
21InBlock.gif  clock_t t2 = clock();
22InBlock.gif
23InBlock.gif  // Total sec., but including floating part, not only integer
24InBlock.gif  double sec = (double)(t2 - t1) / CLOCKS_PER_SEC;
25InBlock.gif  // 0.1 sec. part
26InBlock.gif  int   ssec = getDigitFromDouble(sec,1);
27InBlock.gif  // Convert dobule to const time_t, 
28InBlock.gif  // Since localtime needs (const time_t*) parameter
29InBlock.gif  const time_t time_t_sec = (time_t)sec;
30InBlock.gif  // localtime() return (struct *tm)
31InBlock.gif  struct tm* ts = localtime(&time_t_sec);
32InBlock.gif
33InBlock.gif  std::cout << "Total sec:" << sec << std::endl;
34InBlock.gif  std::cout << "min:" << ts->tm_min << std::endl;
35InBlock.gif  std::cout << "sec:" << ts->tm_sec << std::endl;
36InBlock.gif  std::cout << "0.1 sec:" << ssec << std::endl;
37InBlock.gif
38InBlock.gif  return 0;
39ExpandedBlockEnd.gif}

40 None.gif
41 ExpandedBlockStart.gifContractedBlock.gif void  foo()  dot.gif {
42InBlock.gif  for(int i = 0; i != 1000000++i) 
43InBlock.gif    for(int j = 0; j != 100000++j);
44ExpandedBlockEnd.gif}

45 None.gif
46 None.gif //  get specified floating point number from double
47 ExpandedBlockStart.gifContractedBlock.gif int  getDigitFromDouble( double  d,  int  n)  dot.gif {
48InBlock.gif  int t = 1;
49ExpandedSubBlockStart.gifContractedSubBlock.gif  for(int j = 0; j != n-1++j) dot.gif{
50InBlock.gif    t *= 10;
51ExpandedSubBlockEnd.gif  }

52InBlock.gif
53InBlock.gif  d = d * t;
54InBlock.gif  int i = (int)d;
55InBlock.gif
56InBlock.gif  return (int)((double)(d-i)*10);
57ExpandedBlockEnd.gif}

本范例为了抓到0.1秒,所以才用了clock(),一般若只想抓到sec.,用time()即可。

See Also
(原創) 何抓出小数部份第n位数字? (C/C++)

Reference
C程序设计 500个应用范例技巧大全集 P.473, 平田 丰 着/ 黄正凯 译, 博硕文化
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值