- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
根据documentation struct tm *gmtime(const time_t *timer);
应该将 timer
指向的 time_t
转换为分解时间。
现在他们决定让函数接受指向 time_t
的指针而不是直接传递 time_t
是有原因的吗?
据我所知,time_t
是算术类型,因此应该可以直接传递(而且我发现它适合 long
).似乎还有对 NULL
指针的任何特定处理(这可能会激发传递指针)。
有什么我想念的吗?今天还有相关的东西吗?
最佳答案
据我所知,这更像是一个历史怪癖。当 time.h 首次引入时,它的功能类似于 time
,它使用了一个无法返回的值(即:没有 long int
等)。该标准定义了一个晦涩的 time_t
类型,它仍然为供应商以奇怪的方式实现提供了很大的空间(它必须是算术类型,但例如没有定义范围或最大值)- C11 标准:
7.27.1 Components of time.
[...]
3 .The types declared aresize_t
(described in 7.19);
clock_t
and
time_t
which are real types capable of representing times;
4. The range and precision of times representable inclock_t
andtime_t
are implementation-defined.
size_t
在 C11 中被描述为“是 sizeof 运算符结果的无符号整数类型;”
鉴于此,您的评论(“我发现它适合 long
是合理的”)是可以理解的,但它是不正确的,或者至少是不准确的。例如,POSIX 要求 time_t
是整数或真正的浮点类型。 long
符合该描述,但 long double
不符合 long
。更准确的假设是 time_t
的最小大小是 32 位 int(至少到 2038 年),但 time_t
最好是 64 位类型。
无论如何,在那些日子里,如果无法返回一个值,唯一的选择就是将内存传递给函数(这是一个明智的做法)。
这就是为什么我们有这样的功能
time_t time(time_t *t);
两次设置相同的值并没有什么意义:一次通过返回它,一次使用间接寻址,但是参数在那里,因为最初,函数被定义为
time(time_t *t)
注意缺少返回类型,如果今天添加 time
,它要么被定义为 void time( time_t * )
,要么如果委员会没有喝酒,并意识到在这里传递指针的荒谬性,他们将定义它作为 time_t time ( void );
查看有关时间函数的 C11 标准,似乎函数行为的重点在于返回值。指针参数被简要提及,但它肯定没有任何意义:
7.27.2.4 The time function
1. Synopsis
#include <time.h>
time_t time(time_t *timer);
2. Description
The time function determines the current calendar time. The encoding of the value is unspecified.
3. Returns
The time function returns the implementation’s best approximation to the current calendar time. The value (
time_t
)(-1) is returned if the calendar time is not available. If timer is not a null pointer, the return value is also assigned to the object it points to.
我们可以从中得出的主要结论是,就标准而言,指针只是获取函数返回值的次要方式。鉴于返回值表明出现了错误 ( (time_t)(-1)
),我认为我们应该将此函数视为 time_t time( void )
。
但是由于旧的实现仍在运行,而且我们都已经习惯了它,所以它应该被标记为弃用,但因为它从来没有真正被弃用,所以它可能会成为永远的C语言...
函数像这样使用 time_t
(const
) 的唯一其他原因要么是历史原因,要么是为了在 time.h
API 中保持一致的 API。据我所知
出于历史、兼容性和一致性原因,大多数 time.h
函数都使用指向 time_t
类型的指针。
我知道我以前读过关于 time
函数早期的那些东西,here's a related SO answer
关于c - 为什么 `gmtime` 需要一个指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837440/
我有一个开始的问题 here .我发现了原因并且没有尝试解决其他问题。 我需要将 CherryPy 设置为在不同平台上可用的最长可能 session 时间。为此 CherryPy 使用 time.gm
我正在用 c++/c 检索 GMT 时间。但它返回不正确的时间。例如,现在的时间应该是上午 9 点,但 struct tm 对象返回凌晨 3 点。知道为什么或我做错了什么吗? time_t rawti
我在嵌入式 Linux 上工作,自 1970 年 1 月 1 日起,系统/ Controller 时间以毫秒的形式保持。我正在尝试使用 gmtime 但无法获得准确的结果。将此时间(毫秒)转换为实时小
我在多线程环境中打印事件,控制台是所有线程共享的静态互斥锁。 问题是随机发生的每一百或一千个事件,我得到的时间是本地时间而不是 UTC。 我只在 Linux 机器(使用 g++ 构建)中看到过这个错误
我有这个代码: time_t tt = time(NULL); tm* currentTime = gmtime(&tt); tm* storedTime = gmtime(&m_time); 其中m
我有一个在 Cubox(armhf 平台)上运行的网络服务器(CherryPy),启动服务器后出现以下错误: [14/Aug/2015:09:33:40] HTTP Traceback (most r
是否有一种预定义的方法可以将小时和分钟添加到通过 time.h 获取的 gmtime 中?我想编写一个程序来显示各个国家的时间,那么我必须自己编写逻辑还是有预定义的方法? 最佳答案 如果您有时区名称(
如果我的服务器未配置为使用格林威治标准时间,我不明白为什么 gmdate() 和 date() 返回相同的值。 这是为什么? echo time(); // 13110
将 C 代码迁移到 Swift,对应的等效代码是什么? time(&CurrentTime); tm = gmtime(&CurrentTime); dayOfYear = tm->tm_yday +
根据documentation struct tm *gmtime(const time_t *timer); 应该将 timer 指向的 time_t 转换为分解时间。 现在他们决定让函数接受指向
import time print time.strftime("%a, %d %b %Y %I:%M %p %Z", time.gmtime()) 我住在加利福尼亚。出于某种原因,此代码以 GMT
在谷歌应用引擎 ( http://shell.appspot.com/ ) 上: >>> time.gmtime(1000*365*24*60*60) (2969, 5, 3, 0, 0, 0, 2,
我正在尝试使用 epoch 时间在 STM32L051T6 微 Controller 上设置 RTC 时间。下面是我的代码。 void Rtc_SetTime(uint32_t time_) {
有两个标准的 C 库函数 gmtime() and localtime() .他们都返回这样的结构(一个在 GMT,一个在本地 TZ 和夏令时): struct tm { int tm_sec
我尝试: ts = -216345600000 datetime.datetime.fromtimestamp(ts/1000) ValueError:时间戳超出平台 localtime()/gmti
我有一个在 TZ=UTC 运行的服务器,我有这样的代码: time_t t = time(NULL); struct tm tm; gmtime_r(&t, &tm); 问题是 tm.tm_sec =
如何逆向gmtime(),把时间+日期放在哪里,得到秒数? 我有类似 'Jul 9, 2009 @ 20:02:58 UTC' 之类的字符串,我想返回 epoch 和 2009 年 7 月 9 日之间
我有以下测试代码来查看 gmtime 和 localtime 之间的差异。但他们给了我相同的结果: UTC:2013-05-02T13:59:58 Local:2013-05-02T13:59:58
我有一个 C 程序,我在其中使用遍历目录结构的递归函数。它接受一个 time_t 变量并从中创建一个 gmtime 结构。 递归dir函数来自How to recursively list direc
我一直在搜索谷歌机器,但没有想出任何答案。调用电话时: time.gmtime() 这最终会返回一个时间,正如主题行所说,比我的系统时间早 5 小时。我无法弄清楚发生了什么。 time.tzname
我是一名优秀的程序员,十分优秀!