localtime函数和strftime函数
localtime函数
功能:
把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtime函数转换后的时间没有经过时区变换,是UTC时间 。
用法:
#include <time.h>
struct tm *localtime(const time_t *clock);
返回值:
指向tm 结构体的指针, tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.
strftime函数
功能:
根据区域设置,格式化本地时间/日期,功能就是将时间格式化
用法:
#include <time.h>
size_t strftime(char * strDest, size_t maxszie, const char * format, const struct tm* timeptr);
示例如下:
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main(void)
{
char ct[80];
time_t tt;
struct tm *tblock;
tt = time(NULL);
tblock = localtime(&tt);
printf("Local time is %s\n", asctime(tblock));
strftime(ct, sizeof(ct), "%Y-%m-%d %H:%M:%S", tblock);
printf("now is %s\n", ct);
return 0;
}
结果:

备注:
strftime函数常使用的formate参数:
| specifier | Replaced by | Example |
|---|---|---|
| %a | Abbreviated weekday name * | Thu |
| %A | Full weekday name * | Thursday |
| %b | Abbreviated month name * | Aug |
| %B | Full month name * | August |
| %c | Date and time representation * | Thu Aug 23 14:55:02 2001 |
| %d | Day of the month (01-31) | 23 |
| %H | Hour in 24h format (00-23) | 14 |
| %I | Hour in 12h format (01-12) | 02 |
| %j | Day of the year (001-366) | 235 |
| %m | Month as a decimal number (01-12) | 08 |
| %M | Minute (00-59) | 55 |
| %p | AM or PM designation | PM |
| %S | Second (00-61) | 02 |
| %U | Week number with the first Sunday as the first day of week one (00-53) | 33 |
| %w | Weekday as a decimal number with Sunday as 0 (0-6) | 4 |
| %W | Week number with the first Monday as the first day of week one (00-53) | 34 |
| %x | Date representation * | 08/23/01 |
| %X | Time representation * | 14:55:02 |
| %y | Year, last two digits (00-99) | 01 |
| %Y | Year | 2001 |
| %Z | Timezone name or abbreviation | CDT |
| %% | A % sign | % |
localtime函数和strftime函数的更多相关文章
- SQL-27 给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。 提示:在sqlite中获取datetime时间对应的年份函数为strftime('%Y', to_date)
题目描述 给出每个员工每年薪水涨幅超过5000的员工编号emp_no.薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列. 提示:在s ...
- PHP strftime() 函数
------------恢复内容开始------------ 实例 根据区域设置格式化本地日期和时间: <?php echo(strftime("%B %d %Y, %X %Z&quo ...
- day20 匿名函数,内置函数,面向过程编程
目录 有名函数 匿名函数 max() min() sorted map filter 内置函数 面向过程编程 有名函数 def f1(): print('my name is f1') f1() my ...
- python之函数对象、函数嵌套、名称空间与作用域、装饰器
一 函数对象 一 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 二 利用该特性,优雅的取代多分支的if de ...
- python之旅:函数对象、函数嵌套、名称空间与作用域、装饰器
一 函数对象 一 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 二 利用该特性,优雅的取代多分支的if de ...
- mysql 函数 时间函数,数学函数,字符串函数,条件判断函数
=========================================== mysql 相关函数 ============================================= ...
- 深入理解javascript函数定义与函数作用域
最近在学习javascript的函数,函数是javascript的一等对象,想要学好javascript,就必须深刻理解函数.本人把思路整理成文章,一是为了加深自己函数的理解,二是给读者提供学习的途径 ...
- JavaScript 函数节流和函数去抖应用场景辨析
概述 也是好久没更新 源码解读,看着房价蹭蹭暴涨,心里也是五味杂陈,对未来充满恐惧和迷茫 ...(敢问一句你们上岸了吗) 言归正传,今天要介绍的是 underscore 中两个重要的方法,函数节流和函 ...
- 如果你也会C#,那不妨了解下F#(4):了解函数及常用函数
函数式编程其实就是按照数学上的函数运算思想来实现计算机上的运算.虽然我们不需要深入了解数学函数的知识,但应该清楚函数式编程的基础是来自于数学. 例如数学函数\(f(x) = x^2+x\),并没有指定 ...
随机推荐
- appcms SSRF 绕过漏洞[转载]
漏洞 <?php if(isset($_GET['url']) && trim($_GET['url']) != '' && isset($_GET['type' ...
- int、bool和str
int bit_length 返回以二进制表示的最短长度 print(int.bit_length(10)) 结果 4 Process finished with exit code 0 int() ...
- mosh
mosh 是一款使用 UDP 连接 C/S 的终端工具, 服务器只需安装好 mosh 套件, 并启动 SSH 服务, 等待 Client 连接即可. Client (mosh-client) 连接时, ...
- Android几个比较有用的插件
1.Android Drawable Importer 2.Android ButterKnife Zelezny 使用方法,在SetContentView上右键,Generate 3.Androi ...
- 21.Buffer Pool与压缩页/CheckPoint/LSN
一. 思考题解析• 查看Buffer Pool中的Flush List不要在线上操作该SQL语句,开销较大 SELECT pool_id, lru_position, space, page_numb ...
- vivalidi 一款由Web技术诞生的Web浏览器
vivalidi https://vivaldi.com/ A million ways to customize everything The world is a colorful place b ...
- 二十二、Linux 进程与信号---进程创建
22.1 fork 和 vfork 函数 22.1.1 函数说明 #include <unistd.h> #include <sys/types.h> pid_t fork( ...
- python 学习 argparse
1. argparse python 中用于解析命令行参数和选项的标准模块 创建一个解析器对象,解析器类ArgumentParser add_argument() 方法用来指定程序接受那些命令行参数, ...
- 关于MySQL常用的查询语句
一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>,= ...
- luogu P5112 FZOUTSY
传送门 毒瘤出题人,卡我常数 如果后缀排序后将两两之间height\(\ge k\)的后缀放在一组,那么每次询问的答案就是\(\sum_{i} \binom{num[i]}{2}\)(num[i]是第 ...