1asctime
将时间和日期以字符串格式表示
time,ctime,gmtime,localtime
#include<time.h>
char * asctime(const struct tm * timeptr);
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993\n”
返回一字符串表示目前当地的时间日期.
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
#include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
执行结果:
Sat Oct 28 02:10:06 2000
2ctime
将时间和日期以字符串格式表示
time,asctime,gmtime,localtime
#include<time.h>
char *ctime(const time_t *timep);
ctime()将参数 timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符 串格式为“Wed Jun 30 21 :49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回一字符串表示目前当地的时间日期。
#include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
}
执行结果:
Sat Oct 28 10 : 12 : 05 2000
3gmtime
取得目前时间和日期
time,asctime,ctime,localtime
#include<time.h>
struct tm*gmtime(const time_t*timep);
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回结构tm代表目前UTC 时间
#include <time.h>
main()
{
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行结果:
2000/10/28 Sat 8:15:38
4gettimeofday
取得目前的时间
time,ctime,ftime,settimeofday
#include <sys/time.h>
#include <unistd.h>
int gettimeofday ( struct timeval * tv , struct timezone * tz )
gettimeofday()会把目前的时间用tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 结构定义为:
struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。
tz_dsttime 所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
#include<sys/time.h>
#include<unistd.h>
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf(“tv_sec; %d\n”, tv.tv_sec) ;
printf(“tv_usec; %d\n”,tv.tv_usec);
printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
}
执行结果:
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
5 localtime
取得当地目前时间和日期
time, asctime, ctime, gmtime
#include<time.h>
struct tm *localtime(const time_t * timep);
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回结构tm代表目前的当地时间。
#include<time.h>
main()
{
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得当地时间*/
printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行结果:
2000/10/28 Sat 11:12:22
6 mktime
将时间结构数据转换成经过的秒数.
time,asctime,gmtime,localtime
#include<time.h>
time_t mktime(strcut tm * timeptr);
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回经过的秒数。
/* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d \n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
执行结果:
time():974943297
time()->localtime()->mktime():974943297
7settimeofday
设置目前时间
time,ctime,ftime,gettimeofday
#include<sys/time.h>
#include<unistd.h>
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
成功则返回0,失败返回-1,错误代码存于errno。
错误代码
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
8time
取得当前的时间
ctime,ftime,gettimeofday
#include<time.h>
time_t time(time_t *t);
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
#include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
分享到:
相关推荐
把一个程序分成几个函数,在linux下练习使用
Linux网络编程几个必需的网络函数,资料不错
Linux 的man手册共有以下几个章节: 章节 名称 描述 1 Standard commands(标准命令) Executable programs or shell commands, 普通的命令 2 System calls(系统调用) System calls (functions provided by the ...
经有十几年Linux开发经验的老师多年收集的各种与linux相关的文档,包括GCC中文手册,GNU make中文手册,GUN,Linux C函数文档,LInux C函数库参考手册等等15个文档,欢迎共同交流学习
这几天用linux后就出现了少这个函数的问题,在网上找了很久都没有找到好资源,目前这个还算不错了吧!我正用着!不知道和别的版本的兼容否,大家可以试试!
Linux中的所有命令(包括函数)都是采用的系统时钟设置。在Linux中用于时钟查看和设置的命令主要有date、hwclock和clock。本文简单介绍了这几个设置时间的命令。
1、消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法 2、每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值 3、消息队列与管
下面小编就为大家带来一篇浅谈linux几种定时函数的使用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
通过shell在linux 下的bpython比较了几个函数的区别,让人更容易懂
本demo(“weijia.c”)包含两个模块,一个是linux下UTF-8转GB2312函数,另一个是基于串口通信的几个POS指令函数,可以打印“条码”,“数字”,“中文”,“英文”。 测试环境是58mm热敏打印机(条码数字一次打印...
LINUX内核经典面试题 ,20) 如何加载、卸载一个模块? 21) 模块和应用程序分别运行在什么空间? 22) Linux中的浮点运算由应用程序实现还是内核实现? 23) 模块程序能否使用可链接的库函数? 24) TLB中缓存的是什么...
最近一致致力于linux下的C开发,因为老大...在游戏里,时间是一个非常重要的一个变量,涉及到前后端时间同步,游戏业务的倒计时,心跳等等的一系列功能点等等,如果能够灵活运用时间这个变量,最起码你要知道如下几个函
sXid是一个系统监控程序,软件下载后,使用“make install”命令即可安装。它可以扫描系统中suid和sgid文件和目录,因为这些目录很可能是后门程序,并可以设置通过电子邮件来报告结果。Linux Security Auditing Tool...
给大家介绍PHP执行linux系统命令的几个基本函数。
curses库是UNIX系统下的一个函数库,专门用来处理游标移动及萤幕的显示,使得开发者可以编写出类似tin、elm等工具的图像程序。 curses库的历史可以追溯到柏克莱大学的Bill Joy和Ken Arnold,最初的目的是为了提高...
本文内容大致分为以下几个部分: 1、 LCD硬件及显示原理介绍; 2、 s3c2440 LCD控制器介绍; 3、 内核LCD驱动机制framebuffer(帧缓冲技术)概述; 4、 驱动移植相关及应用程序接口相关重要数据结构分析 5、 在linux...
16.3.2 Linux下与域名系统相关的 几个配置文件 263 16.3.3 Caching Only域名服务器 的配置 264 16.3.4 主域名服务器的配置 269 16.3.5 从域名服务器的配置 272 16.4 设置chroot的DNS 273 小结 275 习题十六 276 一、...
2.3.5 几个线程API23 实验2.3 Linux多线程使用实例——生产者-消费者协议24 2.4 进程创建以及进程间通信25 2.4.1 进程概述25 2.4.2 进程的相关函数25 2.4.3 信号概述27 2.4.4 信号的相关函数27 2.4.5 管道...
glibc是gnu发布的libc库,即c运行库。glibc是linux系统中最底层的api...在 GNU/Linux 系统中,其C函式库发展史点出了GNU/Linux 演进的几个重要里程碑,用 glibc 作为系统的C函式库,是GNU/Linux演进的一个重要里程碑。
2.3.5 几个线程API23 实验2.3 Linux多线程使用实例——生产者-消费者协议24 2.4 进程创建以及进程间通信25 2.4.1 进程概述25 2.4.2 进程的相关函数25 2.4.3 信号概述27 2.4.4 信号的相关函数27 2.4.5 管道...