Skip to content

Commit

Permalink
[MERGE #3420 @obastemur] linux: free datetime interface from tz
Browse files Browse the repository at this point in the history
Merge pull request #3420 from obastemur:time_faster

- Improves performance on XPLAT (up to 10 times)
- New OSX Date interface had dropped old Windows specific date tests. Do the same for Linux
- XPLAT DST for BC (negative years) doesn't have to match to Windows.
  • Loading branch information
obastemur committed Aug 4, 2017
2 parents 9c03405 + e2a51b9 commit ac21f6c
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 308 deletions.
3 changes: 1 addition & 2 deletions lib/Runtime/PlatformAgnostic/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(CHAKRA_PLATFORM_AGNOSTIC)
set(PL_SOURCE_FILES
Common/UnicodeText.Common.cpp
Common/HiResTimer.cpp
Common/DateTime.cpp
Linux/EventTrace.cpp
Linux/UnicodeText.ICU.cpp
Linux/NumbersUtility.cpp
Expand All @@ -14,13 +15,11 @@ set(PL_SOURCE_FILES

if(CC_TARGET_OS_ANDROID OR CC_TARGET_OS_LINUX)
set(PL_SOURCE_FILES ${PL_SOURCE_FILES}
Linux/DateTime.cpp
Linux/SystemInfo.cpp
)
elseif(CC_TARGET_OS_OSX)
set(PL_SOURCE_FILES ${PL_SOURCE_FILES}
Unix/AssemblyCommon.cpp
Unix/DateTime.cpp
Unix/SystemInfo.cpp
)
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,68 @@
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

#ifndef __APPLE__
// todo: for BSD consider moving this file into macOS folder
#include "../Linux/DateTime.cpp"
#else
#include "Common.h"
#include "ChakraPlatform.h"
#include <CoreFoundation/CFDate.h>
#include <CoreFoundation/CFTimeZone.h>

namespace PlatformAgnostic
{
namespace DateTime
{
const WCHAR *Utility::GetStandardName(size_t *nameLength, const DateTime::YMD *ymd)
int GetTZ(double tv, WCHAR* dst_name, bool* is_dst, int* offset)
{
AssertMsg(ymd != NULL, "xplat needs DateTime::YMD is defined for this call");
double tv = Js::DateUtilities::TvFromDate(ymd->year, ymd->mon, ymd->mday, ymd->time);
int64_t absoluteTime = tv / 1000;
absoluteTime -= kCFAbsoluteTimeIntervalSince1970;
struct tm tm_local, *tm_result;
time_t time_noms = (time_t) (tv / 1000 /* drop ms */);
tm_result = localtime_r(&time_noms, &tm_local);
if (!tm_result)
{
*is_dst = false;
*offset = 0;
if (dst_name != nullptr)
{
dst_name[0] = (WCHAR) 0;
}
return 0;
}

*is_dst = tm_result->tm_isdst > 0;
*offset = (int) tm_result->tm_gmtoff;

CFTimeZoneRef timeZone = CFTimeZoneCopySystem();
if (dst_name != nullptr)
{
if (!tm_result->tm_zone)
{
dst_name[0] = 0;
return 0;
}

int offset = (int)CFTimeZoneGetSecondsFromGMT(timeZone, (CFAbsoluteTime)absoluteTime);
absoluteTime -= offset;
uint32 length = 0;
for (; length < __CC_PA_TIMEZONE_ABVR_NAME_LENGTH
&& tm_result->tm_zone[length] != 0; length++)
{
dst_name[length] = (WCHAR) tm_result->tm_zone[length];
}

char tz_name[128];
CFStringRef abbr = CFTimeZoneCopyAbbreviation(timeZone, absoluteTime);
CFRelease(timeZone);
CFStringGetCString(abbr, tz_name, sizeof(tz_name), kCFStringEncodingUTF16);
wcscpy_s(data.standardName, 32, reinterpret_cast<WCHAR*>(tz_name));
data.standardNameLength = CFStringGetLength(abbr);
CFRelease(abbr);
if (length >= __CC_PA_TIMEZONE_ABVR_NAME_LENGTH)
{
length = __CC_PA_TIMEZONE_ABVR_NAME_LENGTH - 1;
}

dst_name[length] = (WCHAR)0;
return length;
}
else
{
return 0;
}
}

const WCHAR *Utility::GetStandardName(size_t *nameLength, const DateTime::YMD *ymd)
{
AssertMsg(ymd != NULL, "xplat needs DateTime::YMD is defined for this call");
double tv = Js::DateUtilities::TvFromDate(ymd->year, ymd->mon, ymd->mday, ymd->time);
bool isDST;
int mOffset;
data.standardNameLength = GetTZ(tv, data.standardName, &isDST, &mOffset);
*nameLength = data.standardNameLength;
return data.standardName;
}
Expand All @@ -46,22 +75,11 @@ namespace DateTime
return GetStandardName(nameLength, ymd);
}

static time_t IsDST(double tv, int *offset)
{
CFTimeZoneRef timeZone = CFTimeZoneCopySystem();
int64_t absoluteTime = tv / 1000;
absoluteTime -= kCFAbsoluteTimeIntervalSince1970;
*offset = (int)CFTimeZoneGetSecondsFromGMT(timeZone, (CFAbsoluteTime)absoluteTime);

time_t result = CFTimeZoneIsDaylightSavingTime(timeZone, (CFAbsoluteTime)absoluteTime);
CFRelease(timeZone);
return result;
}

static void YMDLocalToUtc(double localtv, YMD *utc)
{
int mOffset = 0;
bool isDST = IsDST(localtv, &mOffset);
bool isDST;
GetTZ(localtv, nullptr, &isDST, &mOffset);
localtv -= DateTimeTicks_PerSecond * mOffset;
Js::DateUtilities::GetYmdFromTv(localtv, utc);
}
Expand All @@ -70,7 +88,8 @@ namespace DateTime
int &bias, int &offset, bool &isDaylightSavings)
{
int mOffset = 0;
bool isDST = IsDST(utctv, &mOffset);
bool isDST;
GetTZ(utctv, nullptr, &isDST, &mOffset);
utctv += DateTimeTicks_PerSecond * mOffset;
Js::DateUtilities::GetYmdFromTv(utctv, local);
isDaylightSavings = isDST;
Expand All @@ -97,4 +116,3 @@ namespace DateTime
}
} // namespace DateTime
} // namespace PlatformAgnostic
#endif
229 changes: 0 additions & 229 deletions lib/Runtime/PlatformAgnostic/Platform/Linux/DateTime.cpp

This file was deleted.

15 changes: 0 additions & 15 deletions test/Date/DateCtr.baseline

This file was deleted.

Loading

0 comments on commit ac21f6c

Please sign in to comment.