mirror of
https://github.com/godotengine/godot.git
synced 2025-10-26 19:24:18 +00:00
Add OS.get_time_zone_info function
The returned dictionary maps "name" to the name of the current time zone, and "bias" to a bias from UTC in minutes.
This commit is contained in:
parent
803069886e
commit
c5338fd6c4
8 changed files with 119 additions and 2 deletions
|
|
@ -480,6 +480,15 @@ Dictionary _OS::get_time(bool utc) const {
|
||||||
return timed;
|
return timed;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dictionary _OS::get_time_zone_info() const {
|
||||||
|
OS::TimeZoneInfo info = OS::get_singleton()->get_time_zone_info();
|
||||||
|
Dictionary infod;
|
||||||
|
infod["bias"] = info.bias;
|
||||||
|
infod["name"] = info.name;
|
||||||
|
return infod;
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t _OS::get_unix_time() const {
|
uint64_t _OS::get_unix_time() const {
|
||||||
|
|
||||||
return OS::get_singleton()->get_unix_time();
|
return OS::get_singleton()->get_unix_time();
|
||||||
|
|
@ -776,6 +785,7 @@ void _OS::_bind_methods() {
|
||||||
|
|
||||||
ObjectTypeDB::bind_method(_MD("get_date","utc"),&_OS::get_date,DEFVAL(false));
|
ObjectTypeDB::bind_method(_MD("get_date","utc"),&_OS::get_date,DEFVAL(false));
|
||||||
ObjectTypeDB::bind_method(_MD("get_time","utc"),&_OS::get_time,DEFVAL(false));
|
ObjectTypeDB::bind_method(_MD("get_time","utc"),&_OS::get_time,DEFVAL(false));
|
||||||
|
ObjectTypeDB::bind_method(_MD("get_time_zone_info"),&_OS::get_time_zone_info);
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -206,6 +206,7 @@ public:
|
||||||
void set_icon(const Image& p_icon);
|
void set_icon(const Image& p_icon);
|
||||||
Dictionary get_date(bool utc) const;
|
Dictionary get_date(bool utc) const;
|
||||||
Dictionary get_time(bool utc) const;
|
Dictionary get_time(bool utc) const;
|
||||||
|
Dictionary get_time_zone_info() 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;
|
||||||
|
|
|
||||||
|
|
@ -245,8 +245,14 @@ public:
|
||||||
int sec;
|
int sec;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct TimeZoneInfo {
|
||||||
|
int bias;
|
||||||
|
String name;
|
||||||
|
};
|
||||||
|
|
||||||
virtual Date get_date(bool local=false) const=0;
|
virtual Date get_date(bool local=false) const=0;
|
||||||
virtual Time get_time(bool local=false) const=0;
|
virtual Time get_time(bool local=false) const=0;
|
||||||
|
virtual TimeZoneInfo get_time_zone_info() 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;
|
||||||
|
|
|
||||||
|
|
@ -246,6 +246,33 @@ OS::Time OS_Unix::get_time(bool utc) const {
|
||||||
ret.hour=lt->tm_hour;
|
ret.hour=lt->tm_hour;
|
||||||
ret.min=lt->tm_min;
|
ret.min=lt->tm_min;
|
||||||
ret.sec=lt->tm_sec;
|
ret.sec=lt->tm_sec;
|
||||||
|
get_time_zone_info();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS::TimeZoneInfo OS_Unix::get_time_zone_info() const {
|
||||||
|
time_t t = time(NULL);
|
||||||
|
struct tm *lt = localtime(&t);
|
||||||
|
char name[16];
|
||||||
|
strftime(name, 16, "%Z", lt);
|
||||||
|
name[15] = 0;
|
||||||
|
TimeZoneInfo ret;
|
||||||
|
ret.name = name;
|
||||||
|
|
||||||
|
char bias_buf[16];
|
||||||
|
strftime(bias_buf, 16, "%z", lt);
|
||||||
|
int bias;
|
||||||
|
bias_buf[15] = 0;
|
||||||
|
sscanf(bias_buf, "%d", &bias);
|
||||||
|
|
||||||
|
// convert from ISO 8601 (1 minute=1, 1 hour=100) to minutes
|
||||||
|
int hour = (int)bias / 100;
|
||||||
|
int minutes = bias % 100;
|
||||||
|
if (bias < 0)
|
||||||
|
ret.bias = hour * 60 - minutes;
|
||||||
|
else
|
||||||
|
ret.bias = hour * 60 + minutes;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,7 @@ public:
|
||||||
|
|
||||||
virtual Date get_date(bool utc) const;
|
virtual Date get_date(bool utc) const;
|
||||||
virtual Time get_time(bool utc) const;
|
virtual Time get_time(bool utc) const;
|
||||||
|
virtual TimeZoneInfo get_time_zone_info() const;
|
||||||
|
|
||||||
virtual uint64_t get_unix_time() const;
|
virtual uint64_t get_unix_time() const;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -273,6 +273,32 @@ OS::Time OSNacl::get_time(bool utc) const {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
OS::TimeZoneInfo OS_Unix::get_time_zone_info() const {
|
||||||
|
time_t t = time(NULL);
|
||||||
|
struct tm *lt = localtime(&t);
|
||||||
|
char name[16];
|
||||||
|
strftime(name, 16, "%Z", lt);
|
||||||
|
name[15] = 0;
|
||||||
|
TimeZoneInfo ret;
|
||||||
|
ret.name = name;
|
||||||
|
|
||||||
|
char bias_buf[16];
|
||||||
|
strftime(bias_buf, 16, "%z", lt);
|
||||||
|
int bias;
|
||||||
|
bias_buf[15] = 0;
|
||||||
|
sscanf(bias_buf, "%d", &bias);
|
||||||
|
|
||||||
|
// convert from ISO 8601 (1 minute=1, 1 hour=100) to minutes
|
||||||
|
int hour = (int)bias / 100;
|
||||||
|
int minutes = bias % 100;
|
||||||
|
if (bias < 0)
|
||||||
|
ret.bias = hour * 60 - minutes;
|
||||||
|
else
|
||||||
|
ret.bias = hour * 60 + minutes;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
|
||||||
void OSNacl::delay_usec(uint32_t p_usec) const {
|
void OSNacl::delay_usec(uint32_t p_usec) const {
|
||||||
|
|
||||||
//usleep(p_usec);
|
//usleep(p_usec);
|
||||||
|
|
|
||||||
|
|
@ -1858,6 +1858,36 @@ OS::Time OS_Windows::get_time(bool utc) const {
|
||||||
time.sec=systemtime.wSecond;
|
time.sec=systemtime.wSecond;
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
OS::Time OS_Windows::get_time(bool utc) const {
|
||||||
|
|
||||||
|
SYSTEMTIME systemtime;
|
||||||
|
if (utc)
|
||||||
|
GetSystemTime(&systemtime);
|
||||||
|
else
|
||||||
|
GetLocalTime(&systemtime);
|
||||||
|
|
||||||
|
Time time;
|
||||||
|
time.hour=systemtime.wHour;
|
||||||
|
time.min=systemtime.wMinute;
|
||||||
|
time.sec=systemtime.wSecond;
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS::TimeZoneInfo OS_Windows::get_time_zone_info() const {
|
||||||
|
TIME_ZONE_INFORMATION info;
|
||||||
|
bool daylight = false;
|
||||||
|
if (GetTimeZoneInformation(info) == TIME_ZONE_ID_DAYLIGHT)
|
||||||
|
daylight = true;
|
||||||
|
|
||||||
|
if (daylight) {
|
||||||
|
ret.name = info.DaylightName;
|
||||||
|
} else {
|
||||||
|
ret.name = info.StandardName;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.bias = info.Bias;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t OS_Windows::get_unix_time(bool local) const {
|
uint64_t OS_Windows::get_unix_time(bool local) const {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -469,6 +469,22 @@ OS::Time OSWinrt::get_time(bool utc) const {
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OS::TimeZoneInfo OS_Windows::get_time_zone_info() const {
|
||||||
|
TIME_ZONE_INFORMATION info;
|
||||||
|
bool daylight = false;
|
||||||
|
if (GetTimeZoneInformation(info) == TIME_ZONE_ID_DAYLIGHT)
|
||||||
|
daylight = true;
|
||||||
|
|
||||||
|
if (daylight) {
|
||||||
|
ret.name = info.DaylightName;
|
||||||
|
} else {
|
||||||
|
ret.name = info.StandardName;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.bias = info.Bias;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t OSWinrt::get_unix_time(bool utc) const {
|
uint64_t OSWinrt::get_unix_time(bool utc) const {
|
||||||
|
|
||||||
FILETIME ft;
|
FILETIME ft;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue