Skip to content

Native (iOS): Fix availableZoneIds having non-available ids #4

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

Merged
merged 1 commit into from
Apr 28, 2020
Merged
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
7 changes: 7 additions & 0 deletions core/commonTest/src/TimeZoneTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class TimeZoneTest {
assertTrue("UTC" in allTzIds)
}

@Test
fun availableZonesAreAvailable() {
for (zoneName in TimeZone.availableZoneIds) {
TimeZone.of(zoneName)
}
}

@Test
fun of() {
val tzm = TimeZone.of("Europe/Moscow")
Expand Down
52 changes: 31 additions & 21 deletions core/nativeMain/cinterop/cpp/apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@
#import <Foundation/NSDate.h>
#import <Foundation/NSCalendar.h>
#import <limits.h>
#import <set>
#import <string>
#include "helper_macros.hpp"

static NSTimeZone * zone_by_name(NSString *zone_name)
{
auto abbreviations = NSTimeZone.abbreviationDictionary;
auto true_name = [abbreviations valueForKey: zone_name];
NSString *name = zone_name;
if (true_name != nil) {
name = true_name;
}
return [NSTimeZone timeZoneWithName: name];
}

extern "C" {
#include "cdate.h"

Expand All @@ -40,52 +53,49 @@

char ** available_zone_ids()
{
std::set<std::string> ids;
auto zones = NSTimeZone.knownTimeZoneNames;
auto abbrevs = NSTimeZone.abbreviationDictionary.allKeys;
for (NSString * zone in zones) {
ids.insert(std::string([zone UTF8String]));
}
auto abbrevs = NSTimeZone.abbreviationDictionary;
for (NSString * key in abbrevs) {
if (ids.count(std::string([abbrevs[key] UTF8String]))) {
ids.insert(std::string([key UTF8String]));
}
}
char ** zones_copy = (char **)malloc(
sizeof(char *) * (zones.count + abbrevs.count + 1));
sizeof(char *) * (ids.size() + 1));
if (zones_copy == nullptr) {
return nullptr;
}
zones_copy[zones.count + abbrevs.count] = nullptr;
unsigned long idx = 0;
for (unsigned long i = 0; i < zones.count; ++i) {
idx = i;
CFIndex bufferSize = zones[i].length + 1;
char * buffer = (char *)malloc(bufferSize);
PUSH_BACK_OR_RETURN(zones_copy, idx, buffer);
strncpy(buffer, zones[i].UTF8String, bufferSize);
}
for (unsigned long i = 0; i < abbrevs.count; ++i) {
idx = zones.count + i;
CFIndex bufferSize = abbrevs[i].length + 1;
char * buffer = (char *)malloc(bufferSize);
PUSH_BACK_OR_RETURN(zones_copy, idx, buffer);
strncpy(buffer, abbrevs[i].UTF8String, bufferSize);
zones_copy[ids.size()] = nullptr;
unsigned long i = 0;
for (auto it = ids.begin(); it != ids.end(); ++i, ++it) {
PUSH_BACK_OR_RETURN(zones_copy, i, strdup(it->c_str()));
}
return zones_copy;
}

int offset_at_instant(const char *zone_name, int64_t epoch_sec)
{
auto zone_name_nsstring = [NSString stringWithUTF8String: zone_name];
auto zone = [NSTimeZone timeZoneWithName: zone_name_nsstring];
auto zone = zone_by_name(zone_name_nsstring);
auto date = [NSDate dateWithTimeIntervalSince1970: epoch_sec];
return (int32_t)[zone secondsFromGMTForDate: date];
}

bool is_known_timezone(const char *zone_name) {
auto zone_name_nsstring = [NSString stringWithUTF8String: zone_name];
auto zone = [NSTimeZone timeZoneWithName: zone_name_nsstring];
return (zone != nil);
return (zone_by_name(zone_name_nsstring) != nil);
}

int offset_at_datetime(const char *zone_name, int64_t epoch_sec, int *offset) {
*offset = INT_MAX;
// timezone name
auto zone_name_nsstring = [NSString stringWithUTF8String: zone_name];
// timezone
auto zone = [NSTimeZone timeZoneWithName: zone_name_nsstring];
auto zone = zone_by_name(zone_name_nsstring);
if (zone == nil) { return 0; }
/* a date in an unspecified timezone, defined by the number of seconds since
the start of the epoch in *that* unspecified timezone */
Expand Down
1 change: 0 additions & 1 deletion core/nativeMain/cinterop/cpp/windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ char * get_system_timezone()
char ** available_zone_ids()
{
std::set<std::string> known_native_names, known_ids;
known_ids.insert("UTC");
DYNAMIC_TIME_ZONE_INFORMATION dtzi{};
for (DWORD dwResult = 0, i = 0; dwResult != ERROR_NO_MORE_ITEMS; ++i) {
dwResult = EnumDynamicTimeZoneInformation(i, &dtzi);
Expand Down
2 changes: 1 addition & 1 deletion core/nativeMain/src/TimeZone.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public actual open class TimeZone internal constructor(actual val id: String) {

actual val availableZoneIds: Set<String>
get() {
val set = mutableSetOf<String>()
val set = mutableSetOf<String>("UTC")
val zones = available_zone_ids()
?: throw RuntimeException("Failed to get the list of available timezones")
var ptr = zones
Expand Down