1.  time() 函数

/*  time - 获取计算机系统当前的日历时间(Calender Time)
* 处理日期时间的函数都是以本函数的返回值为基础进行运算
*
* 函数原型:
* #include <time.h>
*
* time_t time(time_t *calptr);
*
* 返回值:
* 成功:秒数,从1970-1-1,00:00:00
*
* 使用:
* time_t now;
*
* time(&now); // == now = time(NULL);
*/

2.  localtime() 函数

/*
* localtime - 将时间数值变换成本地时间,考虑到本地时区和夏令时标志
*
* 函数声明:
* #include <time.h>
*
* struct tm * localtime(const time_t *timer);
*
*/
/*  struct tm 结构
*
* 此结构体空间由内核自动分配,而且不需要去释放它
*/
struct tm {
int tm_sec; /*秒, 范围从0到59 */
int tm_min; /*分, 范围从0到59 */
int tm_hour; /*小时, 范围从0到23 */
int tm_mday; /*一个月中的第几天,范围从1到31 */
int tm_mon; /*月份, 范围从0到11 */
int tm_year; /*自 1900起的年数 */
int tm_wday; /*一周中的第几天,范围从0到6 */
int tm_yday; /*一年中的第几天,范围从0到365 */
int tm_isdst; /*夏令时 */
};

3. Demo 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h> #define _DATETIME_SIZE 32 // GetDate - 获取当前系统日期
/**
* 函数名称:GetDate
* 功能描述:取当前系统日期
*
* 输出参数:char * psDate - 系统日期,格式为yyymmdd
* 返回结果:0 -> 成功
*/
int
GetDate(char * psDate){
time_t nSeconds;
struct tm * pTM; time(&nSeconds); // 同 nSeconds = time(NULL);
pTM = localtime(&nSeconds); /* 系统日期,格式:YYYMMDD */
sprintf(psDate,"%04d-%02d-%02d",
pTM->tm_year + , pTM->tm_mon + , pTM->tm_mday); return ;
} // GetTime - 获取当前系统时间
/**
* 函数名称:GetTime
* 功能描述:取当前系统时间
*
* 输出参数:char * psTime -- 系统时间,格式为HHMMSS
* 返回结果:0 -> 成功
*/
int
GetTime(char * psTime) {
time_t nSeconds;
struct tm * pTM; time(&nSeconds);
pTM = localtime(&nSeconds); /* 系统时间,格式: HHMMSS */
sprintf(psTime, "%02d:%02d:%02d",
pTM->tm_hour, pTM->tm_min, pTM->tm_sec); return ;
} // GetDateTime - 取当前系统日期和时间
/**
* 函数名称:GetDateTime
* 功能描述:取当前系统日期和时间
*
* 输出参数:char * psDateTime -- 系统日期时间,格式为yyymmddHHMMSS
* 返回结果:0 -> 成功
*/
int
GetDateTime(char * psDateTime) {
time_t nSeconds;
struct tm * pTM; time(&nSeconds);
pTM = localtime(&nSeconds); /* 系统日期和时间,格式: yyyymmddHHMMSS */
sprintf(psDateTime, "%04d-%02d-%02d %02d:%02d:%02d",
pTM->tm_year + , pTM->tm_mon + , pTM->tm_mday,
pTM->tm_hour, pTM->tm_min, pTM->tm_sec); return ;
} // 测试代码
int main()
{
int ret;
char DateTime[_DATETIME_SIZE]; memset(DateTime, , sizeof(DateTime)); /* 获取系统当前日期 */
ret = GetDate(DateTime);
if(ret == )
printf("The Local date is %s\n", DateTime);
else
perror("GetDate error!"); memset(DateTime, , sizeof(DateTime));
/* 获取当前系统时间 */
ret = GetTime(DateTime);
if(ret == )
printf("The Local time is %s\n", DateTime);
else
perror("GetTime error!"); memset(DateTime, , sizeof(DateTime));
/* 获取系统当前日期时间 */
ret = GetDateTime(DateTime);
if(ret == )
printf("The Local date and time is %s\n", DateTime);
else
perror("GetDateTime error!"); return ;
}

运行结果

4.  后记

诫子书 - 诸葛亮

夫君子之行,静以修身,俭以养德。

非淡泊无以明志,非宁静无以致远。

夫学须静也,才须学也,非学无以广才,非志无以成学。

淫慢则不能励精,险躁则不能冶性。

年与时驰,意与日去,遂成枯落,多不接世,悲守穷庐,将复何及!

