Skip to content

Commit

Permalink
Cherry pick PR #1615: Add SetTimeZone API as a starboard extension (#…
Browse files Browse the repository at this point in the history
…1887)

Refer to the original PR: #1615

In order to test SbTimeZoneGetCurrent(), we need to set a time zone
first. Here we add a new time zone API as a starboard extension to set
time zone. We start with Linux implementation. Will have follow up PR to
add implementations for all first party platforms. This will help catch
the issue if a platform forget to implement SbTimeZoneGetCurrent() which
always return 0.

b/300108997

Co-authored-by: Sherry Zhou <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and sherryzy authored Nov 1, 2023
1 parent 0507187 commit 6756839
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 3 deletions.
21 changes: 21 additions & 0 deletions starboard/extension/extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "starboard/extension/memory_mapped_file.h"
#include "starboard/extension/platform_info.h"
#include "starboard/extension/platform_service.h"
#include "starboard/extension/time_zone.h"
#include "starboard/extension/updater_notification.h"
#include "starboard/extension/url_fetcher_observer.h"
#include "starboard/system.h"
Expand Down Expand Up @@ -438,5 +439,25 @@ TEST(ExtensionTest, PlatformInfo) {
<< "Extension struct should be a singleton";
}

TEST(ExtensionTest, TimeZone) {
typedef StarboardExtensionTimeZoneApi ExtensionApi;
const char* kExtensionName = kStarboardExtensionTimeZoneName;

const ExtensionApi* extension_api =
static_cast<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
if (!extension_api) {
return;
}

EXPECT_STREQ(extension_api->name, kExtensionName);
EXPECT_EQ(extension_api->version, 1u);
EXPECT_NE(extension_api->SetTimeZone, nullptr);

const ExtensionApi* second_extension_api =
static_cast<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
EXPECT_EQ(second_extension_api, extension_api)
<< "Extension struct should be a singleton";
}

} // namespace extension
} // namespace starboard
47 changes: 47 additions & 0 deletions starboard/extension/time_zone.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 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_EXTENSION_TIME_ZONE_H_
#define STARBOARD_EXTENSION_TIME_ZONE_H_

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define kStarboardExtensionTimeZoneName "dev.starboard.extension.TimeZone"

typedef struct StarboardExtensionTimeZoneApi {
// Name should be the string |kStarboardExtensionSetTimeZoneName|.
// This helps to validate that the extension API is correct.
const char* name;

// This specifies the version of the API that is implemented.
uint32_t version;

// Sets the current time zone to the specified time zone name.
// Note: This function should not be called with a NULL or empty
// string. It does not actually change the system clock, so it
// will not affect the time displayed on the system clock or
// used by other system processes.
bool (*SetTimeZone)(const char* time_zone_name);

} StarboardExtensionTimeZoneApi;

#ifdef __cplusplus
} // extern "C"
#endif

#endif // STARBOARD_EXTENSION_TIME_ZONE_H_
2 changes: 2 additions & 0 deletions starboard/linux/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ static_library("starboard_platform_sources") {
"//starboard/linux/shared/system_get_extensions.cc",
"//starboard/linux/shared/system_get_path.cc",
"//starboard/linux/shared/system_has_capability.cc",
"//starboard/linux/shared/time_zone.cc",
"//starboard/linux/shared/time_zone.h",
"//starboard/shared/alsa/alsa_audio_sink_type.cc",
"//starboard/shared/alsa/alsa_audio_sink_type.h",
"//starboard/shared/alsa/alsa_util.cc",
Expand Down
5 changes: 5 additions & 0 deletions starboard/linux/shared/system_get_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include "starboard/extension/free_space.h"
#include "starboard/extension/memory_mapped_file.h"
#include "starboard/extension/platform_service.h"
#include "starboard/extension/time_zone.h"
#include "starboard/linux/shared/soft_mic_platform_service.h"
#include "starboard/linux/shared/time_zone.h"
#include "starboard/shared/enhanced_audio/enhanced_audio.h"
#include "starboard/shared/ffmpeg/ffmpeg_demuxer.h"
#include "starboard/shared/posix/free_space.h"
Expand Down Expand Up @@ -74,5 +76,8 @@ const void* SbSystemGetExtension(const char* name) {
return use_ffmpeg_demuxer ? starboard::shared::ffmpeg::GetFFmpegDemuxerApi()
: NULL;
}
if (strcmp(name, kStarboardExtensionTimeZoneName) == 0) {
return starboard::shared::GetTimeZoneApi();
}
return NULL;
}
60 changes: 60 additions & 0 deletions starboard/linux/shared/time_zone.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 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/linux/shared/time_zone.h"

#include "starboard/extension/time_zone.h"

#include <stdlib.h>
#include <time.h>
#include <cstring>

#include "starboard/common/log.h"

namespace starboard {
namespace shared {

namespace {

// Definitions of any functions included as components in the extension
// are added here.

bool SetTimeZone(const char* time_zone_name) {
if (time_zone_name == nullptr || strlen(time_zone_name) == 0) {
SB_LOG(ERROR) << "Set time zone failed!";
SB_LOG(ERROR) << "Time zone name can't be null or empty string.";
return false;
}
if (setenv("TZ", time_zone_name, 1) != 0) {
SB_LOG(WARNING) << "Set time zone failed!";
return false;
}
tzset();
return true;
}

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

} // namespace

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

} // namespace shared
} // namespace starboard
27 changes: 27 additions & 0 deletions starboard/linux/shared/time_zone.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 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_LINUX_SHARED_TIME_ZONE_H_
#define STARBOARD_LINUX_SHARED_TIME_ZONE_H_

// Omit namespace linux due to symbol name conflict.
namespace starboard {
namespace shared {

const void* GetTimeZoneApi();

} // namespace shared
} // namespace starboard

#endif // STARBOARD_LINUX_SHARED_TIME_ZONE_H_
63 changes: 60 additions & 3 deletions starboard/nplb/time_zone_get_current_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// 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"

Expand All @@ -34,9 +36,64 @@ TEST(SbTimeZoneGetCurrentTest, IsKindOfSane) {
// ... and +24 hours from the Prime Meridian, inclusive
EXPECT_LE(zone, 24 * 60);

if (zone == 0) {
SB_LOG(WARNING) << "SbTimeZoneGetCurrent() returns 0. This is only correct "
"if the current time zone is the same as UTC";
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);
}
}

Expand Down

0 comments on commit 6756839

Please sign in to comment.