C中的时间函数的用法
C中的时间函数的用法
这个类展示了C语言中的时间函数的常用的用法。
源代码:
#include <ctime>
#include <iostream>
using namespace std;
class MyTime
{
public:
MyTime() { mPTime = 0; mStLocalTime = 0; mStGMTTime = 0; }
~MyTime() {};
//time_t time(time_t * timer) 返回自1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。
time_t GetTime() { return time(0); }
//time_t time(time_t * timer) 将自1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数设定到t指向的一个长整形变量里。
time_t GetTimeB() { time(&mPTime); return mPTime; }
//struct tm *localtime(const time_t *clock) 可以将时间戳转换为一个时间结构体,且时间为经过时区转化的本地时间。
int GetYear() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_year; }
int GetMonth() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_mon; }
int GetDate() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_mday; }
int GetHour() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_hour; }
int GetMinute() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_min; }
int GetSecond() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_sec; }
int GetWeekDay() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_wday; }
int GetYearDay() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_yday; }
//char *asctime(const struct tm *tblock) 将换日期和时间的结构体转换为相应的字符串。
char *GetTimeStr() { GetTimeB(); mStLocalTime = localtime(&mPTime); return asctime(mStLocalTime); }
//char *ctime(const time_t *time) 将时间戳转换为响应的字符串。
char *GetLocalTimeStr2() { GetTimeB(); return ctime(&mPTime); }
//tzset() 为设置时区。
//struct tm *gmtime(long *clock) 把日期和时间转换为格林威治(GMT)时间。
char *GetGMTTimeStr() { tzset(); GetTimeB(); mStGMTTime = gmtime(&mPTime); return asctime(mStGMTTime); }
//time_t mktime(strcut tm * timeptr) 将struct tm格式的时间转换为时间戳。
time_t GetTime3() { time(&mPTime); mStLocalTime = localtime(&mPTime); return mktime(mStLocalTime); }
//double difftime(time_t time1, time_t time0) 计算时间间隔才长度,以秒为单位,且只能精确到秒
double CalDuration(time_t t1, time_t t2) { return difftime(t1,t2); }
//size_t strftime(char *strDest,size_t maxsize,const char *format,const struct tm *timeptr);
//将时间格式化,或者说:格式化一个时间字符串。
//根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。
void FormatTime( char *str) { time(&mPTime); strftime(str,100,"%Y-%m-%d",localtime(&mPTime)); }
private:
time_t mPTime; //time_t 实际上是个长整形,用于保存自1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。
struct tm *mStLocalTime;
struct tm *mStGMTTime;
};
int main(int argc, char *argv[])
{
cout<<"process begin @["<<(void*)&main<<"]"<<endl;
MyTime myTime;
cout<<"current Time 1 is ["<<myTime.GetTime()<<"]"<<endl;
cout<<"current Time 2 is ["<<myTime.GetTimeB()<<"]"<<endl;
cout<<"current Time 3 is ["
<<myTime.GetYear() + 1900<<"-"<<myTime.GetMonth()<<"-"<<myTime.GetDate()<<" "
<<myTime.GetHour()<<":"<<myTime.GetMinute()<<":"<<myTime.GetSecond()<<"]"<<endl;
cout<<"current Time week day is ["<<myTime.GetWeekDay()<<"]"<<endl;
cout<<"current Time year day is ["<<myTime.GetYearDay()<<"]"<<endl;
cout<<"current Time 4 is ["<<myTime.GetTimeStr()<<"]"<<endl;
cout<<"current Time 5 is ["<<myTime.GetLocalTimeStr2()<<"]"<<endl;
cout<<"current Time 6 (GMT time) is ["<<myTime.GetGMTTimeStr()<<"]"<<endl;
cout<<"current Time 7 is ["<<myTime.GetTime3()<<"]"<<endl;
cout<<"calculate duration of ["<<myTime.GetTime3()<<"] and ["<<myTime.GetTime3() - 1<<"] is ["
<<myTime.CalDuration(myTime.GetTime3(), myTime.GetTime3() - 1 )<<"]"<<endl;
char szTime[20] = {0};
myTime.FormatTime(szTime);
cout<<"current Time 8 is ["<<szTime<<"]"<<endl;
return 0;
}
输出结果:
process begin @[0x4016b0]
current Time 1 is [1416756697]
current Time 2 is [1416756697]
current Time 3 is [2014-10-23 23:31:37]
current Time week day is [0]
current Time year day is [326]
current Time 4 is [Sun Nov 23 23:31:37 2014
]
current Time 5 is [Sun Nov 23 23:31:37 2014
]
current Time 6 (GMT time) is [Sun Nov 23 15:31:37 2014
]
current Time 7 is [1416756697]
calculate duration of [1416756697] and [1416756696] is [1.41676e+009]
current Time 8 is [2014-11-23]
C中的时间函数的用法的更多相关文章
- PHP中日期时间函数date()用法总结
date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...
- ylb:SQL Server中的时间函数
ylbtech-SQL Server:SQL Server-SQL Server中的时间函数 SQL Server中的时间函数. 1,SQL Server中的时间函数 返回顶部 1. 当前系统日期 ...
- 借助JavaScript中的时间函数改变Html中Table边框的颜色
借助JavaScript中的时间函数改变Html中Table边框的颜色 <html> <head> <meta http-equiv="Content-Type ...
- C语言中关于scanf函数的用法
scanf()函数的控制串 函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化 ...
- JavaScript中字符串分割函数split用法实例
这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...
- (转)Python中的split()函数的用法
Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...
- C++中的时间函数
C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...
- SQLSERVER数据库中的 时间函数
一.sql server日期时间函数 Sql Server中的日期与时间函数 1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返 ...
- 【推荐】PHP中格式化时间函数date与gmdate的区别 | 修改PHP的默认时区
PHP中的时间有2个格式化函数:date()和gmdate(),在官方的文档中的描述为: date -- 格式化一个本地时间/日期 gmdate -- 格式化一个 GMT/UTC 日期/时间,返回的是 ...
随机推荐
- 函数没有返回值,默认返回undefined
var a =( function(){return})(); a = undefined;
- yii框架的中的一些使用介绍
Yii框架的使用整理 获取配置文件中的数据 Yii::$app->params[‘配置文件中对应的参数名称’] 获取文件表单提交的数据 Yii::$app->request->pos ...
- VSCode eslint校验 tab改为2个空格
修改:.eslintrc.json
- iOS清除缓存功能开发
在APP开发中,大量的图片或消息占用系统内存,造成一堆垃圾信息,所以开发清除缓存功能就显得必不可少了. 代码段1:获取文件的大小 - (long long) fileSizeAtPath:(NSStr ...
- 关于v4l2的一点变更
先打个连接 http://linuxtv.org/downloads/presentations/media_ws_2013/v4l2-multi-format.pdf 2013年linux 多媒体构 ...
- 【转载】Android端百度地图API使用详解
转载地址:http://www.cnblogs.com/rocomp/p/4994110.html 百度地图API简介 百度地图移动版API(Android)是一套基于Android设备的应用程序接口 ...
- 为什么可以Ping通IP地址,但Ping不通域名?
能否ping通IP地址,与能否解析域名是两回事不能ping通IP地址,说明对方禁止ICMP报文或对方没有开机等解析域名只是将域名翻译成IP地址,不论该IP地址是否能够正常访问 问题是ping域名的时候 ...
- java深入探究08-连接池,分页
1.连接池 1)自定义连接池 思路:定义一个类Pool->添加4个属性(最大连接数,初始化连接数,当前连接数,用来存放连接对象的LinkList集合对象)->定义一个createConne ...
- Valid Parentheses有效括号匹配。利用栈。
问题描述:给定一个字符串,其中只包含字符‘{’, '}', '[', ']', '(', ')'确定如果输入字符串是有效的.括号必须以正确的顺序排列,“()”和“()[]{ ...
- ZC_知识点
1. 在创建一个JNI动态库的工程时应该将工程的输出目标设置为动态连接库(Windows下为.dll,Unix-like系统下为.so,OS X下为.dylib) 2.类型对应关系 (Java与C/C ...