C语言获取当前时间(Linux环境下,VC6.0,Codeblock环境下通用)

栏目: C · 发布时间: 7年前

内容简介:在Linux环境下用C语言编写程序获取当前的时间只要调用其内部的函数即可。这些函数在 time.h 这个头文件里,第一个函数原型:① time_t time(time_t *t),通过Linux的man也很方便能够找到这个函数的相关说明:

Linux 环境下用 C语言 编写程序获取当前的时间只要调用其内部的函数即可。这些函数在 time.h 这个头文件里,第一个函数原型:

① time_t time(time_t *t),通过Linux的man也很方便能够找到这个函数的相关说明:

C语言获取当前时间(Linux环境下,VC6.0,Codeblock环境下通用)

在Linux环境的命令行模式中输入 man 2 time即可找到上图的对time函数的说明,这个函数可以计算从1970年1月1日到当前的总秒数。

第二个函数的函数原型是:

② struct tm *localtime(const time_t *timep)

C语言获取当前时间(Linux环境下,VC6.0,Codeblock环境下通用)

C语言获取当前时间(Linux环境下,VC6.0,Codeblock环境下通用)

在Linux环境的命令行模式中输入 man localtime即可找到上图的对time函数的说明。有了这两个函数就可以编写程序了,程序如下:

#include <stdio.h>

#include <time.h> 

int main(void) 

time_t t; 

t = time(NULL); 

printf("时间秒数:%d\n",t); 

struct tm *p = localtime(&t); 

printf("%d-",1900+ p->tm_year);//Year  需要加上1900

printf("%d-",1+p->tm_mon);//Month  需要加上1

printf("%d\t",p->tm_mday);//Day 

printf("%d:",p->tm_hour);//Hour 

printf("%d:",p->tm_min);//Minute 

printf("%d\t",p->tm_sec);//Second 

printf("Week=%d\n",p->tm_wday);//Week 

return 0; 

}

输出:

时间秒数:1541247008

2018-11-3 20:10:8 Week=6

如下图:

C语言获取当前时间(Linux环境下,VC6.0,Codeblock环境下通用)

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-11/155157.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volumes 1-3 Boxed Set

The Art of Computer Programming, Volumes 1-3 Boxed Set

Donald E. Knuth / Addison-Wesley Professional / 1998-10-15 / USD 199.99

This multivolume work is widely recognized as the definitive description of classical computer science. The first three volumes have for decades been an invaluable resource in programming theory and p......一起来看看 《The Art of Computer Programming, Volumes 1-3 Boxed Set》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具