Skip to content

Commit fd74ff0

Browse files
committed
Rename items
1 parent 0d3bf57 commit fd74ff0

File tree

3 files changed

+145
-63
lines changed

3 files changed

+145
-63
lines changed

CHANGELOG.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
# Changelog
22

3-
## v2.0.0 - 2025-06-01
3+
## v2.0.0 - 2025-05-31
44

55
- Added [gleam_time](https://hexdocs.pm/gleam_time/index.html) as a dependency.
6-
- Replaced the custom `Date` type with [calendar.Date](https://hexdocs.pm/gleam_time/gleam/time/calendar.html#Date).
7-
- Replaced the custom `Time` type with [calendar.TimeOfDay](https://hexdocs.pm/gleam_time/gleam/time/calendar.html#TimeOfDay).
8-
- Reworked the `Offset` record to hold [duration.Duration](https://hexdocs.pm/gleam_time/gleam/time/duration.html#Duration).
9-
- Updated the `DateTime` type to incorporate the new `Date`, `Time`, and `Offset`.
6+
- Replaced the custom `Date` type with
7+
[calendar.Date](https://hexdocs.pm/gleam_time/gleam/time/calendar.html#Date).
8+
- Replaced the custom `Time` type with
9+
[calendar.TimeOfDay](https://hexdocs.pm/gleam_time/gleam/time/calendar.html#TimeOfDay).
10+
- Reworked the `Offset` record to hold
11+
[duration.Duration](https://hexdocs.pm/gleam_time/gleam/time/duration.html#Duration).
12+
- The `DateTime` type has been removed.
1013
- Updated the time parser to support nanosecond precision.
14+
- Added `as_timestamp`.
15+
- Added `get_timestamp`.
16+
- Renamed `as_date_time` to `as_calendar_time`.
17+
- Renamed `as_time` to `as_time_of_day`.
18+
- Renamed `get_date_time` to `get_calendar_time`.
19+
- Renamed `get_time` to `get_time_of_day`.
1120

1221
[View diff on Hex](https://diff.hex.pm/diff/tom/1.1.1..2.0.0)
1322

src/tom.gleam

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import gleam/result
3232
import gleam/string
3333
import gleam/time/calendar
3434
import gleam/time/duration
35+
import gleam/time/timestamp
3536

3637
/// A TOML document.
3738
pub type Toml {
@@ -47,17 +48,13 @@ pub type Toml {
4748
String(String)
4849
Date(calendar.Date)
4950
Time(calendar.TimeOfDay)
50-
DateTime(DateTime)
51+
DateTime(date: calendar.Date, time: calendar.TimeOfDay, offset: Offset)
5152
Array(List(Toml))
5253
ArrayOfTables(List(Dict(String, Toml)))
5354
Table(Dict(String, Toml))
5455
InlineTable(Dict(String, Toml))
5556
}
5657

57-
pub type DateTime {
58-
DateTimeValue(date: calendar.Date, time: calendar.TimeOfDay, offset: Offset)
59-
}
60-
6158
pub type Offset {
6259
Local
6360
Offset(duration.Duration)
@@ -243,11 +240,11 @@ pub fn get_date(
243240
///
244241
/// ```gleam
245242
/// let assert Ok(parsed) = parse("a.b.c = 07:32:00")
246-
/// get_time(parsed, ["a", "b", "c"])
243+
/// get_time_of_day(parsed, ["a", "b", "c"])
247244
/// // -> Ok(TimeOfDay(7, 32, 0, 0))
248245
/// ```
249246
///
250-
pub fn get_time(
247+
pub fn get_time_of_day(
251248
toml: Dict(String, Toml),
252249
key: List(String),
253250
) -> Result(calendar.TimeOfDay, GetError) {
@@ -264,21 +261,47 @@ pub fn get_time(
264261
///
265262
/// ```gleam
266263
/// let assert Ok(parsed) = parse("a.b.c = 1979-05-27T07:32:00")
267-
/// get_date_time(parsed, ["a", "b", "c"])
268-
/// // -> Ok(DateTimeValue(Date(1979, May, 27), TimeOfDay(7, 32, 0, 0), Local))
264+
/// get_calendar_time(parsed, ["a", "b", "c"])
265+
/// // -> Ok(#(Date(1979, May, 27), TimeOfDay(7, 32, 0, 0), Local))
269266
/// ```
270267
///
271-
pub fn get_date_time(
268+
pub fn get_calendar_time(
272269
toml: Dict(String, Toml),
273270
key: List(String),
274-
) -> Result(DateTime, GetError) {
271+
) -> Result(#(calendar.Date, calendar.TimeOfDay, Offset), GetError) {
275272
case get(toml, key) {
276-
Ok(DateTime(i)) -> Ok(i)
273+
Ok(DateTime(d, t, o)) -> Ok(#(d, t, o))
277274
Ok(other) -> Error(WrongType(key, "DateTime", classify(other)))
278275
Error(e) -> Error(e)
279276
}
280277
}
281278

279+
/// Get an unambiguous time from a TOML document dictionary.
280+
///
281+
/// If a TOML date time has no offset it is ambiguous and cannot be converted
282+
/// into a timestamp. There's no way to know what actual point in time it would
283+
/// be as it would be different in different time zones.
284+
///
285+
/// ## Examples
286+
///
287+
/// ```gleam
288+
/// let assert Ok(parsed) = parse("a.b.c = 1970-00-00T00:00:00Z")
289+
/// get_timestamp(parsed, ["a", "b", "c"])
290+
/// // -> Ok(timestamp.from_unix_seconds(0))
291+
/// ```
292+
///
293+
pub fn get_timestamp(
294+
toml: Dict(String, Toml),
295+
key: List(String),
296+
) -> Result(timestamp.Timestamp, GetError) {
297+
case get(toml, key) {
298+
Ok(DateTime(date:, time:, offset: Offset(offset))) ->
299+
Ok(timestamp.from_calendar(date, time, offset))
300+
Ok(other) -> Error(WrongType(key, "DateTime with offset", classify(other)))
301+
Error(e) -> Error(e)
302+
}
303+
}
304+
282305
// TODO: test
283306
/// Get an array from a TOML document dictionary.
284307
///
@@ -363,7 +386,7 @@ fn classify(toml: Toml) -> String {
363386
String(_) -> "String"
364387
Date(_) -> "Date"
365388
Time(_) -> "Time"
366-
DateTime(_) -> "DateTime"
389+
DateTime(_, _, _) -> "DateTime"
367390
Array(_) -> "Array"
368391
ArrayOfTables(_) -> "Array"
369392
Table(_) -> "Table"
@@ -1322,7 +1345,7 @@ fn parse_date_end(
13221345
[" ", ..input] | ["T", ..input] -> {
13231346
use time, input <- do(parse_time_value(input))
13241347
use offset, input <- do(parse_offset(input))
1325-
Ok(#(DateTime(DateTimeValue(date, time, offset)), input))
1348+
Ok(#(DateTime(date, time, offset), input))
13261349
}
13271350

13281351
_ -> Ok(#(Date(date), input))
@@ -1452,37 +1475,56 @@ pub fn as_date(toml: Toml) -> Result(calendar.Date, GetError) {
14521475
/// ## Examples
14531476
///
14541477
/// ```gleam
1455-
/// as_time(Time(time))
1478+
/// as_time_of_day(Time(time))
14561479
/// // -> Ok(time)
14571480
/// ```
14581481
///
14591482
/// ```gleam
1460-
/// as_time(Int(1))
1483+
/// as_time_of_day(Int(1))
14611484
/// // -> Error(WrongType([], "Time", "Int"))
14621485
/// ```
1463-
pub fn as_time(toml: Toml) -> Result(calendar.TimeOfDay, GetError) {
1486+
pub fn as_time_of_day(toml: Toml) -> Result(calendar.TimeOfDay, GetError) {
14641487
case toml {
14651488
Time(t) -> Ok(t)
14661489
other -> Error(WrongType([], "Time", classify(other)))
14671490
}
14681491
}
14691492

1493+
/// Get an unambiguous time from a TOML document.
1494+
///
1495+
/// ## Examples
1496+
///
1497+
/// ```gleam
1498+
/// as_timestamp(Int(1))
1499+
/// // -> Error(WrongType([], "DateTime with offset", "Int"))
1500+
/// ```
1501+
pub fn as_timestamp(toml: Toml) -> Result(timestamp.Timestamp, GetError) {
1502+
case toml {
1503+
DateTime(date:, time:, offset: Offset(offset)) ->
1504+
Ok(timestamp.from_calendar(date, time, offset))
1505+
1506+
other -> Error(WrongType([], "DateTime with offset", classify(other)))
1507+
}
1508+
}
1509+
14701510
/// Get a datetime from a TOML document.
14711511
///
14721512
/// ## Examples
14731513
///
14741514
/// ```gleam
1475-
/// as_date_time(DateTime(datetime))
1515+
/// as_calendar_time(DateTime(datetime))
14761516
/// // -> Ok(datetime)
14771517
/// ```
14781518
///
14791519
/// ```gleam
1480-
/// as_date_time(Int(1))
1520+
/// as_calendar_time(Int(1))
14811521
/// // -> Error(WrongType([], "DateTime", "Int"))
14821522
/// ```
1483-
pub fn as_date_time(toml: Toml) -> Result(DateTime, GetError) {
1523+
pub fn as_calendar_time(
1524+
toml: Toml,
1525+
) -> Result(#(calendar.Date, calendar.TimeOfDay, Offset), GetError) {
14841526
case toml {
1485-
DateTime(dt) -> Ok(dt)
1527+
DateTime(d, t, o) -> Ok(#(d, t, o))
14861528
other -> Error(WrongType([], "DateTime", classify(other)))
14871529
}
14881530
}

0 commit comments

Comments
 (0)