博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
与时间相关的函数编程
阅读量:4285 次
发布时间:2019-05-27

本文共 2603 字,大约阅读时间需要 8 分钟。

概念

首先了解几个与时间相关的概念

世界标准时间:即格林威治时间;
日历时间:从1970年1月1日0点到此时经历过的秒数,是一个整数。

函数

time_t time(time_t *t)

该函数功能为获得日历时间。包含的头文件为time.h,函数运行成功则返回日历时间,失败返回-1.如果参数指针t不为NULL,则返回值会保存在t。

#include
#include
void main(){ time_t ctime; ctime=time(NULL); printf("time is %d\n",ctime);}

struct tm* gmtime(const time_t *timep)

该函数功能为将日历时间timep指定的日历时间转化为世界标准时间。包含的头文件time.h,函数运行成功则返回世界标准时间,以struct tm形式存储。

#include
#include
void main(){ time_t ctime; ctime=time(NULL); struct tm *t; t=gmtime(&ctime); printf("the time is %d:%d:%d",t->tm_hour ,t->tm_min ,t->tm_sec );}

其中struct 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};

struct tm* localtime(const time_t *timep)

该函数功能为将日历时间timep指定的日历时间转化为本地时间。包含的头文件time.h,函数运行成功则返回本地时间,以struct tm形式存储。失败返回NULL。

#include
#include
void main(){ time_t ctime; ctime=time(NULL); struct tm *t; t=localtime(&ctime); printf("the time is %d:%d:%d",t->tm_hour ,t->tm_min ,t->tm_sec );}

char *asctime(const struct tm *tm)

该函数功能为将struct tm格式的时间转化为字符串形式。包含的头文件为time.h,运行成功则返回字符串显示的时间

#include
#include
void main(){ time_t ctime; ctime=time(NULL); struct tm *t; t=localtime(&ctime); printf("the time is %d:%d:%d\n",t->tm_hour ,t->tm_min ,t->tm_sec ); char *asct=asctime(t); printf("the time is %s\n",asct);}

int gettimeofday(struct timeval *tv,struct timezone *tzone)

该函数功能为获取高精度时间,精度为微秒级。包含的头文件为<sys/time.h>,该头文件不是c的库文件,是Linux系统的。成功返回0,失败返回-1.函数运行结束后,日历时间和微秒数会保存到参数tv中,tzone一般设置为NULL。

结构timeval为:

struct timeval{    time_t tv_sec;    suseconds_t tv_usec;}

使用实例与运行结果

#include 
#include
#include
void main(){ time_t ctime; ctime=time(NULL); printf("the calendar time is %d\n", ctime); struct tm *t; t=gmtime(&ctime); printf("the gttime is %d:%d:%d\n", t->tm_hour , t->tm_min , t->tm_sec ); t=localtime(&ctime); printf("the localtime is %d:%d:%d\n", t->tm_hour , t->tm_min , t->tm_sec ); char *asct=asctime(t); printf("the asctime is %s", asct); struct timeval tv; gettimeofday(&tv, NULL); printf("gettimeofday sec is %d, usec is %d\n", tv.tv_sec, tv.tv_usec);}

运行结果

你可能感兴趣的文章
git 修改過檔案後,如何commit上server
查看>>
git log 應用
查看>>
Git 版本控制系統(3) 還沒 push 前可以做的事
查看>>
Git 基礎 - 檢視提交的歷史記錄
查看>>
wifi 連ap command
查看>>
git clean reset checkout
查看>>
[轉載]6個超強網站讓你查到最道地的英文
查看>>
HUB 與 Switch 差別
查看>>
linux產生 core dump文件方法及設置
查看>>
How to pass macro definition from “makefile” command line arguments to C source code?
查看>>
英文句型
查看>>
mtd and /dev/mtd*相關資料
查看>>
cp: cannot create symbolic link to fat format of usb: Operation not permitted
查看>>
MTD bad Block issue
查看>>
How to change network interface name
查看>>
ubifs and ubi and mtd
查看>>
shell script set 用法
查看>>
英文序數寫法與唸法 Ordinal Numbers(轉載)
查看>>
DVB-S info
查看>>
绿盟扫描操作指导
查看>>