From bb2050fa9854064f21e7e01d7da2eb7be9582b52 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 13 Apr 2009 09:47:36 +0000 Subject: added sys::get_datetime, added win32 time and date functions --- src/sys/sys.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/sys/sys.h | 2 ++ 2 files changed, 63 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sys/sys.cc b/src/sys/sys.cc index 01c9e3b..f99d533 100644 --- a/src/sys/sys.cc +++ b/src/sys/sys.cc @@ -125,6 +125,37 @@ void signal(int signum, signalfunc handler) #endif } +/* + +POSIX: + + struct tm { + int tm_sec; // seconds + int tm_min; // minutes + int tm_hour; // hours + int tm_mday; // day of the month + int tm_mon; // month + int tm_year; // year + int tm_wday; // day of the week + int tm_yday; // day in the year + int tm_isdst; // daylight saving time + }; + +WIN32: + + typedef struct _SYSTEMTIME { + WORD wYear; + WORD wMonth; + WORD wDayOfWeek; + WORD wDay; + WORD wHour; + WORD wMinute; + WORD wSecond; + WORD wMilliseconds; + } SYSTEMTIME, *PSYSTEMTIME; + +*/ + unsigned long time() { #ifndef _WIN32 @@ -133,7 +164,10 @@ unsigned long time() ::localtime_r(&epochtime, &localtime); return ((unsigned long)(localtime.tm_sec + localtime.tm_min*60 + localtime.tm_hour*3600)); #else - return 0; + SYSTEMTIME localtime; + ::GetLocalTime(&localtime); + + return ((unsigned long)(localtime.wSecond + localtime.wMinute*60 + localtime.wHour*3600)); #endif } @@ -146,10 +180,35 @@ void sleep(float seconds) #endif } +void get_datetime(int &year, int & month, int & day, int & hours, int & minutes) +{ +#ifndef _WIN32 + struct ::tm localtime; + time_t epochtime = ::time(0); + ::localtime_r(&epochtime, &localtime); + + year = localtime.tm_year + 1900; + month = localtime.tm_mon +1; + day = localtime.tm_mday; + + hours = localtime.tm_hour; + minutes = localtime.tm_min; +#else + SYSTEMTIME localtime; + ::GetLocalTime(&localtime); + + year = localtime.wYear; + month = localtime.wMonth; + day = localtime.wDay; + + hours = localtime.wHour; + minutes = localtime.wMinute; +#endif +} + void quit(int status) { ::exit(status); } } - diff --git a/src/sys/sys.h b/src/sys/sys.h index 5e41daa..3d94bcb 100644 --- a/src/sys/sys.h +++ b/src/sys/sys.h @@ -45,6 +45,8 @@ void sleep(float seconds); /// return the current system time of day, in seconds after midnight unsigned long time(); +/// get the current system date and time +void get_datetime(int &year, int & month, int & day, int & hours, int & minutes); } #include "sys/consoleinterface.h" -- cgit v1.2.3