当前位置: 首页 > news >正文

宁波企业建站网站建设科技

宁波企业建站,网站建设科技,网站建设与维护的试题卷判断题,移除wordpress上边栏目录 1.strftime时间格式转换 1.1通用格式说明符: 1.2 strftime函数 1.2.1 C语言测试 1.2.2 shell脚本测试 2.asctime 函数‌ 2.1 asctime多线程不安全 2.2 asctime_r线程安全版本 3.asctime、ctime、strftime区别 1.strftime时间格式转换 strftime 是用于…

目录

1.strftime时间格式转换

1.1通用格式说明符:

1.2 strftime函数

1.2.1 C语言测试

1.2.2 shell脚本测试

2.asctime 函数‌

2.1 asctime多线程不安全

2.2 asctime_r线程安全版本

3.asctime、ctime、strftime区别


1.strftime时间格式转换

strftime 是用于将时间格式化为字符串的函数。它的核心是通过格式说明符(如 %Y、%m 等)将日期和时间转换为自定义字符串。

1.1通用格式说明符:

符号含义示例
%Y四位年份2025
%m两位月份04
%d两位日期07
%H24 小时制小时15
%I12 小时制小时03
%M分钟30
%S45
%A星期全称Monday
%a星期简写Mon
%B月份全称April
%b月份简写Apr
%pAM/PMPM
%F等价于 %Y-%m-%d2025-04-07
%T等价于 %H:%M:%S15:30:45
%c本地日期时间Mon Apr 7 15:30:45 2025
%x

本地日期(等价于 %m/%d/%y

 04/07/25   (2025年4月7日)
%X本地时间(等价于 %H:%M:%S)20:33:42 
%Z时区名称 CST
%z时区偏移 +0800
%j一年中的第几天(001-366)097
%U一年中的第几周(周日为一周起始)14
%W一年中的第几周(周一为一周起始)14

1.2 strftime函数

1.2.1 C语言测试

函数原型:

#include <time.h>
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);‌参数‌:
str:目标字符串缓冲区。
maxsize:缓冲区最大容量(防止溢出)。
format:格式字符串(使用 % 开头的符号)。
timeptr:指向 tm 结构体的指针(通过 localtime 或 gmtime 生成)。

程序:

#include <stdio.h>
#include <time.h>/*
struct tm 
{int tm_sec;   // 秒 [0, 60](允许闰秒)int tm_min;   // 分 [0, 59]int tm_hour;  // 时 [0, 23]int tm_mday;  // 日 [1, 31]int tm_mon;   // 月 [0, 11](0 表示 1 月)int tm_year;  // 年(实际年份 = tm_year + 1900)int tm_isdst; // 夏令时标志(>0: 生效,0: 不生效,<0: 自动判断)
};
*//*
关键步骤‌
1.使用 time() 获取当前时间戳(time_t 类型)。
2.通过 localtime() 将时间戳转换为本地时间的 tm 结构体。
3.调用 strftime 将 tm 结构体格式化为字符串。
*/int main() 
{time_t raw_time;struct tm *time_info;char buffer[64] = "";// 获取当前时间戳time(&raw_time);//将当前时间戳 转换为本地时间结构体time_info = localtime(&raw_time);// 格式化为字符串strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S 星期:%A ", time_info);printf("strftime Time: %s\n", buffer); /*%F	等价于 %Y-%m-%d		2025-04-07%T	等价于 %H:%M:%S		15:30:45*/strftime(buffer, sizeof(buffer), "%F %T 星期:%A ", time_info);printf("strftime Time: %s\n", buffer); // Mon Apr  7 20:33:42 2025 - 04/07/25 - 20:33:42strftime(buffer, sizeof(buffer), "%c - %x - %X ", time_info);printf("strftime Time: %s\n", buffer); // CST - +0800 - 097 - 14 - 14strftime(buffer, sizeof(buffer), "%Z - %z - %j - %U - %W", time_info);printf("strftime Time: %s\n", buffer); return 0;
}

运行结果:

注意:buffer空间大小需要大于存储时间字节。

1.2.2 shell脚本测试

程序:

#!/bin/bashecho "strftime 时间格式符 shell脚本测试"# 示例:输出当前时间(格式:2025-04-07 15:30:45)
date +"%Y-%m-%d %H:%M:%S"# 输出:Monday, April 07, 2025 03:30 PM
date +"%A, %B %d, %Y %I:%M %p"# 输出时间戳(秒级)
date +"%s"str=$(date +"%F %T")#输出变量
echo "str:"
echo $str

运行结果:

2.asctime 函数‌

2.1 asctime多线程不安全

函数功能‌:

