zoukankan      html  css  js  c++  java
  • 时间处理 c++ 获取当前系统时间 1. 时间戳形式 2. char *形式[转]

    1. 获取时间戳:

    time_t rawtime;

    time(&rawtime);  //结果是时间戳

    2. 将时间戳转为 char* 类型的日期 Www Mmm dd hh:mm:ss yyyy

    复制代码
     1 /* ctime example */
     2 #include <stdio.h>
     3 #include <time.h>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 int main ()
     9 {
    10   time_t rawtime;
    11 
    12   time ( &rawtime );
    13   printf("%ld\n", &rawtime);
    14   printf ( "The current local time is: %s", ctime (&rawtime) );
    15   
    16   char * time = ctime(&rawtime);
    17   //ctime(&rawtime) : time_t/timestampe -> "Www Mmm dd hh:mm:ss yyyy" format
    18   cout<< time << endl;
    19   printf("%s", asctime(gmtime(&rawtime)));// asctime(gmtime(&rawtime)) = ctime(&rawtime);
    20   //here gmtime(&rawtime) : time_t(timpstampe) -> struct tm
    21   //here asctime(gmtime) : struct tm -> "Www Mmm dd hh:mm:ss yyyy" format 
    22   
    23   
    24   struct tm * ptm;
    25   ptm = gmtime(&rawtime);
    26   
    27   cout<<(ptm->tm_year + 1900)<<"year "<<(ptm->tm_mon + 1)<<"month "<<(ptm->tm_mday)<<"day "<<(ptm
    28   ->tm_hour)<<":"<<(ptm->tm_min)<<":"<<(ptm->tm_sec)<<endl;
    29   
    30 
    31   return 0;
    32 }
    复制代码

     

    结果:

    134507764  //time ( &rawtime );  printf("%ld\n", &rawtime);
    The current local time is: Mon Jul  9 15:17:03 2012  //printf ( "The current local time is: %s", ctime (&rawtime) );
    Mon Jul  9 15:17:03 2012  //char * time = ctime(&rawtime);

    Mon Jul  9 13:17:03 2012  //printf("%s", asctime(gmtime(&rawtime)));
    2012year 7month 9day 13:17:3  //cout<<(ptm->tm_year + 1900)<<"year "<<(ptm->tm_mon + 1)<<"month "<<(ptm->tm_mday)<<"day "<<(ptm 28 ->tm_hour)<<":"<<(ptm->tm_min)          <<":"<<(ptm->tm_sec)<<endl;

    3.

     

    结果 输出 UTC 标准时间 12:49  //时间协调时间

    ********************

    关于时间 定义的 struct tm的介绍:

     
    type

    struct tm

    <ctime>
    Time structure
    Structure containing a calendar date and time broken down into its components.

    The structure contains nine members of type int, which are (in any order):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
    而直接存储年月日的是一个结构:
    struct tm
    {
        int tm_sec;  /*秒,正常范围0-59, 但允许至61*/
        int tm_min;  /*分钟,0-59*/
        int tm_hour; /*小时, 0-23*/
        int tm_mday; /*日,即一个月中的第几天,1-31*/
        int tm_mon;  /*月, 从一月算起,0-11*/  1+p->tm_mon;
        int tm_year;  /*年, 从1900至今已经多少年*/ 1900+ p->tm_year;
        int tm_wday; /*星期,一周中的第几天, 从星期日算起,0-6*/
        int tm_yday; /*从今年1月1日到目前的天数,范围0-365*/
        int tm_isdst; /*日光节约时间的旗标*/
    };

    需要特别注意的是,年份是从1900年起至今多少年,而不是直接存储如2011年,月份从0开始的,0表示一月星期也是从0开始的, 0表示星期日,1表示星期一。



    The meaning of each is:
    MemberMeaningRange
    tm_secseconds after the minute0-61*
    tm_minminutes after the hour0-59
    tm_hourhours since midnight0-23
    tm_mdayday of the month1-31
    tm_monmonths since January0-11real month = tm_mon + 1
    tm_yearyears since 1900 1900+tm_year
    tm_wdaydays since Sunday0-6
    tm_ydaydays since January 10-365
    tm_isdstDaylight Saving Time flag 
    The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.

    tm_sec is generally 0-59. Extra range to accommodate for leap seconds in certain systems.
  • 相关阅读:
    Google Code 项目代码托管网站上 Git 版本控制系统使用简明教程
    C/C++预定义宏
    使用 Raspberry Pi 远程桌面
    Vim 中将 tab 自动转换成空格
    DR模式搭建LVS负载均衡
    NAT模式LVS搭建负载均衡集群
    php扩展memached安装
    raw_input与input的区别
    keepalived+lvs搭建高可用负载均衡集群
    使用keepalived搭建nginx高可用
  • 原文地址:https://www.cnblogs.com/rooney/p/2708378.html
Copyright © 2011-2022 走看看