Skip to content

Commit 453b1e2

Browse files
remove rfc 3339
1 parent 1673574 commit 453b1e2

File tree

3 files changed

+0
-177
lines changed

3 files changed

+0
-177
lines changed

lib/std/date/gregorian.zig

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -167,34 +167,6 @@ pub fn DateAdvanced(comptime YearT: type, epoch_: comptime_int, shift: comptime_
167167
const epoch_days = self.toEpoch() +% Weekday.thu.numeric();
168168
return @enumFromInt(std.math.comptimeMod(epoch_days, 7));
169169
}
170-
171-
pub fn fromRfc3339(str: *const [10]u8) !Self {
172-
if (str[4] != '-' or str[7] != '-') return error.Parsing;
173-
174-
const year = try std.fmt.parseInt(IntFittingRange(0, 9999), str[0..4], 10);
175-
const month = try std.fmt.parseInt(Month.Int, str[5..7], 10);
176-
if (month < 1 or month > 12) return error.Parsing;
177-
const m: Month = @enumFromInt(month);
178-
const day = try std.fmt.parseInt(Day, str[8..10], 10);
179-
if (day < 1 or day > m.days(is_leap(year))) return error.Parsing;
180-
181-
return .{
182-
.year = @intCast(year), // if YearT is `i8` or `u8` this may fail. increase it to not fail.
183-
.month = m,
184-
.day = day,
185-
};
186-
}
187-
188-
pub fn toRfc3339(self: Self, writer: anytype) !void {
189-
if (self.year < 0 or self.year > 9999) return error.Range;
190-
if (self.day < 1 or self.day > 99) return error.Range;
191-
if (self.month.numeric() < 1 or self.month.numeric() > 12) return error.Range;
192-
try writer.print("{d:0>4}-{d:0>2}-{d:0>2}", .{
193-
@as(UEpochDays, @intCast(self.year)),
194-
self.month.numeric(),
195-
self.day,
196-
});
197-
}
198170
};
199171
}
200172

@@ -241,13 +213,6 @@ test "Date from and to epoch" {
241213
try testFromToEpoch(Date(u64, epoch.windows));
242214
}
243215

244-
test "Date RFC3339" {
245-
const T = Date(i16, 0);
246-
try expectEqual(T{ .year = 2000, .month = .jan, .day = 1 }, try T.fromRfc3339("2000-01-01"));
247-
try std.testing.expectError(error.Parsing, T.fromRfc3339("2000T01-01"));
248-
try std.testing.expectError(error.InvalidCharacter, T.fromRfc3339("2000-01-AD"));
249-
}
250-
251216
test Date {
252217
const T = Date(i16, 0);
253218
const d1 = T{ .year = 1960, .month = .jan, .day = 1 };
@@ -278,12 +243,6 @@ test Date {
278243
try expectEqual(.tue, (T{ .year = 1980, .month = .jan, .day = 1 }).weekday());
279244
// $ date -d '1960-01-01'
280245
try expectEqual(.fri, d1.weekday());
281-
282-
try expectEqual(T{ .year = 2000, .month = .jan, .day = 1 }, try T.fromRfc3339("2000-01-01"));
283-
var buf: [10]u8 = undefined;
284-
var stream = std.io.fixedBufferStream(&buf);
285-
try (T{ .year = 2000, .month = .jan, .day = 1 }).toRfc3339(stream.writer());
286-
try std.testing.expectEqualStrings("2000-01-01", stream.getWritten());
287246
}
288247

289248
const WeekdayInt = IntFittingRange(1, 7);

lib/std/date_time.zig

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,6 @@ pub fn DateTime(comptime DateT: type, comptime TimeT: type) type {
4848
const date = self.date.add(year, month, day + time.day_overflow);
4949
return .{ .date = date, .time = time.time };
5050
}
51-
52-
pub fn fromRfc3339(str: []const u8) !Self {
53-
if (str.len < 10 + "hh:mm:ssZ".len) return error.Parsing;
54-
if (std.ascii.toUpper(str[10]) != 'T') return error.Parsing;
55-
return .{
56-
.date = try Date.fromRfc3339(str[0..10]),
57-
.time = try Time.fromRfc3339(str[11..]),
58-
};
59-
}
60-
61-
pub fn toRfc3339(self: Self, writer: anytype) !void {
62-
try self.date.toRfc3339(writer);
63-
try writer.writeByte('T');
64-
try self.time.toRfc3339(writer);
65-
}
6651
};
6752
}
6853

