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

[NPLB] Support SetTimeZone extension on win #4288

Open
wants to merge 3 commits into
base: 25.lts.1+
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ target(gtest_target_type, "nplb") {
[ "//starboard/nplb/compiler_compliance/cpp_compiler_version_check.cc" ]
}

if (is_win) {
sources +=
[ "//starboard/nplb/time_zone_with_expect_value_win.cc" ]
} else {
sources +=
[ "//starboard/nplb/time_zone_with_expect_value.cc" ]
}

configs += [ "//starboard/build/config:starboard_implementation" ]
if (sb_enable_cast_codec_tests) {
configs += [ ":cast_codec_tests" ]
Expand Down
101 changes: 42 additions & 59 deletions starboard/nplb/time_zone_get_current_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/nplb/time_zone_with_expect_value.h"

#include <gmock/gmock.h>
#include <array>

#include "starboard/extension/time_zone.h"
#include "starboard/nplb/time_constants.h"
#include "starboard/system.h"
#include "starboard/time_zone.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand All @@ -21,6 +27,32 @@ namespace starboard {
namespace nplb {
namespace {

class SbTimeZoneGetCurrentSetTimeZoneTest
: public testing::Test,
public testing::WithParamInterface<TimeZoneWithExpectValue> {
protected:
void SetUp() override {
time_zone_extension = static_cast<const StarboardExtensionTimeZoneApi*>(
SbSystemGetExtension(kStarboardExtensionTimeZoneName));
if (!time_zone_extension) {
GTEST_SKIP()
<< "Skipping test for platform with missing Time Zone Extension.";
}
ASSERT_STREQ(time_zone_extension->name, kStarboardExtensionTimeZoneName);
ASSERT_EQ(time_zone_extension->version, 1u);
savedTimeZone = SbTimeZoneGetName();
}

void TearDown() override {
if (time_zone_extension) {
time_zone_extension->SetTimeZone(savedTimeZone.c_str());
}
}

std::string savedTimeZone;
const StarboardExtensionTimeZoneApi* time_zone_extension;
};

TEST(SbTimeZoneGetCurrentTest, IsKindOfSane) {
SbTimeZone zone = SbTimeZoneGetCurrent();

Expand All @@ -34,68 +66,19 @@ TEST(SbTimeZoneGetCurrentTest, IsKindOfSane) {

// ... and +24 hours from the Prime Meridian, inclusive
EXPECT_LE(zone, 24 * 60);
}

static auto const* time_zone_extension =
static_cast<const StarboardExtensionTimeZoneApi*>(
SbSystemGetExtension(kStarboardExtensionTimeZoneName));
if (time_zone_extension) {
ASSERT_STREQ(time_zone_extension->name, kStarboardExtensionTimeZoneName);
ASSERT_EQ(time_zone_extension->version, 1u);
time_zone_extension->SetTimeZone("UTC");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 0);

// Atlantic time zone, UTC−04:00
time_zone_extension->SetTimeZone("America/Puerto_Rico");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 240);

// Eastern time zone, UTC−05:00
time_zone_extension->SetTimeZone("America/New_York");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 300);

time_zone_extension->SetTimeZone("US/Eastern");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 300);

// Central time zone, UTC−06:00
time_zone_extension->SetTimeZone("America/Chicago");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 360);

// Mountain time zone, UTC−07:00
time_zone_extension->SetTimeZone("US/Mountain");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 420);

// Pacific time zone, UTC-08:00
time_zone_extension->SetTimeZone("US/Pacific");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 480);

// Alaska time zone, UTC-09:00
time_zone_extension->SetTimeZone("US/Alaska");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 540);

// Hawaii-Aleutian time zone, UTC-10:00
time_zone_extension->SetTimeZone("Pacific/Honolulu");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 600);

// American Samoa time zone, UTC-11:00
time_zone_extension->SetTimeZone("US/Samoa");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, 660);