Linux C 中获取local日期和时间 time()&localtime()函数的更多相关文章

  1. ACCESS中如何比较日期和时间,使用DateDiff函数

    DateDiff,语法如下:DateDiff( 间隔字符, 日期1, 日期2 [,firstdayofweek[, firstweekofyear]])一般使用 DateDiff( 间隔字符, 日期1 ...

  2. SqlServer中日期和时间数据类型及函数 【转】

    来源:http://blog.csdn.net/royalwzy/article/details/6446075 日期和时间数据类型 下表列出了 Transact-SQL 的日期和时间数据类型. 数据 ...

  3. java 8中新的日期和时间API

    java 8中新的日期和时间API 使用LocalDate和LocalTime LocalDate的实例是一个不可变对象,它只提供了简单的日期,并不含当天的时间信息.另外,它也不附带任何与时区相关的信 ...

  4. bat 获取系统日期,时间,并去掉时间小时前面的空格和时间后面的空格

    @echo off rem BAT获取系统日期,时间,并去掉时间小时前面的空格和时间后面的空格 echo *** %DATE% echo *** %TIME% set THISDATE=%DATE:~ ...

  5. JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;

    学习目标 1.掌握创建日期对象的方法 2.掌握date对象中获取日期时间的方法 3.掌握date对象中设置日期时间的方法 如何创建一个日期对象 语法:new Date(); 功能:创建一个日期时间对象 ...

  6. java中获取系统的当前时间

    转自:http://www.cnblogs.com/Matrix54/archive/2012/05/01/2478158.html 一. 获取当前系统时间和日期并格式化输出: import java ...

  7. 从Linux内核中获取真随机数【转】

    转自:http://www.cnblogs.com/bigship/archive/2010/04/04/1704228.html 内核随机数产生器 Linux内核实现了一个随机数产生器,从理论上说这 ...

  8. 从Linux内核中获取真随机数

    内核随机数产生器 Linux内核实现了一个随机数产生器,从理论上说这个随机数产生器产生的是真随机数.与标准C库中的rand(),srand()产生的伪随机数不同,尽管伪随机数带有一定的随机特征,但这些 ...

  9. matlab中datest() 将日期和时间转换为字符串格式

    来源:https://ww2.mathworks.cn/help/matlab/ref/datestr.html?searchHighlight=datestr&s_tid=doc_srcht ...

随机推荐

  1. UVa 11991 一道简单题

    https://vjudge.net/problem/UVA-11991 题意:给出一个包含n个整数的数组,你需要回答若干询问.每次询问两个整数k和v,输出从左到右第k个v的下标. 思路: 把每个数字 ...

  2. Myeclipse下配置svn

    转载高鑫的..嘻嘻.. MyEclipse安装配置SVN 2013.10.15 No Comments 67 Views 配置之前请先关闭MyEclipse,OK开始了 1.解压site-1.6.18 ...

  3. poj 2762 Going from u to v or from v to u? trajan+拓扑

    Going from u to v or from v to u?   Description In order to make their sons brave, Jiajia and Wind t ...

  4. OpenVPN Windows 平台安装部署教程

    一.环境准备: 操作系统Windows 服务器IP:192.168.88.123  VPN:192.168.89.1 客户端IP:192.168.78.3 客户端服务端单网卡,路由器做好端口映射 安装 ...

  5. Ubuntu 14.04 编写service 服务

    有时我们需要将特定操作封装成服务,通过服务启动停止,例如nginx的启动停止,service nginx start 或者service nginx stop 下面我们将编写一个demo cd /et ...

  6. MongoDB(课时20 游标)

    3.5 游标(重点) 所谓游标就是指数据可以一行行的进行操作,非常类似于ResultSet数据处理.在MongoDB里对游标的控制使用find()函数就可以返回游标.对于返回的游标如果想进行操作,使用 ...

  7. TP5框架whereor

    whereOr方法 Db::table('think_user') ->where('name','like','%thinkphp') ->whereOr('title','like', ...

  8. ASCII 对照表

    ASCII(American Standard Code for Information Interchange,美国信息互换标准代码,ASCⅡ)是基于拉丁字母的一套电脑编码系统.它主要用于显示现代英 ...

  9. English trip -- Phonics 2 元音字母a

    xu言: 欲速则不达,如果这是你生命中最后一天.你还愿意花这么多精力继续坚持你现在做的事吗?如果答案是否定的,那么你需要改变了! What makes a word? 单词构成 Word 单词:    ...

  10. 实训10a--用数据值填充下拉列表

    1.新建mvc4项目,选择基本模板. (1)点击“开始->所有程序->Microsoft Visual Studio 2012->Visual Studio 2012”菜单,打开Vi ...