我在linux框中搜索并看到它是typedef
typedef __time_t time_t;
Run Code Online (Sandbox Code Playgroud)
但找不到__time_t定义.
我目前正在使用显式强制转换unsigned long long
并使用%llu
它来打印它,但既然size_t
有说明%z
符,为什么没有clock_t
?
甚至没有宏观.也许我可以假设在x64系统(操作系统和CPU)size_t
上长度为8个字节(即使在这种情况下,他们已提供%z
),但是呢clock_t
?
#include <stdio.h>
#include <time.h>
int main()
{
printf("Size of time_t is %lu bytes.\n", sizeof(time_t));
time_t biggest = 0x7fffffffffffffff; // line 1
printf("time_t's wrap around will be a second after %s.\n", asctime(gmtime(&biggest)) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在time.h中,这是time_t的定义吗?
typedef __darwin_time_t time_t
Run Code Online (Sandbox Code Playgroud)
怎么解释呢?由于我不知道,我使用sizeof函数来查找time_t = 8个字节.
为什么第1行会给出错误呢?我收到了这个错误
Segmentation fault: 11
Run Code Online (Sandbox Code Playgroud) 可能重复:
什么原始数据类型是time_t?
基本上,我想要做的就是产生当前的UNIX时间(从时间(NULL)得到的结果)并将其打印到我打开的文件中.
我尝试过以下代码:
fprintf(f, "%i", time(NULL));
Run Code Online (Sandbox Code Playgroud)
但我得到这些恼人的编译器警告:
src/database-createtbl.c:140: warning: int format, time_t arg (arg 3)
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用-Wall编译 - 这真的不应该是一个问题,但它让我疯了.
我有一个来自我正在使用的库中的函数,它需要一个 double 作为参数。它需要传递一个纳秒类型的偏移量加上 sytem_clock::now()。到目前为止我有这个代码:
system_clock::time_point now = std::chrono::system_clock::now();
auto timepointoffset = (now + desiredOffset);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
编辑:所以我需要补充一点,问题是我需要在不存在丢失数据风险的情况下进行操作。我有这个代码:
system_clock::time_point now = std::chrono::system_clock::now();
auto timepointoffset = std::chrono::time_point_cast<std::chrono::nanoseconds>(now + desiredOffset);
double value = timepointoffset.time_since_epoch().count();
Run Code Online (Sandbox Code Playgroud)
问题是编译器说可能会丢失数据。
首先要说的是,我在编程中是一个绝对的菜鸟,所以它可能是一个非常简单的事情,我没有得到它.
我想知道从一天开始以来已经过了多少时间,为此我使用了这个time()
功能.
那么我想打印出来,这里是我的问题:与第一个printf变量seconds
打印正确,但在第二个printf
(其中我打印mills
和seconds
)它给了我一个错误的输出.
代码:
#include <stdio.h>
#include <time.h>
int main(void) {
long long int mills, seconds;
mills = time(NULL);
printf("Mills: %i\n", mills );
seconds = mills / 1000;
//here the variable is printed correctly
printf("Seconds: %i\n", seconds );
//here mills gets printed correctly but seconds gets printed as 0
printf("Milliseconds since midnight: %i\nSeconds since midnight: %i\n", mills, seconds);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Mills: 1486143107
Seconds: 1486143
Milliseconds since midnight: 1486143107 …
Run Code Online (Sandbox Code Playgroud)