日期时间函数(1)-time()&gmtime()&strftime()&localtime()
◆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()的更多相关文章
- SQLite日期时间函数
SQLite日期时间函数 SQLite支持以下五个日期时间函数: date(timestring, modifier, modifier, …) time(timestring, modifier, ...
- Python与SQLite日期时间函数的使法
SQLite的时间函数跟Python的时间函数有些许差别,所以稍做记录,供自己以后查询. 网上有将SQLite官方WIKI内容翻译成中文的文章,大家有兴趣可以搜索一下,我这里单纯记录一下个人比较常用的 ...
- PHP中日期时间函数date()用法总结
date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...
- Python日期时间函数处理
所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...
- mysql与oracle的日期/时间函数小结
前言 本文的日期/时间全部格式化为”2016-01-01 01:01:01“形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时间转字符串函数:to ...
- Oracle日期时间函数大全
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...
- ORACLE 常用函数 日期/时间函数
---------------------------------------------日期/时间函数----------------------------------------------- ...
- MySQL日期时间函数大全(转)
MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1 ...
- Mysql学习笔记(五)数学与日期时间函数
学习内容: 1.数学函数 2.日期时间函数 这些函数都是很常用的函数...在这里进行简单的介绍... 数学函数: mysql); //取绝对值函数 这个函数可安全地使用于 BIGINT 值. mysq ...
随机推荐
- UISlider设置按钮透明
UISlider *aslider = [[UISlider alloc]initWithFrame:kCR(, , , )]; [aslider setValue:0.5]; [aslider se ...
- 【CSS】瀑布流布局的两种方式:传统多列浮动和绝对定位布局
传统多列浮动 各列固定宽度,并且左浮动: 一列中的数据块为一组,列中的每个数据块依次排列即可: 更多数据加载时,需要分别插入到不同的列上: 优点: (1)布局简单,应该说没啥特别的难点: (2)不用明 ...
- window中findstr命令的用法
http://www.netingcn.com/window-findstr-command.html findstr是window系统自带的命令,用途是查找指定的一个或多个文件文件中包含(或通过参数 ...
- Modifying a Request or Response
To make custom changes to web requests and responses, use FiddlerScript to add rules to Fiddler's On ...
- Statusbar
Main window The QtGui.QMainWindow class provides a main application window. This enables to create a ...
- php如何实现页面跳转
•PHP页面跳转一.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header ...
- $stateParams 详解
如何传递参数(参考 http://www.cnblogs.com/jager/p/5293225.html) 首先,要在目标页面定义接受的参数: 传参, ui-sref: $state.go: 接收 ...
- 用Java实现AES加密(坑!)
大坑!使用SecureRandom默认的加密方式即SHA1PRNG生成的密码有误,即使使用相同的password来生成,不同runtime或时刻生成的随机密码也有可能不同,造成的错误为javax.cr ...
- 〖Linux〗Shell脚本修改输出文字颜色
Shell函数: echocolor(){ color=${} && shift case ${color} in black) echo -e "\e[0;30m${@}\ ...
- TouchID 指纹解锁
概述 TouchID 指纹解锁 详细 代码下载:http://www.demodashi.com/demo/10701.html 一.软硬件支持 指纹验证功能的最低硬件支持为iPhone5s, iPa ...