Skip to content

Commit

Permalink
revise ergonomics / stringIO
Browse files Browse the repository at this point in the history
  • Loading branch information
FObersteiner committed Aug 28, 2024
1 parent e8453b7 commit 8dace85
Show file tree
Hide file tree
Showing 29 changed files with 150 additions and 138 deletions.
20 changes: 13 additions & 7 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) [![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 All @@ -7,15 +8,19 @@
- [on Codeberg](https://codeberg.org/FObersteiner/zdt)
- [on github](https://github.com/FObersteiner/zdt)

Demo:
[Demo](https://github.com/FObersteiner/zdt/blob/master/examples/ex_demo.zig):

```zig
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var tz_LA = try zdt.Timezone.fromTzfile("America/Los_Angeles", allocator);
defer tz_LA.deinit();
var tz_Paris = try zdt.Timezone.fromTzfile("Europe/Paris", allocator);
defer tz_Paris.deinit();
const a_datetime = try zdt.stringIO.parseISO8601("2022-03-07");
const a_datetime = try zdt.parseISO8601("2022-03-07");
const this_time_LA = try a_datetime.tzLocalize(tz_LA);
const this_time_Paris = try this_time_LA.tzConvert(tz_Paris);
Expand Down Expand Up @@ -55,17 +60,18 @@ See [changelog](https://codeberg.org/FObersteiner/zdt/src/branch/main/docs/chang

## Zig version

This library is developed with Zig `0.14.0-dev`, might not compile with older versions. As of 2024-08-07, Zig-0.13 stable or higher should work.
This library is developed with Zig `0.14.0-dev`, might not compile with older versions. As of 2024-08-28, Zig-0.13 stable or higher should work.

## Dependencies
## IANA timezone database version

- none
- `zdt v0.2.1`: `2024a`
- `zdt v0.2.0`: `2024a`

## Time zone database
## Dependencies, Development and Time zone database

`zdt` comes with [eggert/tz](https://github.com/eggert/tz). The database is compiled and shipped with `zdt` (as-is; not tar-balled or compressed). If you wish to use your own version of the [IANA time zone db](https://www.iana.org/time-zones), you can set a path to it using the `-Dprefix-tzdb="path/to/your/tzdb"` option. See also `zig build --help`

For development, to update the time zone database and the version info, run the following build steps: `zig build update-tz-database && zig build update-tz-version`.
For development, to update the time zone database and the version info, run the following build steps: `zig build update-tz-database && zig build update-tz-version`. Some of the code generation is done with Python scripts, which require Python >= 3.9 (no third party packages required).

## License

Expand Down
5 changes: 5 additions & 0 deletions docs/change.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2024-08-28, v0.2.1

- change of ergonomics: `formatToString`, `parseToDatetime` and `parseISO8601` functions are now methods of the `zdt` struct (formerly `stringIO.[...]`). After importing zdt, they can be called like `zdt.formatToString` etc.
- this is the first version that will be on github only. Having the same repository at both Codeberg and github is just too cumbersome.

## 2024-08-07, v0.2.0

- add a complete embedding of the IANA tzdb that allows cross-compilation
Expand Down
1 change: 0 additions & 1 deletion examples/ex_datetime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const zdt = @import("zdt");
const Datetime = zdt.Datetime;
const Duration = zdt.Duration;
const Tz = zdt.Timezone;
const str = zdt.stringIO;

pub fn main() !void {
println("---> datetime example", .{});
Expand Down
2 changes: 1 addition & 1 deletion examples/ex_demo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn main() !void {
var tz_Paris = try zdt.Timezone.fromTzfile("Europe/Paris", allocator);
defer tz_Paris.deinit();

const a_datetime = try zdt.stringIO.parseISO8601("2022-03-07");
const a_datetime = try zdt.parseISO8601("2022-03-07");
const this_time_LA = try a_datetime.tzLocalize(tz_LA);
const this_time_Paris = try this_time_LA.tzConvert(tz_Paris);

Expand Down
1 change: 0 additions & 1 deletion examples/ex_offsetTz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const builtin = @import("builtin");
const zdt = @import("zdt");
const Datetime = zdt.Datetime;
const Tz = zdt.Timezone;
const str = zdt.stringIO;

pub fn main() !void {
println("---> UTC offset time zone example", .{});
Expand Down
11 changes: 5 additions & 6 deletions examples/ex_strings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const zdt = @import("zdt");
const Datetime = zdt.Datetime;
const Duration = zdt.Duration;
const Timezone = zdt.Timezone;
const str = zdt.stringIO;

pub fn main() !void {
println("---> datetime example", .{});
Expand All @@ -18,20 +17,20 @@ pub fn main() !void {
println("", .{});
println("---> (usage) ISO8601: parse some allowed formats", .{});
const date_only = "2014-08-23";
var parsed = try str.parseISO8601(date_only);
var parsed = try zdt.parseISO8601(date_only);
assert(parsed.hour == 0);
println("parsed '{s}'\n to {s}", .{ date_only, parsed });
// the default string representation of a zdt.Datetime instance is always ISO8601

// we can have fractional seconds:
const datetime_with_frac = "2014-08-23 12:15:56.123456789";
parsed = try str.parseISO8601(datetime_with_frac);
parsed = try zdt.parseISO8601(datetime_with_frac);
assert(parsed.nanosecond == 123456789);
println("parsed '{s}'\n to {s}", .{ datetime_with_frac, parsed });

// we can also have a leap second, and a time zone specifier (Z == UTC):
const leap_datetime = "2016-12-31T23:59:60Z";
parsed = try str.parseISO8601(leap_datetime);
parsed = try zdt.parseISO8601(leap_datetime);
assert(parsed.second == 60);
assert(std.meta.eql(parsed.tzinfo.?, Timezone.UTC));
println("parsed '{s}'\n to {s}", .{ leap_datetime, parsed });
Expand All @@ -41,7 +40,7 @@ pub fn main() !void {
println("", .{});
println("---> (usage): parse some non-standard format", .{});
const dayfirst_dtstr = "23.7.2021, 9:45h";
parsed = try str.parseToDatetime("%d.%m.%Y, %H:%Mh", dayfirst_dtstr);
parsed = try zdt.parseToDatetime("%d.%m.%Y, %H:%Mh", dayfirst_dtstr);
assert(parsed.day == 23);
println("parsed '{s}'\n to {s}", .{ dayfirst_dtstr, parsed });

Expand All @@ -55,7 +54,7 @@ pub fn main() !void {
var s = std.ArrayList(u8).init(allocator);
defer s.deinit();
// the formatting directive is comptime-known:
try str.formatToString(s.writer(), "%a, %b %d %Y, %H:%Mh", parsed);
try zdt.formatToString(s.writer(), "%a, %b %d %Y, %H:%Mh", parsed);
println("formatted {s}\n to '{s}'", .{ parsed, s.items });
}

Expand Down
1 change: 0 additions & 1 deletion examples/ex_timezones.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const builtin = @import("builtin");
const zdt = @import("zdt");
const Datetime = zdt.Datetime;
const Tz = zdt.Timezone;
const str = zdt.stringIO;

pub fn main() !void {
println("---> time zones example", .{});
Expand Down
4 changes: 3 additions & 1 deletion lib/Timezone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn abbreviation(tz: *Timezone) []const u8 {
return std.mem.sliceTo(tz.tzOffset.?.__abbrev_data[0..], 0);
}

/// Make a time zone from a IANA tz database TZif data, taken from the embedded tzdata.
/// Make a time zone from IANA tz database TZif data, taken from the embedded tzdata.
/// The caller must make sure to de-allocate memory used for storing the TZif file's content
/// by calling the deinit method of the returned TZ instance.
pub fn fromTzdata(identifier: []const u8, allocator: std.mem.Allocator) TzError!Timezone {
Expand All @@ -90,6 +90,8 @@ pub fn fromTzdata(identifier: []const u8, allocator: std.mem.Allocator) TzError!
}

/// Make a time zone from a IANA tz database TZif file. The identifier must be comptime-known.
/// This method allows the usage of a user-supplied tzdata; that path has to be specified
/// via the tzdb_prefix option in the build.zig.
/// The caller must make sure to de-allocate memory used for storing the TZif file's content
/// by calling the deinit method of the returned TZ instance.
pub fn fromTzfile(comptime identifier: []const u8, allocator: std.mem.Allocator) !Timezone {
Expand Down
Binary file modified lib/tzdata/zoneinfo/America/Bahia_Banderas
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Cancun
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Chihuahua
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Ciudad_Juarez
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Ensenada
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Hermosillo
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Mazatlan
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Merida
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Mexico_City
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Monterrey
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Ojinaga
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Santa_Isabel
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/America/Tijuana
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/Mexico/BajaNorte
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/Mexico/BajaSur
Binary file not shown.
Binary file modified lib/tzdata/zoneinfo/Mexico/General
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/windows/windows_tznames.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// --- Do not edit ---
//
// latest referesh: 2024-08-07T15:27:54+00:00
// latest referesh: 2024-08-28T14:40:36+00:00
// windows_names are sorted alphabetically so we can do binary search (later)
pub const windows_names = [_][]const u8{
"AUS Central Standard Time",
Expand Down
Loading

0 comments on commit 8dace85

Please sign in to comment.