C 语言的标准时间封装

C语言处理时间有很多函数和结构体类型完成,并且涉及到时间戳,标准时间,本地时间的转换。学习起来感觉头大,外加上小写并且简写函数名称,就难以理解。

代码中解释

#include <time.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
typedef time_t TimeStamp; //旧的时间戳类型 给他其个新名字
typedef struct tm scTime;  //旧的时间结构 临时改个名 或许也不需要typedef struct DateTime   //新的时间结构体类型
{

    unsigned int Year;
    unsigned int Month;
    unsigned int Day;
    unsigned int Hour;
    unsigned int Minute;
    unsigned int Second;
    unsigned int DayofWeek;
    unsigned int DayofYear;
} TDateTime;

typedef struct              //新的时间类型 除了秒外的
{
  unsigned long time_mil ;//1毫秒 = 1秒/1000 
  unsigned long time_mic ;//1微秒 = 1毫秒/1000
  unsigned long time_nsec ;//1纳秒 = 1微秒/1000

} Time;

TimeStamp GetTimeStamp( )  //获得时间戳
    TimeStamp stamp;
    time (&stamp);     //这个函数在C里面就是获得时间戳的
    return stamp;
};

Time GetTime()   //返回秒后面的更小的精度{


     int time_mil = 0;//1毫秒 = 1秒/1000 
     int time_mic = 0;//1微秒 = 1毫秒/1000
     int time_nse = 0;
     long l_nsec  = 0;
     struct timespec tn; //纳秒结构体

     clock_gettime(CLOCK_REALTIME, &tn);  //使用该函数获得高精度时间

     l_nsec   = tn.tv_nsec;
     time_mil = tn.tv_nsec/1000000;       //需要对纳秒数进行分离 从而获得毫秒 微秒
     time_mic = (tn.tv_nsec-time_mil*1000000)/1000;
     time_nse = (tn.tv_nsec-time_mil*1000000-time_mic*1000);

     Time l_thisTime;

     l_thisTime.time_mil = time_mil;
     l_thisTime.time_mic = time_mic;
     l_thisTime.time_nsec= time_nse;

     return l_thisTime;

};


TDateTime TimeStampToDateTime(TimeStamp *pTimeStamp) //时间戳转日期格式{    struct tm *pold_DateTime;   
    TDateTime l_date;

    pold_DateTime=gmtime(pTimeStamp);    //通过旧日期结构体转换函数

    l_date.Year   = pold_DateTime->tm_year+1900; //时间戳是从1900年开始
    l_date.Month  = pold_DateTime->tm_mon+1;       //月是从0开始
    l_date.Day    = pold_DateTime->tm_mday;
    l_date.Hour   = pold_DateTime->tm_hour;
    l_date.Minute = pold_DateTime->tm_min;
    l_date.Second = pold_DateTime->tm_sec;
    l_date.DayofYear = pold_DateTime->tm_yday+1;  //从0开始
    l_date.DayofWeek = pold_DateTime->tm_wday;

    return  l_date;
};

TDateTime GetDateTimeUTC()  //获得UTC日期格式{
    TDateTime l_date;
    TimeStamp l_timestamp;
    l_timestamp = GetTimeStamp( );
    l_date = TimeStampToDateTime(&l_timestamp);
    return l_date;
};

TDateTime GetDateTimeLocal(int TimeExtent) //获得本地日期格式{
    TDateTime l_date;
    TimeStamp l_timestamp;
    l_timestamp = GetTimeStamp( );
    l_date = TimeStampToDateTime(&l_timestamp);
    l_date.Hour= l_date.Hour+TimeExtent;
    return l_date;
};


DateTimeToStr(const TDateTime datetime,char *pStrDateTime) //日期转字符串
{
    char StrDateTime [25] = {0};
    char lcTemp[4] ={0};
    char lcSeparator1[2] = {'-'};  //分隔符必须是字符串 后面要跟\0
    char lcSeparator2[2] = {' '};   //用字符变量无法使用strcat()
    char lcSeparator3[2] = {':'};

    itoa(datetime.Year,lcTemp,10);     //整数转字符串以10进制表现
    strcat(StrDateTime,lcTemp);           //字符拼接函数
    strcat(StrDateTime,lcSeparator1); 


    if (datetime.Month < 10 )                //个位数前面置0
    {
       itoa(0,lcTemp,10);
       strcat(StrDateTime,lcTemp);
    }
    itoa(datetime.Month,lcTemp,10);
    strcat(StrDateTime,lcTemp);
    strcat(StrDateTime,lcSeparator1);


    if(datetime.Day < 10)           
    {
       itoa(0,lcTemp,10);
       strcat(StrDateTime,lcTemp);  
    }
    itoa(datetime.Day,lcTemp,10);
    strcat(StrDateTime,lcTemp);
    strcat(StrDateTime,lcSeparator2);

    if(datetime.Hour < 10)
    {
       itoa(0,lcTemp,10);
       strcat(StrDateTime,lcTemp);  
    }   
    itoa(datetime.Hour,lcTemp,10);
    strcat(StrDateTime,lcTemp);
    strcat(StrDateTime,lcSeparator3);

    if(datetime.Minute < 10)
    {
       itoa(0,lcTemp,10);
       strcat(StrDateTime,lcTemp);  
    }   
    itoa(datetime.Minute,lcTemp,10);
    strcat(StrDateTime,lcTemp);
    strcat(StrDateTime,lcSeparator3);

    if(datetime.Second < 10)
    {
       itoa(0,lcTemp,10);
       strcat(StrDateTime,lcTemp);  
    }   
    itoa(datetime.Second,lcTemp,10);
    strcat(StrDateTime,lcTemp);

    int i=0;
    for ( i = 0 ;i < 25; i++)          //循环赋值给返回变量
    (
      pStrDateTime[i]=StrDateTime[i]
    );
};


int main()
{
 TimeStamp time_stamp;

 TDateTime thisDateTime ,thisLocalDateTime ;
 char StrLcalDateTime [25] ;

 time_stamp = GetTimeStamp(); 
 thisDateTime = GetDateTimeUTC();
 thisLocalDateTime =GetDateTimeLocal(8);
 DateTimeToStr(thisLocalDateTime,StrLcalDateTime);
 Time thisTime = GetTime();

printf("UTC TimeStamp :%d\n", time_stamp); 
printf("UTC Datetime  %d-%d-%d %d:%d:%d\n", 
  thisDateTime.Year,thisDateTime.Month ,thisDateTime.Day,thisDateTime.Hour,thisDateTime.Minute,thisDateTime.Second);  

printf("Local Datetime Str:%s\n", StrLcalDateTime); 
printf("The Time isMillisecond.Microsecond.NanoSecond:%d.%d.%d\n",thisTime.time_mil,thisTime.time_mic,thisTime.time_nsec);
printf("How day of year today? %d\n",thisLocalDateTime.DayofYear);
printf("How week today ? %d\n",thisLocalDateTime.DayofWeek) ;

 return 0;
}

 

当然代码写得不够高效,大量使用值传和本地变量。

 

UTC TimeStamp :1615730198
UTC Datetime  2021-3-14 13:56:38
Local Datetime Str:2021-03-14 21:56:38
The Time isMillisecond.Microsecond.NanoSecond:171.678.500
How day of year today? 73
How week today ? 0

--------------------------------
Process exited after 0.01856 seconds with return value 0
请按任意键继续. . .

希望大家多多关注我的公众号:

https://mp.weixin.qq.com/s/tzzEw4Sknz06O4bf9igQow

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值