// American Samoa time zone, UTC+10:00
time_zone_extension->SetTimeZone("Pacific/Guam");
zone = SbTimeZoneGetCurrent();
EXPECT_EQ(zone, -600);
}
TEST_P(SbTimeZoneGetCurrentSetTimeZoneTest, IsKindOfSane) {
EXPECT_TRUE(time_zone_extension->SetTimeZone(GetParam().timeZoneName.c_str()));
auto zone = SbTimeZoneGetCurrent();
EXPECT_THAT(zone, ::testing::AnyOf(GetParam().expectedStandardValue,
GetParam().expectedDaylightValue));
}

INSTANTIATE_TEST_SUITE_P(SbTimeZoneGetCurrentSetTimeZoneTest,
SbTimeZoneGetCurrentSetTimeZoneTest,
::testing::ValuesIn(timeZonesWithExpectedTimeValues));

} // namespace
} // namespace nplb
} // namespace starboard
33 changes: 33 additions & 0 deletions starboard/nplb/time_zone_with_expect_value.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/nplb/time_zone_with_expect_value.h"

#if !defined(_WIN32)

const std::array<TimeZoneWithExpectValue, 12> timeZonesWithExpectedTimeValues{
TimeZoneWithExpectValue("UTC", 0, 0),
TimeZoneWithExpectValue("America/Puerto_Rico", 240, 240),
TimeZoneWithExpectValue("America/New_York", 300, 300),
TimeZoneWithExpectValue("US/Eastern", 300, 300),
TimeZoneWithExpectValue("America/Chicago", 360, 360),
TimeZoneWithExpectValue("US/Mountain", 420, 420),
TimeZoneWithExpectValue("US/Pacific", 480, 480),
TimeZoneWithExpectValue("US/Alaska", 540, 540),
TimeZoneWithExpectValue("Pacific/Honolulu", 600, 600),
TimeZoneWithExpectValue("US/Samoa", 660, 660),
TimeZoneWithExpectValue("Australia/South", -570, -570),
TimeZoneWithExpectValue("Pacific/Guam", -600, -600)};

#endif
40 changes: 40 additions & 0 deletions starboard/nplb/time_zone_with_expect_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STARBOARD_NPLB_TIME_ZONE_WITH_EXPECT_VALUE_H_
#define STARBOARD_NPLB_TIME_ZONE_WITH_EXPECT_VALUE_H_

#include "starboard/time_zone.h"

#include <gmock/gmock.h>
#include <array>
#include <string>

struct TimeZoneWithExpectValue {
TimeZoneWithExpectValue(std::string timeZoneName_,
SbTimeZone expectedStandardValue_,
SbTimeZone expectedDaylightValue_)
: timeZoneName{timeZoneName_},
expectedStandardValue{expectedStandardValue_},
expectedDaylightValue{expectedDaylightValue_} {}

std::string timeZoneName;

SbTimeZone expectedStandardValue;
SbTimeZone expectedDaylightValue;
};

extern const std::array<TimeZoneWithExpectValue, 12> timeZonesWithExpectedTimeValues;

#endif // STARBOARD_NPLB_TIME_ZONE_WITH_EXPECT_VALUE_H_
33 changes: 33 additions & 0 deletions starboard/nplb/time_zone_with_expect_value_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/nplb/time_zone_with_expect_value.h"

#if defined(_WIN32)

const std::array<TimeZoneWithExpectValue, 12> timeZonesWithExpectedTimeValues{
TimeZoneWithExpectValue("UTC", 0, 0),
TimeZoneWithExpectValue("Atlantic Standard Time", 240, 180),
TimeZoneWithExpectValue("Eastern Standard Time", 300, 240),
TimeZoneWithExpectValue("Central Standard Time", 360, 300),
TimeZoneWithExpectValue("Mountain Standard Time", 420, 360),
TimeZoneWithExpectValue("Pacific Standard Time", 480, 420),
TimeZoneWithExpectValue("Yukon Standard Time", 420, 420),
TimeZoneWithExpectValue("Samoa Standard Time", -840, -840),
TimeZoneWithExpectValue("China Standard Time", -480, -480),
TimeZoneWithExpectValue("Central European Standard Time", -60, -120),
TimeZoneWithExpectValue("Omsk Standard Time", -360, -360),
TimeZoneWithExpectValue("Cen. Australia Standard Time", -570, -630)};

