zoukankan      html  css  js  c++  java
  • C和C++如何来编写时间的程序[转]

      unix时间相关,也是在标准库里面的。

        1.timegm函数只是将struct tm结构转成time_t结构,不使用时区信息

        time_t timegm(struct tm *tm);

        2.mktime使用时区信息

        time_t mktime(struct tm *tm);

        timelocal 函数是GNU扩展的与posix函数mktime相当

        time_t timelocal (struct tm *tm);

        3.gmtime函数只是将time_t结构转成struct tm结构,不使用时区信息

        struct tm * gmtime(const time_t *clock);

        4.localtime使用时区信息

        struct tm * localtime(const time_t *clock);

        1.time获取时间,stime设置时间

        time_t t;

        t = time(&t);

        2.stime其参数应该是GMT时间,根据本地时区设置为本地时间;

        int stime(time_t *tp)

        3.UTC=true 表示采用夏时制;

        4.文件的修改时间等信息全部采用GMT时间存放,不同的系统在得到修改时间后通过localtime转换成本地时间;

        5.设置时区推荐使用setup来设置;

        6.设置时区也可以先更变/etc/sysconfig/clock中的设置 再将ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效

        time_t只能表示68年的范围,即mktime只能返回1970-2038这一段范围的time_t

        看看你的系统是否有time_t64,它能表示更大的时间范围

        Window里面的一些不一样的

        CTime MFC类,好像就是把time.h封了个类,没扩展

        CTime t = GetCurrentTime();

        SYSTEMTIME 结构包含毫秒信息

        typedef struct _SYSTEMTIME {

        WORD wYear;

        WORD wMonth;

        WORD wDayOfWeek;

        WORD wDay;

        WORD wHour;

        WORD wMinute;

        WORD wSecond;

        WORD wMilliseconds;

        } SYSTEMTIME, *PSYSTEMTIME;

        SYSTEMTIME t1;

        GetSystemTime(&t1)

        CTime curTime(t1);

        WORD ms = t1.wMilliseconds;

        SYSTEMTIME sysTm;

        ::GetLocalTime(&sysTm);

        在time.h中的_strtime() //只能在windows中用

        char t[11];

        _strtime(t);

        puts(t);

        /***************************************************************/

        _timeb定义在SYS\TIMEB.H,有四个fields

        dstflag

        millitm

        time

        timezone

        void _ftime( struct _timeb *timeptr );

        struct _timeb timebuffer;

        _ftime( &timebuffer );

        取当前时间:文档讲可以到ms,有人测试,好象只能到16ms!

        /***************************************************************/

        如何设定当前系统时间---windows

        SYSTEMTIME m_myLocalTime,*lpSystemTime;

        m_myLocalTime.wYear=2003;

        m_myLocalTime.wMonth=1;

        m_myLocalTime.wDay=1;

        m_myLocalTime.wHour=0;

        m_myLocalTime.wMinute=0;

        m_myLocalTime.wSecond=0;

        m_myLocalTime.wMilliseconds=0;

        lpSystemTime=&m_myLocalTime;

        if( SetLocalTime(lpSystemTime) ) //此处换成 SetSystemTime( )也不行

        MessageBox("OK !");

        else

        MessageBox("Error !");

        SYSTEMTIME m_myLocalTime,*lpSystemTime;

        m_myLocalTime.wYear=2003;

        m_myLocalTime.wMonth=1;

        m_myLocalTime.wDay=1;

        lpSystemTime=&m_myLocalTime;

        if( SetDate(lpSystemTime) ) //此处换成 SetSystemTime( )也不行

        MessageBox("OK !");

        else

        MessageBox("Error !");

        /***************************************************************/

        用clock()函数,得到系统启动以后的毫秒级时间,然后除以CLOCKS_PER_SEC,就可以换成“秒”,标准c函数。

        clock_t clock ( void );

        #include

        clock_t t = clock();

        long sec = t / CLOCKS_PER_SEC;

        他是记录时钟周期的,实现看来不会很精确,需要试验验证;

        /***************************************************************/

        据说tc2.0的time结构含有毫秒信息

        #include

        #include

        int main(void)

        {

        struct time t;

        gettime(&t);

        printf("The current time is: %2d:%02d:%02d.%02d\n",

        t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);

        return 0;

        }

        time 是一个结构体,, 其中成员函数 ti_hund 是豪秒。。。上程序可以在tc2.0运行

        /***************************************************************/

        这个是windows里面常用来计算程序运行时间的函数;

        DWORD dwStart = GetTickCount();

        //这里运行你的程序代码

        DWORD dwEnd = GetTickCount();

        则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位

        这个函数只精确到55ms,1个tick就是55ms。

        /***************************************************************/

        timeGetTime()基本等于GetTickCount(),但是精度更高

        DWORD dwStart = timeGetTime();

        //这里运行你的程序代码

        DWORD dwEnd = timeGetTime();

        则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位

        虽然返回的值单位应该是ms,但传说精度只有10ms。

        /***************************************************************/

        Borland C++ Builder VCL的时间函数

        1. Date

        返回TDateTime对象,包含当前的年月日信息,函数原型如下:

        System::TDateTime __fastcall Date(void);

        2. Time

        返回TDateTime对象,包含当前的时间信息,函数原型如下:

        System::TDateTime __fastcall Time(void);

        3. Now

        返回TDateTime对象,获取当前的日期和时间信息,函数原型如下:

        System::TDateTime __fastcall Now(void);

        4. DatetimeToString

        将TDateTime对象转换为指定格式的字符串对象,函数原型如下:

        void __fastcall DateTimeToString(AnsiString &;Result, const AnsiString Format,System::TDateTime DateTime);

        5. DateToStr

        将TDateTime对象(包含当前年月日信息)转换为字符串对象,函数原型如下:

        AnsiString __fastcall DateToStr(System::TDateTime Date);

        6. TimeToStr

        将当前日期转换为字符串对象,函数原型如下:

        AnsiString __fastcall TimeToStr(System::TDateTime Time);

        7. DateTimetoStr

        将TDateTime对象转换为字符串对象,函数原型如下:

        AnsiString __fastcall DateTimeToStr(System::TDateTime DateTime);

        8. StrToDate

        将字符串对象转换为年月日对象,函数原型如下:

        System::TDateTime __fastcall StrToDate(const AnsiString S);

        9. StrToTime

        将字符串对象转换时间对象,函数原型如下:

        System::TDateTime __fastcall StrToTime(const AnsiString S);

        10.StrToDateTime

        将字符串对象转换为年月日时间对象,函数原型如下:

        System::TDateTime __fastcall StrToDateTime(const AnsiString S);

        11.DateTimeToSystemTime

        将TDateTime对象转换为操作系统时间,函数原型如下:

        void __fastcall DateTimeToSystemTime(System::TDateTime DateTime, _SYSTEMTIME &;SystemTime);

        12.SystemTimeToDateTime

        将操作系统时间转换为TDateTime对象,函数原型如下:

        System::TDateTime __fastcall SystemTimeToDateTime(const _SYSTEMTIME &;SystemTime);

        /***************************************************************/

        下面是转的一个用汇编的精确计时方法

        如何获得程序或者一段代码运行的时间?你可能说有专门的程序测试工具,确实,不过你也可以在程序中嵌入汇编代码来实现。

        在Pentium的指令系统中有一条指令可以获得CPU内部64位计数器的值,我们可以通过代码两次获取该计数器的值而获得程序或代码运行的时钟周期数,进而通过你的cpu的频率算出一个时钟周期的时间,从而算出程序运行的确切时间。

        我们通过指令TDSIC来获得cpu内部计数器的值,指令TDSIC返回值放在EDX:EAX中,其中EDX中存放64位寄存器中高32位的值,EAX存放第32位的值.

        下面看看实现的代码:

        //用汇编实现获取一段代码运行的时间

        #include

        using namespace std;

        void GetClockNumber (long high, long low);

        void GetRunTime();

        int main()

        {

        long HighStart,LowStart,HighEnd,LowEnd;

        long numhigh,numlow;

        //获取代码运行开始时cpu内部计数器的值

        __asm

        {

        RDTSC

        mov HighStart, edx

        mov LowStart, eax

        }

        for(int i= 0; i<100000; i++ )

        {

        for(int i= 0; i<100000; i++ )

        {

        }

        }

        //获取代码结束时cpu内部计数器的值,并减去初值

        __asm

        {

        RDTSC

        mov HighEnd, edx

        Mov LowEnd, eax

        ;获取两次计数器值得差

        sub eax, LowStart

        cmp eax, 0 ; 如果低32的差为负则求返,因为第二次取得永远比第一次的大

        jg L1

        neg eax

        jmp L2

        L1: mov numlow, eax

        L2: sbb edx, HighStart

        mov numhigh, edx

        }

        //把两个计数器值之差放在一个64位的整形变量中

        //先把高32位左移32位放在64的整形变量中,然后再加上低32位

        __int64 timer =(numhigh<<32) + numlow;

        //输出代码段运行的时钟周期数

        //以频率1.1Gcpu为例,如果换计算机把其中的1.1改乘其它即可,因为相信大家的cpu都应该在1G以上 ^_^

        cout<< (double) (timer /1.1/1000000000) << endl;

        return 0;

        }

        这样通过一条简单的汇编指令就可以获得程序或一段代码的大概时间,不过并不能得到运行的确切时间,因为即使去掉中间的循环,程序也会有个运行时间,

        因为在第一次取得计数器的值后,有两条汇编指令mov HighStart, edx mov LowStart, eax这两条指令当然也有运行时间 ,当然你可以减去这两条指令的运行时间(在1.1G的机子上是3e-8s),这样会更精确一点。

      例:

      int getTick(int iY,int iM,int iD,int iH,int iMin,int iS)

      {

      struct tm stm;

      stm.tm_year = iY-1900;

      stm.tm_mon = iM-1;

      stm.tm_mday= iD;

      stm.tm_hour = iH;

      stm.tm_min = iMin;

      stm.tm_sex = iS;

      return mktime(&stm);//使用时区信息

      return timegm(&stm);//不使用时区信息

      }

  • 相关阅读:
    Notepad++ 配置python快捷键运行方法
    Python 安装setuptools方法
    Python 安装selenium方法
    Sublime 的汉化以及原因
    PHP书籍推荐
    PHP 语言结构(Language constructs)和函数的区别 (转)
    程序员问答网站:StackOverflow
    安装SQL SEVER 2005中的两个常见问题
    致IT同仁 —— IT人士常犯的17个职场错误
    PHP书写规范 PHP Coding Standard
  • 原文地址:https://www.cnblogs.com/rooney/p/2637713.html
Copyright © 2011-2022 走看看