◆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. Eclipse中SVN修改的*星号没了,解决方法

    Eclipse中SVN修改的*星号没了,解决方法 打开Preference 第一步:去掉外加的 ">" 第二步:勾选Outgoing changes 这样做之后," ...

  2. ubuntu server 安装 mantis bug tracker 中文配置

    ubuntu server 安装 mantis bug tracker 中文配置 官网:http://www.mantisbt.org/ 一:安装: 1:进入到 apache2的网站目录: cd /v ...

  3. 虚拟机stack全分析

    通过jps -lv 获取到本地的一个JVM实例进程.再通过jstack pid  > thread.txt ,把stack trace输出到thread.txt文件中. 2012-08-28 2 ...

  4. STS 自动生成 getter 和 setter

    1.Source --  Generate Getters and Setters

  5. 为什么要有GDT

    逻辑地址-------------->线性地址------------> 物理地址   分段 分页 GDT是[gobal (segment) descriptor table]的缩写,它保 ...

  6. js-取值&赋值-获取某标签某属性的值

      js 取值&赋值-获取某标签某属性的值 CreateTime--2016年10月16日16:35:34 Author:Marydon 1.取值 //方法一 //自定义属性必须用getAtt ...

  7. 〖Linux〗以后台方式启动/结束指定程序/命令(不受 exit 或点击窗口关闭按钮等终端退出操作的影响)

    #!/bin/bash - #=============================================================================== # # F ...

  8. message 弹出窗口

    import  javax.swing.JOptionPane;public class gong {    public static void main(String [] args){      ...

  9. deque容器的运用一点一点积累

    #include<iostream> #include<deque> #include<cstdio> #include<cstring> #inclu ...

  10. 如何用python轻松破解wifi密码( 源码 )

    摘要: 我得说明下这个东西一点都不高端,甚至看起来有点糟糕.而且用的是单线程~,因为过几天要搬家了,于是.. 环境准备 python2.7 凑合的linux 差不多的无线网卡 pywifi模块 弱口令 ...