相关疑难解决方法(0)

什么是time_t typedef到底是什么?

我在linux框中搜索并看到它是typedef

typedef __time_t time_t;
Run Code Online (Sandbox Code Playgroud)

但找不到__time_t定义.

c linux types

188
推荐指数
6
解决办法
27万
查看次数

使用printf打印clock_t的正确方法是什么?

我目前正在使用显式强制转换unsigned long long并使用%llu它来打印它,但既然size_t有说明%z符,为什么没有clock_t

甚至没有宏观.也许我可以假设在x64系统(操作系统和CPU)size_t上长度为8个字节(即使在这种情况下,他们已提供%z),但是呢clock_t

c io printf clock

29
推荐指数
3
解决办法
6万
查看次数

time_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)

c

8
推荐指数
4
解决办法
3万
查看次数

用C(-Wall噩梦)正确记录UNIX时间

可能重复:
什么原始数据类型是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编译 - 这真的不应该是一个问题,但它让我疯了.

c

3
推荐指数
1
解决办法
409
查看次数

给定一个时间点,如何获得自纪元以来的秒数?

我有一个来自我正在使用的库中的函数,它需要一个 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)

问题是编译器说可能会丢失数据。

c++ time c++11 c++-chrono

2
推荐指数
1
解决办法
5820
查看次数

为什么第一次而不是第二次正确打印变量"秒"?

首先要说的是,我在编程中是一个绝对的菜鸟,所以它可能是一个非常简单的事情,我没有得到它.

我想知道从一天开始以来已经过了多少时间,为此我使用了这个time()功能.

那么我想打印出来,这里是我的问题:与第一个printf变量seconds打印正确,但在第二个printf(其中我打印millsseconds)它给了我一个错误的输出.

代码:

#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)

c printf

1
推荐指数
1
解决办法
111
查看次数

标签 统计

c ×5

printf ×2

c++ ×1

c++-chrono ×1

c++11 ×1

clock ×1

io ×1

linux ×1

time ×1

types ×1