Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulate positive dst for timezones like Dublin #29

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,73 @@ void shim::add_math_shimmed_symbols(std::vector<shim::shimmed_symbol> &list) {
});
}

static bool hasNegativeDST() {
tm t;
time_t cur = time(nullptr);
localtime_r(&cur, &t);
time_t before = mktime(&t);
tm a = t;
t.tm_mon += 6;
time_t after = mktime(&t);
tm b = t;
if(a.tm_isdst == !b.tm_isdst) {
auto n0 = a.tm_isdst ? b.tm_gmtoff : a.tm_gmtoff;
auto n1 = b.tm_isdst ? b.tm_gmtoff : a.tm_gmtoff;
return n0 > n1;
}
return false;
}

void shim::add_time_shimmed_symbols(std::vector<shim::shimmed_symbol> &list) {
// fixup timezones like Europe/Dublin by reversing the tm_isdst field of the tm structure
// Microsoft Xbox Authentication Library for android doesn't work with negative DST behavior
if(hasNegativeDST()) {
list.insert(list.end(), {
/* sys/time.h */
{"gettimeofday", gettimeofday},

/* time.h */
{"clock", ::clock},
{"time", ::time},
{"difftime", ::difftime},
{"mktime", &detail::arg_rewrite_helper<decltype(::mktime)>::rewrite<::mktime>},
{"strftime", &detail::arg_rewrite_helper<decltype(::strftime)>::rewrite<::strftime>},
{"strptime", &detail::arg_rewrite_helper<decltype(::strptime)>::rewrite<::strptime>},
{"strftime_l", &detail::arg_rewrite_helper<decltype(::strftime_l)>::rewrite<::strftime_l>},
{"strptime_l", &detail::arg_rewrite_helper<decltype(::strptime_l)>::rewrite<::strptime_l>},
{"gmtime", ::gmtime},
{"gmtime_r", ::gmtime_r},
{"localtime", +[](const time_t* time) {
auto loc = ::localtime(time);
if(!loc) {
return loc;
}
switch(loc->tm_isdst) {
case 1:
loc->tm_isdst = 0;
break;
case 0:
loc->tm_isdst = 1;
break;
default:
break;
}
return loc;
}},
{"localtime_r", &detail::arg_rewrite_helper<decltype(::localtime_r)>::rewrite<::localtime_r>},
{"asctime", &detail::arg_rewrite_helper<decltype(::asctime)>::rewrite<::asctime>},
{"ctime", ::ctime},
{"asctime_r", &detail::arg_rewrite_helper<decltype(::asctime_r)>::rewrite<::asctime_r>},
{"ctime_r", ::ctime_r},
{"tzname", ::tzname},
{"tzset", ::tzset},
{"daylight", &::daylight},
{"timezone", &::timezone},
{"nanosleep", ::nanosleep},
{"clock_gettime", clock_gettime},
});
return;
}
list.insert(list.end(), {
/* sys/time.h */
{"gettimeofday", gettimeofday},
Expand Down
53 changes: 53 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "argrewrite.h"
#include "mallinfo.h"
#include <stdarg.h>
#include <ctime>

namespace shim {

Expand Down Expand Up @@ -176,4 +177,56 @@ namespace shim {

void add_socket_shimmed_symbols(std::vector<shim::shimmed_symbol> &list);

namespace detail {

template <class T>
struct tm_detail {
tm loc;
using source = T;

source before(source src) {
if(src) {
loc = *src;
switch(loc.tm_isdst) {
case 1:
loc.tm_isdst = 0;
break;
case 0:
loc.tm_isdst = 1;
break;
default:
break;
}
return &loc;
}
return nullptr;
}
void after(const tm* src) {

}

void after(tm* src) {
if(src) {
switch(loc.tm_isdst) {
case 1:
loc.tm_isdst = 0;
break;
case 0:
loc.tm_isdst = 1;
break;
default:
break;
}
*src = loc;
}
}
};

template <>
struct arg_rewrite<tm *> : tm_detail<tm*> {};
template <>
struct arg_rewrite<const tm *> : tm_detail<const tm*> {};

}

}
Loading