From ad83693dafbe1343336aaace274d1493ba5483b4 Mon Sep 17 00:00:00 2001 From: FObersteiner Date: Fri, 20 Sep 2024 15:57:26 +0200 Subject: [PATCH] cleanup --- README.md | 5 +++-- change.log | 1 + lib/Datetime.zig | 4 ++-- lib/Duration.zig | 2 +- lib/Timezone.zig | 19 +++++++++---------- tests/test_timezone.zig | 4 ++-- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 407a7c4..34d381d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ + [![Zig](https://img.shields.io/badge/-Zig-F7A41D?style=flat&logo=zig&logoColor=white)](https://ziglang.org/) ⚡ [![tests](https://github.com/FObersteiner/zdt/actions/workflows/zdt-tests.yml/badge.svg)](https://github.com/FObersteiner/zdt/actions/workflows/zdt-tests.yml) [![GitHub Release](https://img.shields.io/github/v/release/FObersteiner/zdt)](https://github.com/FObersteiner/zdt/releases) [![tzdata](https://img.shields.io/badge/tzdata-2024b-blue)](https://www.iana.org/time-zones) [![License: MPL 2.0](https://img.shields.io/badge/License-MPL_2.0-brightgreen.svg)](https://github.com/FObersteiner/zdt/blob/master/LICENSE) # zdt @@ -60,11 +61,11 @@ zig build examples && ./zig-out/bin/ex_datetime ## Documentation -- [see the Wiki](https://github.com/FObersteiner/zdt/wiki) +See [Wiki](https://github.com/FObersteiner/zdt/wiki) ## Development -See [changelog](https://github.com/FObersteiner/zdt/blob/master/change.log). +See [changelog](https://github.com/FObersteiner/zdt/blob/master/change.log) ## Zig version diff --git a/change.log b/change.log index 379771c..99f26cf 100644 --- a/change.log +++ b/change.log @@ -9,6 +9,7 @@ - remove method `nowLocal` from Datetime; this can be achieved with `now` plus `Timezone.tzLocal` - added method `nowUTC` to Datetime that returns UTC (aware datetime) without needing a timezone argument and returns no error - change `now` to return an error union, in case loading the timezone causes an error +- remove POSIX TZ provisions since this is currently not planned ## 2024-09-15, v0.2.3 diff --git a/lib/Datetime.zig b/lib/Datetime.zig index 8b84ec2..5c65067 100644 --- a/lib/Datetime.zig +++ b/lib/Datetime.zig @@ -130,8 +130,8 @@ pub const Fields = struct { // one of the types to be not-null: if (fields.tzinfo) |tzinfo| { if (tzinfo.tzFile == null and - tzinfo.tzOffset == null and - tzinfo.tzPosix == null) + // and tzinfo.tzPosix == null + tzinfo.tzOffset == null) { return ZdtError.AllTZRulesUndefined; } diff --git a/lib/Duration.zig b/lib/Duration.zig index 149dfa5..f93e240 100644 --- a/lib/Duration.zig +++ b/lib/Duration.zig @@ -21,7 +21,7 @@ pub fn fromTimespanMultiple(n: i128, timespan: Timespan) Duration { pub fn toTimespanMultiple(duration: Duration, timespan: Timespan) i128 { const ns: i128 = duration.asNanoseconds(); const divisor: u64 = @intFromEnum(timespan); - const result = std.math.divCeil(i128, ns, @as(i128, divisor)) catch 0; + const result = std.math.divCeil(i128, ns, @as(i128, divisor)) catch unreachable; return @intCast(result); } diff --git a/lib/Timezone.zig b/lib/Timezone.zig index e44c101..22fa680 100644 --- a/lib/Timezone.zig +++ b/lib/Timezone.zig @@ -21,7 +21,7 @@ __name_data_len: usize = 0, // time zone rule sources: tzFile: ?tzif.Tz = null, // a IANA db file with transitions list etc. -tzPosix: ?bool = null, // TODO : implement // POSIX TZ string with rule +// tzPosix: ?bool = null, tzOffset: ?UTCoffset = null, /// auto-generated string of the current eggert/tz version @@ -80,7 +80,7 @@ pub fn fromTzdata(identifier: []const u8, allocator: std.mem.Allocator) TzError! const tzif_tz = tzif.Tz.parse(allocator, in_stream.reader()) catch return TzError.TZifUnreadable; // ensure that there is a footer: requires v2+ TZif files. - _ = tzif_tz.footer orelse return TzError.BadTZifVersion; // TODO : handle posix tz + _ = tzif_tz.footer orelse return TzError.BadTZifVersion; var tz = Timezone{ .tzFile = tzif_tz }; tz.__name_data_len = if (identifier.len <= cap_name_data) identifier.len else cap_name_data; @memcpy(tz.__name_data[0..tz.__name_data_len], identifier[0..tz.__name_data_len]); @@ -100,8 +100,7 @@ pub fn fromTzfile(comptime identifier: []const u8, allocator: std.mem.Allocator) var in_stream = std.io.fixedBufferStream(data); const tzif_tz = try tzif.Tz.parse(allocator, in_stream.reader()); // ensure that there is a footer: requires v2+ TZif files. - _ = tzif_tz.footer orelse return TzError.BadTZifVersion; // TODO : handle posix tz - + _ = tzif_tz.footer orelse return TzError.BadTZifVersion; var tz = Timezone{ .tzFile = tzif_tz }; tz.__name_data_len = if (identifier.len <= cap_name_data) identifier.len else cap_name_data; @memcpy(tz.__name_data[0..tz.__name_data_len], identifier[0..tz.__name_data_len]); @@ -127,7 +126,7 @@ pub fn runtimeFromTzfile(identifier: []const u8, db_path: []const u8, allocator: return TzError.TZifUnreadable; // ensure that there is a footer: requires v2+ TZif files. - _ = tzif_tz.footer orelse return TzError.BadTZifVersion; // TODO : handle posix tz + _ = tzif_tz.footer orelse return TzError.BadTZifVersion; var tz = Timezone{ .tzFile = tzif_tz }; // default: use identifier as name @@ -179,7 +178,7 @@ pub fn deinit(tz: *Timezone) void { tz.tzFile.?.deinit(); // free memory allocated for the data from the tzfile tz.tzFile = null; } - tz.tzPosix = null; + // tz.tzPosix = null; tz.tzOffset = null; tz.__name_data = std.mem.zeroes([cap_name_data]u8); tz.__name_data_len = 0; @@ -212,7 +211,7 @@ pub fn tzLocal(allocator: std.mem.Allocator) TzError!Timezone { /// Priority for offset determination is tzfile > POSIX TZ > fixed offset. /// tzFile and tzPosix set tzOffset if possible. pub fn atUnixtime(tz: Timezone, unixtime: i64) TzError!UTCoffset { - if (tz.tzFile == null and tz.tzPosix == null and tz.tzOffset == null) { + if (tz.tzFile == null and tz.tzOffset == null) { // and tz.tzPosix == null return TzError.AllTZRulesUndefined; } @@ -238,9 +237,9 @@ pub fn atUnixtime(tz: Timezone, unixtime: i64) TzError!UTCoffset { }; } - if (tz.tzPosix != null) { - return TzError.NotImplemented; // TODO : handle posix tz - } + // if (tz.tzPosix != null) { + // return TzError.NotImplemented; + // } // if we already have an offset here but no tzFile or tzPosix, there's nothing more we can do. if (tz.tzOffset) |offset| { diff --git a/tests/test_timezone.zig b/tests/test_timezone.zig index e143b5a..1bb00e3 100644 --- a/tests/test_timezone.zig +++ b/tests/test_timezone.zig @@ -418,13 +418,13 @@ test "convert time zone" { test "make TZ with convenience func" { const off = try Tz.fromOffset(42, "hello_world"); try testing.expect(off.tzFile == null); - try testing.expect(off.tzPosix == null); + // try testing.expect(off.tzPosix == null); try testing.expect(off.tzOffset != null); var tzinfo = try Tz.fromTzfile("Asia/Kolkata", testing.allocator); defer _ = tzinfo.deinit(); try testing.expect(tzinfo.tzFile != null); - try testing.expect(tzinfo.tzPosix == null); + // try testing.expect(tzinfo.tzPosix == null); try testing.expect(tzinfo.tzOffset == null); }