asctime 是 C 标准库中的函数,用于将 struct tm 时间结构体转换为‌固定格式的字符串‌。 输出格式示例:Wed Jun 30 21:49:08 1993\n(末尾自动添加换行符 \n 和终止符 \0

函数原型:

#include <time.h>
char *asctime(const struct tm *timeptr);参数‌:
timeptr – 指向 struct tm 结构体的指针(通常由 localtime 或 gmtime 生成)。
‌返回值‌:
返回静态分配的字符串指针(格式固定),‌多线程不安全‌。

输出格式:

输出格式说明‌
生成的字符串格式固定为:
星期缩写 月份缩写 日 时:分:秒 年份\n\0‌字段细节‌:
‌星期缩写‌:3 字母(如 Mon, Tue)。
‌月份缩写‌:3 字母(如 Jan, Apr)。
‌日‌:若为个位数,前方补空格(如 7 显示为 7)。
‌时间‌:24 小时制(如 15:30:45)。
‌年份‌:4 位数字(如 2025)。

程序:

#include <stdio.h>
#include <time.h>/*
struct tm 
{int tm_sec;   // 秒 [0, 60](允许闰秒)int tm_min;   // 分 [0, 59]int tm_hour;  // 时 [0, 23]int tm_mday;  // 日 [1, 31]int tm_mon;   // 月 [0, 11](0 表示 1 月)int tm_year;  // 年(实际年份 = tm_year + 1900)int tm_isdst; // 夏令时标志(>0: 生效,0: 不生效,<0: 自动判断)
};
*/int main() 
{time_t raw_time;struct tm *time_info;// 获取当前时间戳time(&raw_time);// 转换为本地时间的 tm 结构体time_info = localtime(&raw_time);// 转换为固定格式字符串char *time_str = asctime(time_info);printf("asctime: %s", time_str);// 输出示例:Wed Apr  7 15:30:45 2025return 0;
}

运行结果:

2.2 asctime_r线程安全版本

asctime_r 是 Linux 特有的线程安全函数,需手动提供缓冲区:

函数原型:

#include <time.h>
char *asctime_r(const struct tm *timeptr, char *buf);参数‌:
timeptr:指向 struct tm 结构体的指针(通常由 localtime_r或 gmtime_r 生成)。buf:用户提供的缓冲区,‌长度至少为 26 字节‌(固定格式字符串占 26 字节,
包含换行符 \n 和终止符 \0)。‌返回值‌:
成功:返回指向 buf 的指针。
失败:返回 NULL(如 timeptr 或 buf 为非法指针)。

使用步骤:

  1. 定义缓冲区(≥26 字节)。
  2. 获取时间戳并转换为 struct tm(使用线程安全的 localtime_r 或 gmtime_r)。
  3. 调用 asctime_r 转换时间格式

程序:

#include <stdio.h>
#include <time.h>/*
struct tm 
{int tm_sec;   // 秒 [0, 60](允许闰秒)int tm_min;   // 分 [0, 59]int tm_hour;  // 时 [0, 23]int tm_mday;  // 日 [1, 31]int tm_mon;   // 月 [0, 11](0 表示 1 月)int tm_year;  // 年(实际年份 = tm_year + 1900)int tm_isdst; // 夏令时标志(>0: 生效,0: 不生效,<0: 自动判断)
};
*/int main()
{time_t raw_time;struct tm time_info;char buffer[64];  // 必须至少 26 字节// 获取当前时间戳time(&raw_time);// 转换为本地时间(线程安全)localtime_r(&raw_time, &time_info);// 转换为固定格式字符串if (asctime_r(&time_info, buffer) != NULL) {printf("Time: %s", buffer);  // 输出示例:Mon Apr  7 15:30:45 2025\n} else {perror("asctime_r failed");}return 0;
}

运行结果:

3.asctime、ctime、strftime区别

特性asctimectimestrftime
输入类型struct tmtime_tstruct tm
输出格式固定格式固定格式(本地时间)完全自定义
线程安全
缓冲区来源静态内存静态内存用户提供
灵活性
替代方案asctime_r(线程安全)ctime_r(线程安全)无(本身安全)

1.关键区别

(1)输入参数:

  • asctime 处理 struct tm。
  • ctime 处理 time_t(自动转换为本地时间)。
  • strftime 处理 struct tm,但允许自定义格式。

(2)线程安全:

  • asctime 和 ctime 非线程安全(依赖静态内存)。
  • strftime 线程安全(需用户管理缓冲区)。

(3)格式控制:

  • asctime 和 ctime 输出固定格式。
  • strftime 支持任意格式(如 %Y-%m-%d)。

2.选择建议

  • 简单输出:单线程下用 asctime 或 ctime。
  • 多线程:改用 asctime_r/ctime_r 或 strftime。
  • 自定义格式:必须使用 strftime。

 

http://www.dtcms.com/a/471250.html

相关文章:

  • 网站开发学习什么网站建设实施规范
  • 安徽建设厅网站进不去郑州网站优化公司平台
  • 如何在网站投放广告wordpress标题后乱码
  • 【C++学习】继承和多态
  • 开发一个网站需要多少人杭州品牌vi设计公司
  • 韩雪冬做网站多少钱网址搜索ip地址
  • Google 智能体设计模式:探索与发现
  • 湛江购房网官方网站沈阳点金网站建设
  • 靖江网站设计做网站服务好
  • 制作网站的素材wordpress怎么改表缀
  • 合肥网站制作公司有哪些公司网站维护中 源码
  • C++的string类
  • 【软件设计师中级】计算机组成与结构(五):指令系统与计算机体系结构 - CPU的“思维语言“与架构蓝图
  • 柳州网站建设数公式大全wordpress 输出the id
  • 17网站一起做网店杭州wordpress 当前页面
  • 百度推广客户端兰州网站seo收费
  • 建站系统主要包括什么自适应网站导航怎么做
  • 服务器运维(五)服务器漏洞扫描赛博修仙版本——东方仙化神期
  • 深圳建专业网站上海网站关键词优化服务
  • c语言-运算符
  • 从AAAI2025中挑选出对目标检测有帮助的文献——第一期
  • Flowise与cpolar:AI工作流的无界运行解决方案
  • 做网站的三个软件兼职做视频的网站
  • 营销型网站的好处企业网站 生成html
  • AI学习日记——卷积神经网络(CNN):完整实现与可视化分析
  • 【开题答辩全过程】以 报修系统为例,包含答辩的问题和答案
  • 中国做木线条的网站北京高档网站建设
  • 【前端状态管理技术解析:Redux 与 Vue 生态对比】
  • 做标书有哪些网站能接到网站弹广告是什么样做的
  • 第145期《2025年AI现状报告》解读(三):安全篇