Skip to content
Merged
Changes from 1 commit
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
54 changes: 23 additions & 31 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -40493,38 +40493,31 @@ static const JSCFunctionListEntry js_math_obj[] = {

/* OS dependent. d = argv[0] is in ms from 1970. Return the difference
between UTC time and local time 'd' in minutes */
static int getTimezoneOffset(int64_t time) {
static int getTimezoneOffset() {
#if defined(_WIN32)
/* XXX: TODO */
return 0;
#else
time_t ti;
struct tm tm;

time /= 1000; /* convert to seconds */
if (sizeof(time_t) == 4) {
/* on 32-bit systems, we need to clamp the time value to the
range of `time_t`. This is better than truncating values to
32 bits and hopefully provides the same result as 64-bit
implementation of localtime_r.
*/
if ((time_t)-1 < 0) {
if (time < INT32_MIN) {
time = INT32_MIN;
} else if (time > INT32_MAX) {
time = INT32_MAX;
}
} else {
if (time < 0) {
time = 0;
} else if (time > UINT32_MAX) {
time = UINT32_MAX;
}
}
time_t unixTime = time(NULL);

struct tm* localTime = localtime(&unixTime);
time_t localTimeNum = mktime(localTime);

struct tm *gmtTime = gmtime(&unixTime);
time_t gmtTimeNum = mktime(gmtTime);

// Calculate the time zone offset in minutes
int timeZoneOffset = (int)difftime(localTimeNum, gmtTimeNum) / 60;

// In JavaScript, it's the opposite
timeZoneOffset = -timeZoneOffset;

// Take off another hour if daylight savings it active
if (localTime->tm_isdst) {
timeZoneOffset = timeZoneOffset - 60;
}
ti = time;
localtime_r(&ti, &tm);
return -tm.tm_gmtoff / 60;

return timeZoneOffset;
#endif
}

Expand Down Expand Up @@ -46531,7 +46524,7 @@ static __exception int get_date_fields(JSContext *ctx, JSValue obj,
} else {
d = dval;
if (is_local) {
tz = -getTimezoneOffset(d);
tz = -getTimezoneOffset();
d += tz * 60000;
}
}
Expand Down Expand Up @@ -46600,7 +46593,7 @@ static double set_date_fields(double fields[], int is_local) {
fields[5] * 1000 + fields[6];
d = days * 86400000 + h;
if (is_local)
d += getTimezoneOffset(d) * 60000;
d += getTimezoneOffset() * 60000;
return time_clip(d);
}

Expand Down Expand Up @@ -47203,15 +47196,14 @@ static JSValue js_date_Symbol_toPrimitive(JSContext *ctx, JSValue this_val,
static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
// getTimezoneOffset()
double v;

if (JS_ThisTimeValue(ctx, &v, this_val))
return JS_EXCEPTION;
if (isnan(v))
return JS_NAN;
else
return JS_NewInt64(ctx, getTimezoneOffset((int64_t)trunc(v)));
return JS_NewInt64(ctx, getTimezoneOffset());
}

static JSValue js_date_getTime(JSContext *ctx, JSValue this_val,
Expand Down