网站首页 > 基础教程 正文
Visual C++ 6.0开发环境中显示当地日期与时间主要通过localtime()函数来实现,该函数的原型在time.h头文件中,其语法格式如下:
struct tm *localtime(xonst time_t *timer)
该函数的作用是把timer所指的时间(如函数time返回的时间)转换为当地标准时间,并以tm结构形式返回。其中,参数timer为主要获取当前时间的传递参数,格式为time_t指针类型。
而在Visual Studio 2010极其以后的版本,新增了安全函数,改成localtime_s(),语法格式也发生了变化:
- errno_t localtime_s(
- struct tm* _tm,
- const time_t *time
- );
其中:
- _tm
- 指向要填充的时间结构的指针。
- time
- 指针,指向存储的时间。
如果成功,返回值则为零。 如果失败,返回值将是错误代码。 错误代码是在 Errno.h 中定义的。
结构类型的字段 tm 存储下面的值,其中每个为 int。
- tm_sec
- 分钟后的几秒 (0-59)。
- tm_min
- 小时后的分钟 (0-59)。
- tm_hour
- 午夜后经过的小时 (0-23)。
- tm_mday
- 月 (1-31) 天。
- tm_mon
- 月 (0 – 11;年 1 月 = 0)。
- tm_year
- 年份 (当前年份减去 1900年)。
- tm_wday
- 星期几 (0 – 6;星期日 = 0)。
- tm_yday
- 每年的一天 (0-365;1 月 1 日 = 0)。
- tm_isdst
- 如果夏令时有效,则为,正值夏时制不起作用; 如果为 0如果夏时制的状态是未知的负值。 如果 TZ 设置环境变量,C 运行库会假定规则适用于美国境内用于实现夏令时 (DST) 计算。
下面以一个实例来输出当地日期与时间:
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- int main(void)
- {
- struct tm t; //tm结构指针
- time_t now; //声明time_t类型变量
- time(&now); //获取系统日期和时间
- localtime_s(&t, &now); //获取当地日期和时间
- //格式化输出本地时间
- printf("年:%d\n", t.tm_year + 1900);
- printf("月:%d\n", t.tm_mon + 1);
- printf("日:%d\n", t.tm_mday);
- printf("周:%d\n", t.tm_wday);
- printf("一年中:%d\n", t.tm_yday);
- printf("时:%d\n", t.tm_hour);
- printf("分:%d\n", t.tm_min);
- printf("秒:%d\n", t.tm_sec);
- printf("夏令时:%d\n", t.tm_isdst);
- system("pause");
- //getchar();
- return 0;
- }
猜你喜欢
- 2024-10-19 Python3 datetime模块指南:日期时间操作、时区管理与实战案例
- 2024-10-19 C++及数据结构复习笔记,类和对象很简单?为啥好多程序员还不会
- 2024-10-19 Linux时间和日期 linux 时间 表示方法
- 2024-10-19 C/C++的8种时间度量方式以及代码片段
- 2024-10-19 Python实战:使用 datetime模块处理时间日期的全方位指南
- 2024-10-19 C++编程的 42 条建议(四) c++编程100例
- 2024-10-19 mount with noatime - 合理关闭atime提高服务器性能
- 2024-10-19 linux下连续三次fork()——深度理解进程创建函数
- 2024-10-19 C函数time和clock的计时区别 c计时器函数
- 2024-10-19 win进程弹出Microsoft Visual C++ Runtime Library的解决办法
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)