Curline
Curline

Reputation: 5

Get hour/minute/second without user change the system hour

I'd like to know how can I get the current hour, minutes and second of the system using time stamp to avoid that the user change the date by himself, i'm working in c on windows.

Upvotes: 0

Views: 1576

Answers (1)

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

The problem is usually solved either by installing ntp (if you have control over the users' computers) or by using server time (in a client/server app where you don't have control over the users' computers).

That said, you can retrieve the current time of the system like this:

#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 has (at least) the following fields:

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

Upvotes: 1

Related Questions