#endif
5 changes: 5 additions & 0 deletions starboard/shared/win32/system_get_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#include "starboard/common/string.h"
#include "starboard/extension/configuration.h"
#include "starboard/extension/graphics.h"
#include "starboard/extension/time_zone.h"
#include "starboard/shared/win32/configuration.h"
#include "starboard/shared/win32/graphics.h"
#include "starboard/shared/win32/time_zone.h"

const void* SbSystemGetExtension(const char* name) {
if (strcmp(name, kCobaltExtensionGraphicsName) == 0) {
Expand All @@ -27,5 +29,8 @@ const void* SbSystemGetExtension(const char* name) {
if (strcmp(name, kCobaltExtensionConfigurationName) == 0) {
return starboard::shared::win32::GetConfigurationApi();
}
if (strcmp(name, kStarboardExtensionTimeZoneName) == 0) {
return starboard::shared::win32::GetTimeZoneApi();
}
return NULL;
}
119 changes: 119 additions & 0 deletions starboard/shared/win32/time_zone.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/common/log.h"
#include "starboard/extension/time_zone.h"
#include "starboard/time_zone.h"

#include <Windows.h>
#include <string>

namespace starboard {
namespace shared {
namespace win32 {

namespace {

bool SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, bool bEnablePrivilege) {
TOKEN_PRIVILEGES tp;
LUID luid;

if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) {
SB_LOG(ERROR) << "LookupPrivilegeValue error: " << GetLastError();
return false;
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0;

if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
SB_LOG(ERROR) << "AdjustTokenPrivileges error: " << GetLastError();
return false;
}

if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
SB_LOG(ERROR) << "The token does not have the specified privilege.";
return false;
}

return true;
}

bool SetTimeZone(const char* time_zone_name) {
std::string tzName(time_zone_name);
std::wstring windowsTzName(tzName.begin(), tzName.end());

HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
SB_LOG(ERROR) << "OpenProcessToken error: " << GetLastError();
return false;
}

if (!SetPrivilege(hToken, SE_TIME_ZONE_NAME, true)) {
return false;
}

DYNAMIC_TIME_ZONE_INFORMATION dtzi{0};
TIME_ZONE_INFORMATION tzi{0};
int index = 0;
bool found = false;

while (EnumDynamicTimeZoneInformation(index, &dtzi) == ERROR_SUCCESS) {
if (windowsTzName == dtzi.TimeZoneKeyName ||
windowsTzName == dtzi.StandardName ||
windowsTzName == dtzi.DaylightName) {
found = true;
break;
}
index++;
}
if (!found) {
SB_LOG(ERROR) << "Time zone: " << tzName.c_str() << "not found.";
return false;
}
auto result = SetDynamicTimeZoneInformation(&dtzi);
if (result == 0) {
DWORD error = GetLastError();
SB_LOG(ERROR) << "SetDynamicTimeZoneInformation failed for time zone: "
<< tzName.c_str() << " return code: " << result
<< " last error: " << error;
return false;
}

if (!SetPrivilege(hToken, SE_TIME_ZONE_NAME, false)) {
return false;
}

CloseHandle(hToken);

return true;
}

const StarboardExtensionTimeZoneApi kTimeZoneApi = {
kStarboardExtensionTimeZoneName,
1, // API version that's implemented.
&SetTimeZone,
};

} // namespace
const void* GetTimeZoneApi() {
return &kTimeZoneApi;
}

} // namespace win32
} // namespace shared
} // namespace starboard
Loading
Loading