Add utc param to get_time and get_date methods

If utc == false, we return the local time, like before.
Otherwise, we return UTC time.
utc defaults to false to not break behaviour.
This commit is contained in:
est31 2015-06-06 03:40:56 +02:00
parent 26ea12a873
commit 803069886e
11 changed files with 62 additions and 35 deletions

View file

@ -457,9 +457,9 @@ void _OS::set_icon(const Image& p_icon) {
OS::get_singleton()->set_icon(p_icon); OS::get_singleton()->set_icon(p_icon);
} }
Dictionary _OS::get_date() const { Dictionary _OS::get_date(bool utc) const {
OS::Date date = OS::get_singleton()->get_date(); OS::Date date = OS::get_singleton()->get_date(utc);
Dictionary dated; Dictionary dated;
dated["year"]=date.year; dated["year"]=date.year;
dated["month"]=date.month; dated["month"]=date.month;
@ -470,9 +470,9 @@ Dictionary _OS::get_date() const {
} }
Dictionary _OS::get_time() const { Dictionary _OS::get_time(bool utc) const {
OS::Time time = OS::get_singleton()->get_time(); OS::Time time = OS::get_singleton()->get_time(utc);
Dictionary timed; Dictionary timed;
timed["hour"]=time.hour; timed["hour"]=time.hour;
timed["minute"]=time.min; timed["minute"]=time.min;
@ -774,8 +774,8 @@ void _OS::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_cmdline_args"),&_OS::get_cmdline_args); ObjectTypeDB::bind_method(_MD("get_cmdline_args"),&_OS::get_cmdline_args);
ObjectTypeDB::bind_method(_MD("get_main_loop"),&_OS::get_main_loop); ObjectTypeDB::bind_method(_MD("get_main_loop"),&_OS::get_main_loop);
ObjectTypeDB::bind_method(_MD("get_date"),&_OS::get_date); ObjectTypeDB::bind_method(_MD("get_date","utc"),&_OS::get_date,DEFVAL(false));
ObjectTypeDB::bind_method(_MD("get_time"),&_OS::get_time); ObjectTypeDB::bind_method(_MD("get_time","utc"),&_OS::get_time,DEFVAL(false));
ObjectTypeDB::bind_method(_MD("get_unix_time"),&_OS::get_unix_time); ObjectTypeDB::bind_method(_MD("get_unix_time"),&_OS::get_unix_time);
ObjectTypeDB::bind_method(_MD("set_icon"),&_OS::set_icon); ObjectTypeDB::bind_method(_MD("set_icon"),&_OS::set_icon);

View file

@ -204,8 +204,8 @@ public:
void set_use_file_access_save_and_swap(bool p_enable); void set_use_file_access_save_and_swap(bool p_enable);
void set_icon(const Image& p_icon); void set_icon(const Image& p_icon);
Dictionary get_date() const; Dictionary get_date(bool utc) const;
Dictionary get_time() const; Dictionary get_time(bool utc) const;
uint64_t get_unix_time() const; uint64_t get_unix_time() const;
int get_static_memory_usage() const; int get_static_memory_usage() const;

View file

@ -245,8 +245,8 @@ public:
int sec; int sec;
}; };
virtual Date get_date() const=0; virtual Date get_date(bool local=false) const=0;
virtual Time get_time() const=0; virtual Time get_time(bool local=false) const=0;
virtual uint64_t get_unix_time() const; virtual uint64_t get_unix_time() const;
virtual void delay_usec(uint32_t p_usec) const=0; virtual void delay_usec(uint32_t p_usec) const=0;

View file

@ -218,10 +218,14 @@ uint64_t OS_Unix::get_unix_time() const {
}; };
OS::Date OS_Unix::get_date() const { OS::Date OS_Unix::get_date(bool utc) const {
time_t t=time(NULL); time_t t=time(NULL);
struct tm *lt=localtime(&t); struct tm *lt;
if (utc)
lt=gmtime(&t);
else
lt=localtime(&t);
Date ret; Date ret;
ret.year=1900+lt->tm_year; ret.year=1900+lt->tm_year;
ret.month=(Month)lt->tm_mon; ret.month=(Month)lt->tm_mon;
@ -231,10 +235,13 @@ OS::Date OS_Unix::get_date() const {
return ret; return ret;
} }
OS::Time OS_Unix::get_time() const { OS::Time OS_Unix::get_time(bool utc) const {
time_t t=time(NULL); time_t t=time(NULL);
struct tm *lt=localtime(&t); struct tm *lt;
if (utc)
lt=gmtime(&t);
else
lt=localtime(&t);
Time ret; Time ret;
ret.hour=lt->tm_hour; ret.hour=lt->tm_hour;
ret.min=lt->tm_min; ret.min=lt->tm_min;

View file

@ -88,8 +88,8 @@ public:
virtual String get_name(); virtual String get_name();
virtual Date get_date() const; virtual Date get_date(bool utc) const;
virtual Time get_time() const; virtual Time get_time(bool utc) const;
virtual uint64_t get_unix_time() const; virtual uint64_t get_unix_time() const;

View file

@ -240,10 +240,14 @@ MainLoop *OSNacl::get_main_loop() const {
return main_loop; return main_loop;
}; };
OS::Date OSNacl::get_date() const { OS::Date OSNacl::get_date(bool utc) const {
time_t t=time(NULL); time_t t=time(NULL);
struct tm *lt=localtime(&t); struct tm *lt;
if (utc)
lt=gmtime(&t);
else
lt=localtime(&t);
Date ret; Date ret;
ret.year=lt->tm_year; ret.year=lt->tm_year;
ret.month=(Month)lt->tm_mon; ret.month=(Month)lt->tm_mon;
@ -254,10 +258,14 @@ OS::Date OSNacl::get_date() const {
return ret; return ret;
}; };
OS::Time OSNacl::get_time() const { OS::Time OSNacl::get_time(bool utc) const {
time_t t=time(NULL); time_t t=time(NULL);
struct tm *lt=localtime(&t); struct tm *lt;
if (utc)
lt=gmtime(&t);
else
lt=localtime(&t);
Time ret; Time ret;
ret.hour=lt->tm_hour; ret.hour=lt->tm_hour;
ret.min=lt->tm_min; ret.min=lt->tm_min;

View file

@ -137,8 +137,8 @@ public:
virtual MainLoop *get_main_loop() const; virtual MainLoop *get_main_loop() const;
virtual Date get_date() const; virtual Date get_date(bool utc) const;
virtual Time get_time() const; virtual Time get_time(bool utc) const;
virtual void delay_usec(uint32_t p_usec) const; virtual void delay_usec(uint32_t p_usec) const;
virtual uint64_t get_ticks_usec() const; virtual uint64_t get_ticks_usec() const;

View file

@ -1832,7 +1832,10 @@ String OS_Windows::get_name() {
OS::Date OS_Windows::get_date() const { OS::Date OS_Windows::get_date() const {
SYSTEMTIME systemtime; SYSTEMTIME systemtime;
GetLocalTime(&systemtime); if (local)
GetSystemTime(&systemtime);
else
GetLocalTime(&systemtime);
Date date; Date date;
date.day=systemtime.wDay; date.day=systemtime.wDay;
date.month=Month(systemtime.wMonth); date.month=Month(systemtime.wMonth);
@ -1841,10 +1844,13 @@ OS::Date OS_Windows::get_date() const {
date.dst=false; date.dst=false;
return date; return date;
} }
OS::Time OS_Windows::get_time() const { OS::Time OS_Windows::get_time(bool utc) const {
SYSTEMTIME systemtime; SYSTEMTIME systemtime;
GetLocalTime(&systemtime); if (utc)
GetSystemTime(&systemtime);
else
GetLocalTime(&systemtime);
Time time; Time time;
time.hour=systemtime.wHour; time.hour=systemtime.wHour;
@ -1853,7 +1859,7 @@ OS::Time OS_Windows::get_time() const {
return time; return time;
} }
uint64_t OS_Windows::get_unix_time() const { uint64_t OS_Windows::get_unix_time(bool local) const {
FILETIME ft; FILETIME ft;
SYSTEMTIME st; SYSTEMTIME st;

View file

@ -259,8 +259,8 @@ public:
virtual String get_name(); virtual String get_name();
virtual Date get_date() const; virtual Date get_date(bool utc) const;
virtual Time get_time() const; virtual Time get_time(bool utc) const;
virtual uint64_t get_unix_time() const; virtual uint64_t get_unix_time() const;
virtual bool can_draw() const; virtual bool can_draw() const;

View file

@ -454,10 +454,13 @@ OS::Date OSWinrt::get_date() const {
date.dst=false; date.dst=false;
return date; return date;
} }
OS::Time OSWinrt::get_time() const { OS::Time OSWinrt::get_time(bool utc) const {
SYSTEMTIME systemtime; SYSTEMTIME systemtime;
GetLocalTime(&systemtime); if (utc)
GetSystemTime(&systemtime);
else
GetLocalTime(&systemtime);
Time time; Time time;
time.hour=systemtime.wHour; time.hour=systemtime.wHour;
@ -466,11 +469,14 @@ OS::Time OSWinrt::get_time() const {
return time; return time;
} }
uint64_t OSWinrt::get_unix_time() const { uint64_t OSWinrt::get_unix_time(bool utc) const {
FILETIME ft; FILETIME ft;
SYSTEMTIME st; SYSTEMTIME st;
GetSystemTime(&st); if (utc)
GetSystemTime(&systemtime);
else
GetLocalTime(&systemtime);
SystemTimeToFileTime(&st, &ft); SystemTimeToFileTime(&st, &ft);
SYSTEMTIME ep; SYSTEMTIME ep;

View file

@ -198,8 +198,8 @@ public:
virtual String get_name(); virtual String get_name();
virtual Date get_date() const; virtual Date get_date(bool utc) const;
virtual Time get_time() const; virtual Time get_time(bool utc) const;
virtual uint64_t get_unix_time() const; virtual uint64_t get_unix_time() const;
virtual bool can_draw() const; virtual bool can_draw() const;