linux下获取时间的函数

相关函数 time,ctime,gmtime,localtime

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

asctime(将时间和日期以字符串格式表示)

#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

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

ctime(将时间和日期以字符串格式表示)

表头文件

#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

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

gettimeofday(取得目前的时间)

表头文件

#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

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

gmtime(取得目前时间和日期)

表头文件

#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

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

localtime(取得当地目前时间和日期)

表头文件

#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

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

mktime(将时间结构数据转换成经过的秒数)

表头文件

#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

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

settimeofday(设置目前时间)

表头文件

#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 时区或某个数据是不正确的,无法正确设置时间。

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

time(取得目前的时间)

表头文件

#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);
}

转载于:https://www.cnblogs.com/xmphoenix/archive/2011/05/09/2041546.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380517.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux 内核配置 dns,linux bind dns简单配置

操作系统版本&#xff1a;[roottest ~]# cat /etc/issueRed Hat Enterprise Linux AS release 4 (Nahant Update 4)Kernel r on an m内核&#xff1a;[roottest ~]# uname -aLinux test 2.6.9-42.EL #1 Wed Jul 12 23:16:43 EDT 2006 i686 i686 i386 GNU/Linux[roottest ~]#需要…

鸡兔同笼问题

已知鸡兔的总数量为n&#xff0c;总腿数为m。输入n和m&#xff0c;依次输出鸡的数目和兔的数目。如果无解&#xff0c;则输出No answer。 样例输入&#xff1a;①14 32 ② 10 16 样例输出&#xff1a;①12 2 ②No answer 【分析】 设鸡有a只&#xff0c;兔有b只&#xff0c…

两个人 三声叹 一钵泪

寂寞的人,流下的泪珠是单数的 转载于:https://www.cnblogs.com/aque1984/archive/2006/10/02/520282.html

散列碰撞_散列中的碰撞和碰撞解决技术

散列碰撞Prerequisite: Hashing data structure 先决条件&#xff1a; 哈希数据结构 碰撞 (Collisions) Hash functions are there to map different keys to unique locations (index in the hash table), and any hash function which is able to do so is known as the per…

call和ret(f)指令

call指令 CPU执行call指令时&#xff0c;进行两步操作 将当前的ip&#xff08;eip&#xff09;或者cs和ip&#xff08;ecs和eip&#xff09;压入栈中跳转到标号处 call 标号 将当前的IP压栈后&#xff0c;转到标号处执行指令 相当于&#xff1a; push ip jmp near ptr 标号…

天翼云linux远程密码不对,天翼云主机远程连接

天翼云主机如何进行远程连接&#xff1f;操作系统不同自然连接方式也是不一样&#xff0c;今天我们分别介绍一下天翼云Windows主机远程连接和Linux云主机远程连接的方法。一、Windows云主机远程连接(1)在电脑连接网络的情况下&#xff0c;点击桌面左下角的windows图标&#xff…

pku2503 Babelfish

额&#xff0c;跟hdu的1075很像&#xff0c;不过比那题简单多了&#xff0c;就是读入时有点麻烦&#xff0c;用到了一个函数 具体看代码吧&#xff0c;这题没什么&#xff0c;不过我的内存开了太多了&#xff0c;不解…… #include<stdio.h> #include<string.h> #i…

JAVA JDK环境渲染

①&#xff08;随便在哪个盘里都行&#xff09;创建一个文件夹名称&#xff1a;Java&#xff1b; ②在文件夹Java下创建一个子文件夹 名称&#xff1a;jdk&#xff1b; ③在子文件夹jdk下再创建一个子文件夹 名称jre&#xff1b; &#xff08;文件夹名称随便&#xff0c;我这…

英语单词 搞笑着背

背单词的过程是枯燥的&#xff0c;一天紧张的学习用下面的方法检查一下今天记住了多少&#xff1a;&#xff09; 根据中英文的谐音&#xff0c;不知对大家背单词是否有用&#xff01;  peevish------"劈为尸体"------暴躁的  hermit-------"何处觅她"---…

linux查看网卡硬件 lsw,linux系统配置管理小测试试卷答案

广东岭南职业技术学院Linux操作系统试题卷一、选择题&#xff1a;(每题2分&#xff0c;共64分)1&#xff0e;Linux操作系统内核创始人是( C )。A. Bill GatesB. Richard StallmanC. Linus TorvaldsD. Dennis MRitchie、Ken Thompson2、下面哪个Linux命令可以一次显示一页内容&a…

turbo c填充图形_C / C ++中的图形:Turbo C编译器中的简介和图形模式

turbo c填充图形In this advanced learning tutorial, you will learn more about the C/C feature "Graphics" by using which you can make your C program even more interactive and attractive. Graphics in C/C can be used to draw different shapes, animati…

使用C和汇编实现一个加法操作

在C/C嵌入汇编指令格式&#xff1a; __asm{;汇编指令 }代码&#xff1a; #include "stdio.h"int main() {int a1;int b2;int c0;__asm{mov eax,amov ebx,badd eax,ebxmov c,eax}printf("ab%d\n",c);return 0; }结果&#xff1a;

神话人物的现代版简历

她丈夫在河北当建筑工人&#xff0c;工程塌方&#xff0c;被砸死在下面。她要求赔偿&#xff0c;包工头不肯。她起诉到法院&#xff0c;败诉。她上诉&#xff0c;再次败诉。她只好上访&#xff0c;从山东一次次来到河北&#xff0c;当局抓她&#xff0c;关她&#xff0c;在精神…

restrict(TextField.restrict 属性)

restrict&#xff08;TextField.restrict 属性&#xff09; public restrict : String 指示用户可输入到文本字段中的字符集。如果 restrict 属性的值为 null&#xff0c;则可以输入任何字符。如果 restrict 属性的值为空字符串&#xff0c;则不能输入任何字符。如果 restrict …

字符串 charat_Java | String.charAt(index)| 从字符串中按索引获取字符

字符串 charatString.charAt() function is a library function of String class, it is used to get/retrieve the specific character from a string. Where, the index starts from 0 and ends to String.lenght-1. String.charAt()函数是String类的库函数&#xff0c;用于…

移动硬盘linux逻辑分区的区别,主磁盘分区、扩展磁盘分区、逻辑分区的区别

硬盘分区有三种&#xff0c;主磁盘分区、扩展磁盘分区、逻辑分区。一个硬盘可以有一个主分区&#xff0c;一个扩展分区&#xff0c;也可以只有一个主分区没有扩展分区。逻辑分区可以若干。主分区是硬盘的启动分区&#xff0c;他是独立的&#xff0c;也是硬盘的第一个分区&#…

hdu1247

一道水题&#xff0c;居然改错改了一个多小时 题目的意思是其实就是找出一个单词&#xff0c;前半部是一个出现过的单词&#xff0c;后半部也是&#xff0c;记住&#xff0c;要严格满足这个条件 所以&#xff0c;其实也就是先查找一个单词的是否有前缀&#xff0c;再用这个单词…

FPU数据寄存器

FPU有8个独立的、可寻址的80位数据寄存器R0-R7&#xff0c;如下图所示&#xff0c;这些寄存器合称为寄存器栈。FPU 状态字中名为 TOP 的一个 3 位字段给出了当前处于栈顶的寄存器编号。例如&#xff0c;在下图中&#xff0c;TOP 等于二进制数 011&#xff0c;这表示现在栈顶为 …

计算当月的天数(sql)

declare dt datetime set dtgetdate() --set dt2006-8-8 SELECT 32-Day(dt32-Day(dt)) 转载于:https://www.cnblogs.com/super-yc/archive/2006/11/04/549646.html