Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FObersteiner committed Sep 20, 2024
1 parent f138241 commit ad83693
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!-- -*- coding: utf-8 -*- -->

[![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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions change.log
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/Datetime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Duration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
19 changes: 9 additions & 10 deletions lib/Timezone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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| {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_timezone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit ad83693

Please sign in to comment.