◆time()

取得当前时间。此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果参数t为非空指针的话, 此函数也会将返回值存到t指针所指的内存。

成功则返回秒数, 失败则返回((time_t)-1)值, 错误原因存于errno中。

#include <time.h>
time_t time(time_t *t);

例:

#include <time.h>
#include <stdio.h> int main()
{ int seconds = time((time_t *)NULL);
printf("%d\n", seconds); return ;
}

运行结果:1517968358

◆gmtime()

返回当时时间,不过该函数返回的时间日期未经时区转换, 而是UTC时间

#include <time.h>
struct tm *gmtime(const time_t *timep);

例:

#include <time.h>
#include <stdio.h> int 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 \n", (+p->tm_year), (+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);
return ;
}

运行结果:

2018/2/7
Wed 1:55:53

◆localtime()

取得当地目前的时间和日期。与gmtime()函数不同的是,该函数返回的时间日期已经转换成当地时区

#include <time.h>
struct tm *localtime(const time_t *timep);

例:

#include <stdio.h>
#include <time.h> int main()
{ char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};\
time_t timep;
struct tm *p; time(&timep);
// Get local time
p = localtime(&timep);
printf("%d/%d/%d ", (+p->tm_year), (+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);
return ;
}

运行结果:

2018/2/7 Wed 10:0:32

◆strftime()

格式化日期时间。该函数会把结构体tm根据format所指定的字符串格式做转换,并将转换后的内容复制到参数s所指的字符串数组中。

#include <time.h>
size_t strftime(char *s, size_t max, const char *format,
const struct tm *tm);

例:

#include <time.h>
#include <stdio.h> int main()
{ char *format[] = {"%I, %M, %S, %p, %m/%d %a", "%x %X %Y", NULL};
char buf[];
int i;
time_t clock;
struct tm *tm;
time(&clock);
tm = gmtime(&clock); for (i = ; format[i] != NULL; i++) {
strftime(buf, sizeof(buf), format[i], tm);
printf("%s=> %s\n", format[i], buf);
}
return ;
}

运行结果:

%I, %M, %S, %p, %m/%d %a=> 02, 04, 53, AM, 02/07 Wed
%x %X %Y=> 02/07/18 02:04:53 2018

日期时间函数(1)-time()&gmtime()&strftime()&localtime()的更多相关文章

  1. SQLite日期时间函数

    SQLite日期时间函数 SQLite支持以下五个日期时间函数: date(timestring, modifier, modifier, …) time(timestring, modifier, ...

  2. Python与SQLite日期时间函数的使法

    SQLite的时间函数跟Python的时间函数有些许差别,所以稍做记录,供自己以后查询. 网上有将SQLite官方WIKI内容翻译成中文的文章,大家有兴趣可以搜索一下,我这里单纯记录一下个人比较常用的 ...

  3. PHP中日期时间函数date()用法总结

    date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...

  4. Python日期时间函数处理

    所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...

  5. mysql与oracle的日期/时间函数小结

    前言 本文的日期/时间全部格式化为”2016-01-01 01:01:01“形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时间转字符串函数:to ...

  6. Oracle日期时间函数大全

    ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...

  7. ORACLE 常用函数 日期/时间函数

    ---------------------------------------------日期/时间函数----------------------------------------------- ...

  8. MySQL日期时间函数大全(转)

    MySQL日期时间函数大全 DAYOFWEEK(date)  返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1 ...

  9. Mysql学习笔记(五)数学与日期时间函数

    学习内容: 1.数学函数 2.日期时间函数 这些函数都是很常用的函数...在这里进行简单的介绍... 数学函数: mysql); //取绝对值函数 这个函数可安全地使用于 BIGINT 值. mysq ...

随机推荐

  1. 算法笔记_195:历届试题 错误票据(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 某涉密单位下发了某种票据,并要在年终全部收回. 每张票据有唯一的ID号.全年所有票据的ID号是连续的,但ID的开始数码是随机选定的. 因为 ...

  2. Unix时间戳转换

    import time   def timestamp_datetime(value):     format = '%Y-%m-%d %H:%M:%S'     # value为传入的值为时间戳(整 ...

  3. WQL语言简介和WQL测试工具wbemtest.exe使用方法详细介绍

    这篇文章主要介绍了WQL语言简介和WQL测试工具wbemtest.exe使用方法详细介绍,WQL是指Windows管理规范查询语言,需要的朋友可以参考下 WQL就是WMI中的查询语言,WQL的全称是W ...

  4. Apache Hadoop 3.0新版本介绍及未来发展方向

    过去十年,Apache Hadoop从无到有,从理论概念演变到如今支撑起若干全球最大的生产集群.接下来的十年,Hadoop将继续壮大,并发展支撑新一轮的更大规模.高效和稳定的集群. 我们此次将向大家全 ...

  5. 【TP3.2】:日志记录和查看

    1.TP3.2手册日志类链接:http://document.thinkphp.cn/manual_3_2.html#log 2.日志默认路径:/Application/Runtime/Logs 3. ...

  6. java 运行时环境和编译器环境

    必须要保证运行环境高于编译环境 1.编译器的环境设置 单击项目右键-> Properties -> Java Compiler -> 5或6 如果编译器的环境高于运行时环境会报错. ...

  7. 一般web典型的项目目录结构

    本文转自:http://blog.sina.com.cn/s/blog_4758a28b0100l3lp.html WebRoot-       -common   (系统框架公用jsp 如foote ...

  8. 概率校准Probability Calibration

    在分类问题中,我们有时不仅仅需要给测试样本打上类别标签,也需要给出一个"置信度"来表示该样本属于此类别的可能性. 然而,有的分类器只能直接打上类别标签没法给出置信度.概率校准就是用 ...

  9. PHP5.5新特性

    详情见:http://www.php.net/manual/zh/migration55.new-features.php 1. 生成器 yield关键字 yield的中文文档在这里:http://p ...

  10. spring 4.0下集成webservice

    该教程使用的项目可参见: Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构 具体源码请参见GitHub: https://github.com ...