我想知道如何使用时间戳获取系统当前的小时、分钟和秒,以避免用户自己更改日期,我在 Windows 上使用 c 工作。
最佳答案
该问题通常可以通过安装 ntp(如果您可以控制用户的计算机)或使用服务器时间(在您无法控制用户的计算机的客户端/服务器应用程序中)来解决。
也就是说,您可以像这样检索系统的当前时间:
#include <time.h>
int main(int argc, char **argv) {
struct tm localTime = localtime(time(null)); // Get system time using system time zone
struct tm gmTime = gmtime(time(null)); // Get system Greenwich mean time.
}
struct tm
有(至少)以下字段:
int tm_sec seconds [0,61]
int tm_min minutes [0,59]
int tm_hour hour [0,23]
int tm_mday day of month [1,31]
int tm_mon month of year [0,11]
int tm_year years since 1900
int tm_wday day of week [0,6] (Sunday = 0)
int tm_yday day of year [0,365]
int tm_isdst daylight savings flag
关于c - 获取小时/分钟/秒,无需用户更改系统小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259903/