Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9139602
  • 博文数量: 1725
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19840
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1725)

文章存档

2024年(1)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Windows平台

2017-04-19 19:27:23


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <time.h>
  3. #include <sys/time.h>


  4. using namespace std;

  5. int main()
  6. {
  7.     time_t t; //秒时间
  8.     tm* local; //本地时间
  9.     tm* gmt; //格林威治时间
  10.     char buf[128]= {0};
  11.   
  12.     t = time(NULL); //获取目前秒时间
  13.     local = localtime(&t); //转为本地时间
  14.     strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);
  15.     std::cout << buf << std::endl;
  16.   
  17.     gmt = gmtime(&t);//转为格林威治时间
  18.     strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);
  19.     std::cout << buf << std::endl;


  20.     struct timeval tv;
  21.     struct timezone tz;
  22.     gettimeofday(&tv, &tz);
  23.     t = tv.tv_sec;

  24.     local = localtime(&t); //转为本地时间
  25.     strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);
  26.     std::cout << buf << std::endl;

  27.     gmt = gmtime(&t);//转为格林威治时间
  28.     strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);
  29.     std::cout << buf << std::endl;
  30.     

  31. }
输出
2017-04-19 19:26:45
2017-04-19 11:26:45
2017-04-19 19:26:45
2017-04-19 11:26:45
$ date  实际时间
2017年 04月 19日 星期三 19:26:47 CST

可以看到



http://blog.csdn.net/ncepubdtb/article/details/38899505

  1. 1、常用的时间存储方式  
  2.   
  3. 1)time_t类型,这本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。  
  4.   
  5. 2)tm结构,这本质上是一个结构体,里面包含了各时间字段  
  6.   
  7. struct tm {  
  8.         int tm_sec;     /* seconds after the minute - [0,59] */  
  9.         int tm_min;     /* minutes after the hour - [0,59] */  
  10.         int tm_hour;    /* hours since midnight - [0,23] */  
  11.         int tm_mday;    /* day of the month - [1,31] */  
  12.         int tm_mon;     /* months since January - [0,11] */  
  13.         int tm_year;    /* years since 1900 */  
  14.         int tm_wday;    /* days since Sunday - [0,6] */  
  15.         int tm_yday;    /* days since January 1 - [0,365] */  
  16.         int tm_isdst;   /* daylight savings time flag */  
  17.         };  
  18.   
  19. 其中tm_year表示从1900年到目前计时时间间隔多少年,如果是手动设置值的话,tm_isdst通常取值-1。  
  20.   
  21. 2、常用的时间函数  
  22.   
  23. time_t time(time_t *t); //取得从1970年1月1日至今的秒数  
  24. char *asctime(const struct tm *tm); //将结构中的信息转换为真实世界的时间,以字符串的形式显示  
  25. char *ctime(const time_t *timep); //将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样  
  26. struct tm *gmtime(const time_t *timep); //将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针   
  27. struct tm *localtime(const time_t *timep); //和gmtime类似,但是它是经过时区转换的时间。  
  28. time_t mktime(struct tm *tm); //将struct tm 结构的时间转换为从1970年至今的秒数  
  29. int gettimeofday(struct timeval *tv, struct timezone *tz); //返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用  
  30. double difftime(time_t time1, time_t time2); //返回两个时间相差的秒数  
  31.   
  32.   
  33. 3、时间与字符串的转换  
  34.   
  35. 需要包含的头文件如下  
  36.   
  37. #include   
  38. #include   
  39. #include   
  40. #include   
  41.   
  42. 1)unix/windows下时间转字符串参考代码  
  43.   
  44. time_t t;  //秒时间  
  45. tm* local; //本地时间   
  46. tm* gmt;   //格林威治时间  
  47. char buf[128]= {0};  
  48.   
  49. t = time(NULL); //获取目前秒时间  
  50. local = localtime(&t); //转为本地时间  
  51. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);  
  52. std::cout << buf << std::endl;  
  53.   
  54. gmt = gmtime(&t);//转为格林威治时间  
  55. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);  
  56. std::cout << buf << std::endl;  
  57.   
  58.   
  59.   
  60. 2)unix字符串转时间参考代码  
  61.   
  62.   
  63. tm tm_;  
  64. time_t t_;  
  65. char buf[128]= {0};  
  66.   
  67. strcpy(buf, "2012-01-01 14:00:00");  
  68. strptime(buf, "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间  
  69. tm_.tm_isdst = -1;  
  70. t_  = mktime(&tm_); //将tm时间转换为秒时间  
  71. t_ += 3600;  //秒数加3600  
  72.   
  73. tm_ = *localtime(&t_);//输出时间  
  74. strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_);  
  75. std::cout << buf << std::endl;  
  76.   
  77.   
  78.   
  79.  3)由于windows下没有strptime函数,所以可以使用scanf来格式化  
  80.   
  81.   
  82. time_t StringToDatetime(char *str)  
  83. {  
  84.     tm tm_;  
  85.     int year, month, day, hour, minute,second;  
  86.     sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);  
  87.     tm_.tm_year  = year-1900;  
  88.     tm_.tm_mon   = month-1;  
  89.     tm_.tm_mday  = day;  
  90.     tm_.tm_hour  = hour;  
  91.     tm_.tm_min   = minute;  
  92.     tm_.tm_sec   = second;  
  93.     tm_.tm_isdst = 0;  
  94.   
  95.     time_t t_ = mktime(&tm_); //已经减了8个时区  
  96.     return t_; //秒时间  
  97. }  





阅读(3881) | 评论(0) | 转发(0) |
0

上一篇:理解傅里叶分析

下一篇:C++ STL

给主人留下些什么吧!~~