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. BZOJ 1005: [HNOI2008]明明的烦恼(prufer数列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1005 题意: Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标 ...

  2. 词云wordcloud类介绍&python制作词云图&词云图乱码问题等小坑

    词云图,大家一定见过,大数据时代大家经常见,我们今天就来用python的第三方库wordcloud,来制作一个大数据词云图,同时会降到这个过程中遇到的各种坑, 举个例子,下面是我从自己的微信上抓的微信 ...

  3. 【Python】【正则】

    1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...

  4. c assert 用法

    #include <stdio.h> /* printf */ #include <assert.h> /* assert */ void print_number(int* ...

  5. Jmeter ResponseAssertion 【Ignore Status】

    在Jmeter源码中AssertionGui.java中,定义了Ignore Status的作用域 /** * Checkbox to indicate whether the response sh ...

  6. MongoDB(课时12 字段判断)

    3.4.2.7 判断某个字段是否存在 使用“$exists”可以判断某个字段是否存在,如果设置为true表示存在,false表示不存在. 范例:查询具有parents成员的数据 db.students ...

  7. c语言中的0UL或1UL是什么意思

    0UL 表示 无符号长整型 0 1UL 表示 无符号长整型 1 如果不写UL后缀,系统默认为:int, 即,有符号整数. 1.数值常数有:整型常数.浮点常数:2.只有数值常数才有后缀说明:3.数值常数 ...

  8. centos7: iptables保存(配置完nginx的web规则后)

    centos7: iptables保存(配置完nginx的web规则后) 以本地虚拟机为例: 添加规则:入站规则 iptables -I INPUT -p tcp --dport 80 -j ACCE ...

  9. English trip -- VC(情景课)9 D Reading 阅读练习

    Read 阅读 Dear Susie(苏西) It's after dinner, My family is working in the kitchen. My daughter Li is  wa ...

  10. GetImageURL

    Sub GetImageUrl(ByVal URL As String) Dim strText As String Dim i As Long Dim OneImg With CreateObjec ...