Skip to content

Commit

Permalink
[NPLB] Support SetTimeZone extension on win and Xbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Biolik committed Oct 21, 2024
1 parent 8a9e441 commit 2fb481f
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 59 deletions.
146 changes: 87 additions & 59 deletions starboard/nplb/time_zone_get_current_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,58 @@
// limitations under the License.

#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"

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

namespace starboard {
namespace nplb {
namespace {

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

std::string timeZoneName;

SbTimeZone expectedStandardValue;
SbTimeZone expectedDaylightValue;
};

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 +78,52 @@ 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);
}
#if defined(_WIN32)

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", -780, -780),
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)};

#else

std::array<TimeZoneWithExpectValue, 11> 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("Pacific/Guam", -600, -600)};

#endif

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
4 changes: 4 additions & 0 deletions starboard/nplb/time_zone_get_name_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ TEST(SbTimeZoneGetNameTest, IsKindOfSane) {
// ":Pacific/Kiritimati" is the western-most timezone at UTC+14.
}

#if defined(_WIN32)
TEST(SbTimeZoneGetNameTest, DISABLED_IsIANAFormat) {
#else
TEST(SbTimeZoneGetNameTest, IsIANAFormat) {
#endif
const char* name = SbTimeZoneGetName();
SB_LOG(INFO) << "time zone name: " << name;
char cpy[100];
Expand Down
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;
}
75 changes: 75 additions & 0 deletions starboard/shared/win32/time_zone.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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/extension/time_zone.h"
#include "starboard/time_zone.h"
#include "starboard/common/log.h"

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

namespace starboard {
namespace shared {
namespace win32 {

namespace {

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

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;
}

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
28 changes: 28 additions & 0 deletions starboard/shared/win32/time_zone.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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_SHARED_WIN32_TIME_ZONE_H_
#define STARBOARD_SHARED_WIN32_TIME_ZONE_H_

namespace starboard {
namespace shared {
namespace win32 {

const void* GetTimeZoneApi();

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

#endif // STARBOARD_SHARED_WIN32_TIME_ZONE_H_
1 change: 1 addition & 0 deletions starboard/win/win32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static_library("starboard_platform") {
"//starboard/shared/win32/system_get_used_cpu_memory.cc",
"//starboard/shared/win32/system_raise_platform_error.cc",
"//starboard/shared/win32/system_symbolize.cc",
"//starboard/shared/win32/time_zone.cc",
"//starboard/shared/win32/time_zone_get_name.cc",
"//starboard/shared/win32/window_create.cc",
"//starboard/shared/win32/window_destroy.cc",
Expand Down

0 comments on commit 2fb481f

Please sign in to comment.