时间转换

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