自1970年1月1日以来,我如何用c语言获得毫秒数的UTCTime

时间:2022-09-06 08:11:41

Is there any way to get milliseconds and its fraction part from 1970 using time.h in c language?

是否有办法得到毫秒和1970年使用时间的分数部分。h c语言吗?

6 个解决方案

#1


34  

This works on Ubuntu Linux:

这适用于Ubuntu Linux:

struct timeval tv;

gettimeofday(&tv, NULL);

unsigned long long millisecondsSinceEpoch =
    (unsigned long long)(tv.tv_sec) * 1000 +
    (unsigned long long)(tv.tv_usec) / 1000;

printf("%llu\n", millisecondsSinceEpoch);

At the time of this writing, the printf() above is giving me 1338850197035. You can do a sanity check at the TimestampConvert.com website where you can enter the value to get back the equivalent human-readable time (albeit without millisecond precision).

在撰写本文时,上面的printf()给出的是1338850197035。您可以在TimestampConvert.com网站上做一个完整的检查,在那里您可以输入值,以获得等效的人类可读时间(尽管没有毫秒级的精度)。

#2


9  

If you want millisecond resolution, you can use gettimeofday() in Posix. For a Windows implementation see gettimeofday function for windows.

如果想要毫秒级的分辨率,可以在Posix中使用gettimeofday()。有关Windows实现,请参阅Windows的gettimeofday函数。

#include <sys/time.h>

...

struct timeval tp;
gettimeofday(&tp);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;

#3


6  

It's not standard C, but gettimeofday() is present in both SysV and BSD derived systems, and is in POSIX. It returns the time since the epoch in a struct timeval:

它不是标准的C,但是gettimeofday()在SysV和BSD派生系统中都存在,并且在POSIX中。它返回一个结构时间中自纪元以来的时间:

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

#4


4  

For Unix and Linux you could use gettimeofday.

对于Unix和Linux,您可以使用gettimeofday。

For Win32 you could use GetSystemTimeAsFileTime and then convert it to time_t + milliseconds.

对于Win32,您可以使用GetSystemTimeAsFileTime,然后将其转换为time_t +毫秒。

#5


2  

    // the system time
    SYSTEMTIME systemTime;
    GetSystemTime( &systemTime );

    // the current file time
    FILETIME fileTime;
    SystemTimeToFileTime( &systemTime, &fileTime );

    // filetime in 100 nanosecond resolution
    ULONGLONG fileTimeNano100;
    fileTimeNano100 = (((ULONGLONG) fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;

    //to milliseconds and unix windows epoche offset removed
    ULONGLONG posixTime = fileTimeNano100/10000 - 11644473600000;
    return posixTime;

#6


1  

Unix time or Posix time is the time in seconds since the epoch you mentioned.

Unix时间或Posix时间是您提到的那个时代以来的时间。

bzabhi's answer is correct: you simply multiply the Unix timestamp by 1000 to get milliseconds.

bzabhi的答案是正确的:只需将Unix时间戳乘以1000就可以得到毫秒。

Be aware that all millisecond values returned by relying on the Unix timestamp will be multiples of 1000 (like 12345678000). The resolution is still only 1 second.

请注意,依赖于Unix时间戳返回的所有毫秒值将是1000的倍数(如12345678000)。决议仍然只有一秒。

You can't get the fraction part

你不能得到分数部分。

The comment from Pavel is correct also. The Unix timestamp does not take into account leap seconds. This makes it even less wise to rely on a conversion to milliseconds.

帕维尔的评论也是正确的。Unix时间戳不考虑闰秒。这使得依赖于对毫秒的转换变得更加不明智。

#1


34  

This works on Ubuntu Linux:

这适用于Ubuntu Linux:

struct timeval tv;

gettimeofday(&tv, NULL);

unsigned long long millisecondsSinceEpoch =
    (unsigned long long)(tv.tv_sec) * 1000 +
    (unsigned long long)(tv.tv_usec) / 1000;

printf("%llu\n", millisecondsSinceEpoch);

At the time of this writing, the printf() above is giving me 1338850197035. You can do a sanity check at the TimestampConvert.com website where you can enter the value to get back the equivalent human-readable time (albeit without millisecond precision).

在撰写本文时,上面的printf()给出的是1338850197035。您可以在TimestampConvert.com网站上做一个完整的检查,在那里您可以输入值,以获得等效的人类可读时间(尽管没有毫秒级的精度)。

#2


9  

If you want millisecond resolution, you can use gettimeofday() in Posix. For a Windows implementation see gettimeofday function for windows.

如果想要毫秒级的分辨率,可以在Posix中使用gettimeofday()。有关Windows实现,请参阅Windows的gettimeofday函数。

#include <sys/time.h>

...

struct timeval tp;
gettimeofday(&tp);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;

#3


6  

It's not standard C, but gettimeofday() is present in both SysV and BSD derived systems, and is in POSIX. It returns the time since the epoch in a struct timeval:

它不是标准的C,但是gettimeofday()在SysV和BSD派生系统中都存在,并且在POSIX中。它返回一个结构时间中自纪元以来的时间:

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

#4


4  

For Unix and Linux you could use gettimeofday.

对于Unix和Linux,您可以使用gettimeofday。

For Win32 you could use GetSystemTimeAsFileTime and then convert it to time_t + milliseconds.

对于Win32,您可以使用GetSystemTimeAsFileTime,然后将其转换为time_t +毫秒。

#5


2  

    // the system time
    SYSTEMTIME systemTime;
    GetSystemTime( &systemTime );

    // the current file time
    FILETIME fileTime;
    SystemTimeToFileTime( &systemTime, &fileTime );

    // filetime in 100 nanosecond resolution
    ULONGLONG fileTimeNano100;
    fileTimeNano100 = (((ULONGLONG) fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;

    //to milliseconds and unix windows epoche offset removed
    ULONGLONG posixTime = fileTimeNano100/10000 - 11644473600000;
    return posixTime;

#6


1  

Unix time or Posix time is the time in seconds since the epoch you mentioned.

Unix时间或Posix时间是您提到的那个时代以来的时间。

bzabhi's answer is correct: you simply multiply the Unix timestamp by 1000 to get milliseconds.

bzabhi的答案是正确的:只需将Unix时间戳乘以1000就可以得到毫秒。

Be aware that all millisecond values returned by relying on the Unix timestamp will be multiples of 1000 (like 12345678000). The resolution is still only 1 second.

请注意,依赖于Unix时间戳返回的所有毫秒值将是1000的倍数(如12345678000)。决议仍然只有一秒。

You can't get the fraction part

你不能得到分数部分。

The comment from Pavel is correct also. The Unix timestamp does not take into account leap seconds. This makes it even less wise to rely on a conversion to milliseconds.

帕维尔的评论也是正确的。Unix时间戳不考虑闰秒。这使得依赖于对毫秒的转换变得更加不明智。