C语言 以秒为单位获取当前时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2242963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:27:08  来源:igfitidea点击:

get the current time in seconds

c

提问by ant2009

I am wondering is there any function that would return the current time in seconds, just 2 digits of seconds? I'm using gcc 4.4.2.

我想知道是否有任何函数可以以秒为单位返回当前时间,只有 2 位秒?我正在使用 gcc 4.4.2。

回答by paxdiablo

The following complete program shows you how to access the seconds value:

以下完整程序向您展示了如何访问秒值:

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

int main (int argc, char *argv[]) {
    time_t now;
    struct tm *tm;

    now = time(0);
    if ((tm = localtime (&now)) == NULL) {
        printf ("Error extracting time stuff\n");
        return 1;
    }

    printf ("%04d-%02d-%02d %02d:%02d:%02d\n",
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
        tm->tm_hour, tm->tm_min, tm->tm_sec);

    return 0;
}

It outputs:

它输出:

2010-02-11 15:58:29

How it works is as follows.

它的工作原理如下。

  • it calls time()to get the best approximation to the current time (usually number of seconds since the epoch but that's not actually mandated by the standard).
  • it then calls localtime()to convert that to a structure which contains the individual date and time fields, among other things.
  • at that point, you can just de-reference the structure to get the fields you're interested in (tm_secin your case but I've shown a few of them).
  • 它要求time()获得当前时间的最佳近似值(通常是自纪元以来的秒数,但这实际上不是标准规定的)。
  • 然后调用localtime()将其转换为包含单个日期和时间字段等的结构。
  • 那时,您可以取消引用结构以获得您感兴趣的字段(tm_sec在您的情况下,但我已经展示了其中的一些)。

Keep in mind you can also use gmtime()instead of localtime()if you want Greenwich time, or UTC for those too young to remember :-).

请记住gmtime()localtime()如果您想要格林威治时间或 UTC,您也可以使用代替,以供那些太年轻而无法记住的人使用 :-)。

回答by CB Bailey

A more portable way to do this is to get the current time as a time_tstruct:

一种更便携的方法是将当前时间作为time_t结构体获取:

time_t mytime = time((time_t*)0);

Retrieve a struct tmfor this time_t:

struct tm为此检索一个time_t

struct tm *mytm = localtime(&mytime);

Examine the tm_secmember of mytm. Depending on your C library, there's no guarantee that the return value of time()is based on a number of seconds since the start of a minute.

检查 的tm_sec成员mytm。根据您的 C 库,不能保证 的返回值time()基于自一分钟开始以来的秒数。

回答by Michael Foukarakis

You can get the current time with gettimeofday(C11), time(Linux), or localtime_r(POSIX); depending on what calendar & epoch you're interested. You can convert it to seconds elapsed after calendar epoch, or seconds of current minute, whichever you are after:

您可以使用gettimeofday(C11)、time(Linux) 或localtime_r(POSIX)获取当前时间;取决于您感兴趣的日历和纪元。您可以将其转换为日历纪元后经过的秒数,或当前分钟的秒数,无论您是在哪个之后:

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

int main() {
    time_t current_secs = time(NULL);
    localtime_r(&current_secs, &current_time);

    char secstr[128] = {};
    struct tm current_time;
    strftime(secstr, sizeof secstr, "%S", &current_time);

    fprintf(stdout, "The second: %s\n", secstr);
    return 0;
}

回答by Richard Wicks

You want to use gettimeofday:

您想使用 gettimeofday:

man 2 gettimeofday

男人 2 gettimeofday

#include <stdio.h>
#include <sys/time.h>

int main (int argc, char **argv)
{
  int iRet;
  struct timeval tv;

  iRet = gettimeofday (&tv, NULL); // timezone structure is obsolete
  if (iRet == 0)
  {
    printf ("Seconds/USeconds since epoch: %d/%d\n",
            (int)tv.tv_sec, (int)tv.tv_usec);
    return 0;
  }
  else
  {
    perror ("gettimeofday");
  }

  return iRet;
}

This is better to use then time(0), because you get the useconds as well, atomically, which is the more common use case.

这比使用 time(0) 更好,因为您也可以原子地获得 useconds,这是更常见的用例。