@@ -118,41 +103,6 @@ test "Date epoch" {
118103
});
119104
}
120105

121-
test "Date RFC 3339 section 5.8" {
122-
const T = DateTime(date_mod.Date, time_mod.Time(3, true));
123-
const expectEqual = std.testing.expectEqual;
124-
const t1 = T{
125-
.date = .{ .year = 1985, .month = .apr, .day = 12 },
126-
.time = .{ .hour = 23, .minute = 20, .second = 50, .subsecond = 520 },
127-
};
128-
try expectEqual(t1, try T.fromRfc3339("1985-04-12T23:20:50.52Z"));
129-
const t2 = T{
130-
.date = .{ .year = 1996, .month = .dec, .day = 19 },
131-
.time = .{ .hour = 16, .minute = 39, .second = 57, .offset = -8 * 60 },
132-
};
133-
try expectEqual(t2, try T.fromRfc3339("1996-12-19T16:39:57-08:00"));
134-
const t3 = T{
135-
.date = .{ .year = 1990, .month = .dec, .day = 31 },
136-
.time = .{ .hour = 23, .minute = 59, .second = 60 },
137-
};
138-
try expectEqual(t3, try T.fromRfc3339("1990-12-31T23:59:60Z"));
139-
const t4 = T{
140-
.date = .{ .year = 1990, .month = .dec, .day = 31 },
141-
.time = .{ .hour = 15, .minute = 59, .second = 60, .offset = -8 * 60 },
142-
};
143-
try expectEqual(t4, try T.fromRfc3339("1990-12-31T15:59:60-08:00"));
144-
const t5 = T{
145-
.date = .{ .year = 1937, .month = .jan, .day = 1 },
146-
.time = .{ .hour = 12, .second = 27, .subsecond = 870, .offset = 20 },
147-
};
148-
try expectEqual(t5, try T.fromRfc3339("1937-01-01T12:00:27.87+00:20"));
149-
150-
var buf: [32]u8 = undefined;
151-
var stream = std.io.fixedBufferStream(&buf);
152-
try t5.toRfc3339(stream.writer());
153-
try std.testing.expectEqualStrings("1937-01-01T12:00:27.870+00:20", stream.getWritten());
154-
}
155-
156106
const std = @import("std.zig");
157107
const date_mod = @import("./date.zig");
158108
const time_mod = @import("./time.zig");

