【问题标题】:how to get timestamp in c如何在c中获取时间戳
【发布时间】:2013-03-15 15:22:54
【问题描述】:

我想获取我登录 c 的时间戳。 我写了一个函数来获取时间戳。 但是当我返回变量时,我得到了不同的值。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char* get_timestamp(){
   time_t rawtime;
   struct tm * timeinfo;
   char buffer[16];
   time (&rawtime);
   timeinfo = localtime (&rawtime);
   strftime (buffer,16,"%G%m%d%H%M%S",timeinfo);
   puts(buffer);
   return buffer; 
}

int main() 
{
   puts(get_timestamp());
   return 0;
}

输出:

20130315204815
Ir?0315204815

谁能帮帮我...谢谢。

【问题讨论】:

  • 你到底为什么用 Boost、C++ 和 C++11 来标记它??

标签: c


【解决方案1】:

buffer[16] 是一个本地数组,它在char* get_timestamp() 函数的末尾停止存在。然后你返回一个指向不存在的数组的指针。

【讨论】:

    【解决方案2】:

    您正在返回一个指向堆栈变量的指针,因此在函数返回后使用它是无效的:

     char buffer[16];
    

    将在函数的栈上分配。当您返回时,堆栈被清理并且buffer 不再有效。通过最小的更改,这可能是一个更好的函数签名:

    void get_timestamp( char *buffer, size_t buffLen  )
    

    假设您在调用get_timestamp 之前已为buffer 正确分配了空间。

    【讨论】:

    • 任何类似这样的函数也应该接受缓冲区大小,因为核心函数 (strftime()) 需要知道缓冲区大小以防止溢出。
    【解决方案3】:

    正如另一个人所说,您正在使用驻留在堆栈上的数据,一旦您离开声明它的函数,就会停止存在。我看到解决这个问题的两种简单方法:

    方案一:在调用函数中分配缓冲区变量,并将指针传递给get_timestamp

    void get_timestamp( char *buffer, size_t buffersize ) {
        ....
        strftime (buffer,buffersize,"%G%m%d%H%M%S",timeinfo);
    }
    
    int main() 
    {
       char buffer[16];
       puts(get_timestamp(buffer,16));
       return 0;
    }
    

    注意/编辑:我折叠了 unwind 关于将缓冲区大小传递到这个建议的解决方案中的非常有效的评论。

    选项 2:如果您不能或不想更改函数的签名,则可以使用静态变量,但不要忘记静态变量会导致多线程程序出现问题。

    static char buffer[16];
    
    char* get_timestamp(){
       ... 
    }
    
    int main() 
    {
       puts(get_timestamp());
       return 0;
    }
    

    您当然可以使用malloc,但在这种情况下这似乎有点过头了,而且比我描述的两个修复更容易出错。

    【讨论】:

    • 最好将buffersize 指定为size_t
    【解决方案4】:

    您返回的字符串是一个自动变量。当你退出函数访问这个变量是不可能的。根据规范,这是未定义的行为。使用 malloc 分配字符串,你会没事的。 只是不要忘记之后释放它。

    【讨论】:

      【解决方案5】:

      在回答这个问题时,我想要一个简单、线程友好、不返回 char*(管理起来通常很乏味)、线程安全且可以自立的函数。我讨厌返回 char* 或必须管理的指针的函数。

      下面的函数不调用malloc。

      该函数不接受任何参数并返回一个时间戳。我觉得效果不错。

      struct Timestamp {
          time_t seconds;
          long milliseconds;
          char timestring[32];
      };
      
      
      struct Timestamp getTimestamp()
      {
      char   timebuffer[32]     = {0};
      struct timeval  tv        = {0};
      struct tm      *tmval     = NULL;
      struct tm       gmtval    = {0};
      struct timespec curtime   = {0};
      
      struct Timestamp timestamp;
      
      int i = 0;
      
      // Get current time
      clock_gettime(CLOCK_REALTIME, &curtime);
      
      
      // Set the fields
      timestamp.seconds      = curtime.tv_sec;
      timestamp.milliseconds = round(curtime.tv_nsec/1.0e6);
      
      if((tmval = gmtime_r(&timestamp.seconds, &gmtval)) != NULL)
      {
          // Build the first part of the time
          strftime(timebuffer, sizeof timebuffer, "%Y-%m-%d %H:%M:%S", &gmtval);
      
          // Add the milliseconds part and build the time string
          snprintf(timestamp.timestring, sizeof timestamp.timestring, "%s.%03ld", timebuffer, timestamp.milliseconds); 
      }
      
      return timestamp;
      }
      
      int main()
      {
          char   timebuffer[64]     = {0};
          int i = 0;
          struct timespec sleeptime = {0, 5000000L};
      
          struct Timestamp timestamp;
      
          for (i=0; i < 20; i++)
          {
              timestamp = getTimestamp();
              printf("Time is: %s \n", timestamp.timestring);
              nanosleep(&sleeptime, NULL);
          }
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-16
        • 2012-10-18
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        • 2012-04-03
        • 2019-06-05
        相关资源
        最近更新 更多