Skip to content

Mac and iOS timezone support #67

Description

@loftafi

The load timezone function currently has specific support for windows, then does a generic search for timezone data files:

pub fn loadTimeZone(
    alloc: std.mem.Allocator,
    io: std.Io,
    loc: Location,
    env: EnvConfig,
) !TimeZone {
    switch (builtin.os.tag) {
        .windows => {
            const tz = try timezone.Windows.loadFromName(alloc, loc.asText());
            return .{ .windows = tz };
        },
        else => {},
    }

Using the project zig-objc it is possible to have the code optionally read the users current timezone, which means the local timezone can be successfully read on iOS (iPhone, iPad, etc...):

pub fn appleTimezone(gpa: Allocator, io: std.Io) ?zeit.TimeZone {
    const builtin = @import("builtin");
    if (builtin.target.os.tag != .ios and builtin.target.os.tag != .macos)
        return null;
    const objc = @import("objc");
    const NSTimeZone = objc.getClass("NSTimeZone").?;
    const tz = NSTimeZone.msgSend(objc.Object, "localTimeZone", .{});
    const name = tz.msgSend(objc.Object, "name", .{});
    const c_str = name.getProperty(?[*:0]const u8, "UTF8String") orelse return null;
    const location = std.mem.sliceTo(c_str, 0);
    if (std.meta.stringToEnum(zeit.Location, location)) |loc| {
        std.log.info("zeit has enum {t} for apple location {s}", .{ loc, location });
        return zeit.loadTimeZone(gpa, io, loc, .{}) catch return null;
    }
    std.log.err("zeit has no enum for apple location {s}", .{location});
    return null;
}

The downside of doing this, is that it will add a requirement to link to some Mac dependencies in the build file.

I wonder if a patch for this would be a good idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions