现在的位置: 首页 > 综合 > 正文

Windows下日期时间编程简单总结

2013年10月05日 ⁄ 综合 ⁄ 共 2930字 ⁄ 字号 评论关闭

在日常编程中,对日期和时间的处理是非常频繁的,我们可以使用C运行库提供的时间计算函数来支持开发,Windows下还提供了诸如MFCCTime等操作对象,但是在跨平台开发的情况下,CTime就不适用了。所以这里总结了一下C运行库的一些常用函数,以便参考.

 

1.类型 time_t中存储的是自197011日午时到现在的偏移量,以秒计时,因此Windows系统下多用此做时间计算。

类型time_t实际上是个整数,看time_t 的定义:

ifndef _TIME_T_DEFINED

#ifdef _USE_32BIT_TIME_T

typedef __time32_t time_t;      /* time value */

#else

typedef __time64_t time_t;      /* time value */

#endif

#define _TIME_T_DEFINED         /* avoid multiple def's of time_t */

#endif

 

关于__time64_t的定义如下:

#ifndef _TIME32_T_DEFINED

typedef _W64 long __time32_t;   /* 32-bit time value */

#define _TIME32_T_DEFINED

#endif

 

#ifndef _TIME64_T_DEFINED

typedef __int64 __time64_t;     /* 64-bit time value */

#define _TIME64_T_DEFINED

#endif

 

2. 函数gmtimelocaltime

函数localtime, 会把传入的time_t时间转化为 tm类型的数据,并初始化为本地时间

struct tm * localTime(const time_t * timer);

gmtime会得到标准时间(格林尼治时间).

struct tm *gmtime(const time_t * timer);

 

tm结构

struct tm {

        int tm_sec;     /* seconds after the minute - [0,59] */

        int tm_min;     /* minutes after the hour - [0,59] */

        int tm_hour;    /* hours since midnight - [0,23] */

        int tm_mday;    /* day of the month - [1,31] */

        int tm_mon;     /* months since January - [0,11] */

        int tm_year;    /* years since 1900 */

        int tm_wday;    /* days since Sunday - [0,6] */

        int tm_yday;    /* days since January 1 - [0,365] */

        int tm_isdst;   /* daylight savings time flag */

        };

 

3. tm 结构转换为time_t的函数

time_t mktime(struct tm * timeptr);

根据MSDN的解释,mktime会忽略tm_wday, tm_yday数据域.

 

4. 得到系统时间函数 time

time_t time(time_t * timer);

The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, Coordinated Universal Time (UTC), according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.

 

5. 时区信息,可以通过下面的全局变量得到时区偏移,也可以通过当地时间和UTC时间的比较,得到时区偏移.

extern long _timezone; 

extern char *_tzname[2];

 

6. 我的时间函数封装:

头文件:

///////////////////////////////////////////////////////////////////////////////

// 文件: SimpleDateTime.h

// 日期: 2010/04/04

// 作者:

// 说明:

//       简单的时间封装.

///////////////////////////////////////////////////////////////////////////////

#pragma once

 

#include <iostream>

#include <time.h>

 

namespace DateTime

{

class SimpleDateTime

{

public:

     SimpleDateTime(void);

     // time_t 初始化

     SimpleDateTime(time_t time);

     // 用当地时间初始化

     SimpleDateTime(int year, int month, int day);

     // 用当地时间初始化

     SimpleDateTime(int year, int month, int day,

         int hour, int minute, int second);

     // 用指定时区时间初始化

     SimpleDateTime(int year, int month, int day, int timeZone);

     // 用指定时区时间初始化

     SimpleDateTime(int year, int month, int day,

         int hour, int minute, int second, int timeZone);

     SimpleDateTime(const SimpleDateTime & right);

     ~SimpleDateTime(void);

 

public:

     SimpleDateTime & operator=(const SimpleDateTime & right);

 

     bool operator==(const SimpleDateTime & right) const;

     bool operator!=(const SimpleDateTime & right) const;

 

public:

     // 当前时间

     void currentTime(void);

     // 转化为本地时间.

     SimpleDateTime toLocalTime();

     // 返回time_t

     time_t toTime_t() const;

 

public:

     int getYear() const;

     int getMonth() const;

     int getDay() const;

     int getHour() const;

抱歉!评论已关闭.