lib/std/time.zig

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -441,72 +441,6 @@ pub fn Time(precision_: comptime_int, comptime has_offset: bool) type {
441441
pub fn add(self: Self, hour: i64, minute: i64, second: i64, subsecond: i64) Self {
442442
return self.addWithOverflow(hour, minute, second, subsecond).time;
443443
}
444-
445-
pub fn fromRfc3339(str: []const u8) !Self {
446-
if (str.len < "hh:mm:ssZ".len) return error.Parsing;
447-
448-
if (str[2] != ':' or str[5] != ':') return error.Parsing;
449-
450-
const hour = try std.fmt.parseInt(Hour, str[0..2], 10);
451-
const minute = try std.fmt.parseInt(Minute, str[3..5], 10);
452-
const second = try std.fmt.parseInt(Second, str[6..8], 10);
453-
454-
var i: usize = 8;
455-
const subsecond: Subsecond = if (str[i] == '.') brk: {
456-
i += 1;
457-
while (i < str.len and std.ascii.isDigit(str[i])) : (i += 1) {}
458-
if (Subsecond == u0) break :brk 0;
459-
const subsecond_str = str[9..i];
460-
// Choose largest performant type.
461-
// Ideally, this would allow infinite precision.
462-
const T = f64;
463-
var subsecond = try std.fmt.parseFloat(T, subsecond_str);
464-
const actual_precision: T = @floatFromInt(subsecond_str.len);
465-
subsecond *= std.math.pow(T, 10, precision - actual_precision);
466-
467-
break :brk @intFromFloat(subsecond);
468-
} else 0;
469-
470-
// timezone required
471-
if (str.len <= i) return error.Parsing;
472-
473-
const offset = if (std.ascii.toUpper(str[i]) == 'Z') 0 else brk: {
474-
var sign: IMinutes = 1;
475-
if (str[i] == '-') {
476-
sign = -1;
477-
i += 1;
478-
} else if (str[i] == '+') {
479-
i += 1;
480-
}
481-
482-
const offset_hour = try std.fmt.parseInt(IMinutes, str[i..][0..2], 10);
483-
if (str[i + 2] != ':') return error.Parsing;
484-
const offset_minute = try std.fmt.parseInt(IMinutes, str[i + 3 ..][0..2], 10);
485-
486-
break :brk sign * (offset_hour * 60 + offset_minute);
487-
};
488-
489-
return .{ .hour = hour, .minute = minute, .second = second, .subsecond = subsecond, .offset = offset };
490-
}
491-
492-
pub fn toRfc3339(self: Self, writer: anytype) !void {
493-
if (self.hour > 24 or self.minute > 59 or self.second > 60) return error.Range;
494-
try writer.print("{d:0>2}:{d:0>2}:{d:0>2}", .{ self.hour, self.minute, self.second });
495-
if (self.subsecond != 0) {
496-
// We could trim trailing zeros here to save space.
497-
try writer.print(".{d}", .{self.subsecond});
498-
}
499-
if (self.offset == 0) {
500-
try writer.writeByte('Z');
501-
} else {
502-
try writer.writeByte(if (self.offset > 0) '+' else '-');
503-
const abs: u16 = @intCast(if (self.offset > 0) self.offset else -self.offset);
504-
const offset_hour = abs / 60;
505-
if (offset_hour < -24 or offset_hour > 24) return error.Range;
506-
const offset_minute = abs % 60;
507-
try writer.print("{d:0>2}:{d:0>2}", .{ offset_hour, offset_minute });
508-
}
509-
}
510444
};
511445
}
512446

@@ -530,26 +464,6 @@ test Time {
530464
TimeMilliOffset{ .hour = 22, .minute = 30, .offset = -90 },
531465
TimeMilliOffset.fromDaySeconds(0, .{ .offset = -90 }),
532466
);
533-
534-
try expectEqual(TimeMilliOffset{ .hour = 22, .minute = 30, .offset = -90 }, try TimeMilliOffset.fromRfc3339("22:30:00-01:30"));
535-
try expectEqual(TimeMilliOffset{ .hour = 22, .minute = 30, .offset = 90 }, try TimeMilliOffset.fromRfc3339("22:30:00.0000+01:30"));
536-
try expectEqual(TimeMilliOffset{ .hour = 22, .minute = 30, .second = 20, .subsecond = 100 }, try TimeMilliOffset.fromRfc3339("22:30:20.1Z"));
537-
538-
const expectError = std.testing.expectError;
539-
try expectError(error.Parsing, TimeMilliOffset.fromRfc3339("22:30:20.100")); // missing timezone
540-
try expectError(error.InvalidCharacter, TimeMilliOffset.fromRfc3339("22:30:20.1a00"));
541-
try expectError(error.Parsing, TimeMilliOffset.fromRfc3339("2:00:20Z")); // missing hour digit
542-
543-
var buf: [32]u8 = undefined;
544-
var stream = std.io.fixedBufferStream(&buf);
545-
const time = TimeMilliOffset{ .hour = 22, .minute = 30, .second = 20, .subsecond = 100 };
546-
try time.toRfc3339(stream.writer());
547-
try std.testing.expectEqualStrings("22:30:20.100Z", stream.getWritten());
548-
549-
stream.reset();
550-
const time2 = TimeMilliOffset{ .hour = 22, .minute = 30, .second = 20, .subsecond = 100, .offset = 100 };
551-
try time2.toRfc3339(stream.writer());
552-
try std.testing.expectEqualStrings("22:30:20.100+01:40", stream.getWritten());
553467
}
554468

555469
comptime {

0 commit comments

Comments
 (0)