Speed up date

Date was taking a long time for parsing when using ADD in batch, speeded
up by like x50
This commit is contained in:
2025-01-11 15:27:17 +01:00
parent 7d012b527f
commit 1495e779c9
3 changed files with 30 additions and 25 deletions
+12 -11
View File
@@ -24,19 +24,20 @@ pub const DateTime = struct {
}
pub fn now() Self {
return epoch_unix.addYears(1970).addMs(@as(u64, @intCast(std.time.milliTimestamp())));
return epoch_unix.addMs(@as(u64, @intCast(std.time.milliTimestamp())));
}
/// Caller asserts that this is > epoch
pub fn init(year: u16, month: u16, day: u16, hr: u16, min: u16, sec: u16, ms: u16) Self {
return epoch_unix
.addYears(year - epoch_unix.years)
.addMonths(month)
.addDays(day)
.addHours(hr)
.addMins(min)
.addSecs(sec)
.addMs(ms);
pub fn init(year: u16, month: u8, day: u8, hr: u8, min: u8, sec: u8, ms: u16) Self {
return Self{
.years = if (year < 1970) 1970 else year,
.months = month,
.days = day,
.hours = hr,
.minutes = min,
.seconds = sec,
.ms = ms,
};
}
pub const epoch_unix = Self{
@@ -46,7 +47,7 @@ pub const DateTime = struct {
.hours = 0,
.days = 0,
.months = 0,
.years = 0,
.years = 1970,
};
pub fn eql(self: Self, other: Self) bool {
+10 -10
View File
@@ -45,8 +45,8 @@ pub fn parseDate(value_str: []const u8) DateTime {
if (std.mem.eql(u8, value_str, "NOW")) return DateTime.now();
const year: u16 = std.fmt.parseInt(u16, value_str[0..4], 10) catch 0;
const month: u16 = std.fmt.parseInt(u16, value_str[5..7], 10) catch 0;
const day: u16 = std.fmt.parseInt(u16, value_str[8..10], 10) catch 0;
const month: u8 = std.fmt.parseInt(u8, value_str[5..7], 10) catch 0;
const day: u8 = std.fmt.parseInt(u8, value_str[8..10], 10) catch 0;
return DateTime.init(year, month - 1, day - 1, 0, 0, 0, 0);
}
@@ -78,9 +78,9 @@ pub fn parseArrayDateUnix(allocator: std.mem.Allocator, array_str: []const u8) !
pub fn parseTime(value_str: []const u8) DateTime {
if (std.mem.eql(u8, value_str, "NOW")) return DateTime.now();
const hours: u16 = std.fmt.parseInt(u16, value_str[0..2], 10) catch 0;
const minutes: u16 = std.fmt.parseInt(u16, value_str[3..5], 10) catch 0;
const seconds: u16 = if (value_str.len > 6) std.fmt.parseInt(u16, value_str[6..8], 10) catch 0 else 0;
const hours: u8 = std.fmt.parseInt(u8, value_str[0..2], 10) catch 0;
const minutes: u8 = std.fmt.parseInt(u8, value_str[3..5], 10) catch 0;
const seconds: u8 = if (value_str.len > 6) std.fmt.parseInt(u8, value_str[6..8], 10) catch 0 else 0;
const milliseconds: u16 = if (value_str.len > 9) std.fmt.parseInt(u16, value_str[9..13], 10) catch 0 else 0;
return DateTime.init(0, 0, 0, hours, minutes, seconds, milliseconds);
@@ -114,11 +114,11 @@ pub fn parseDatetime(value_str: []const u8) DateTime {
if (std.mem.eql(u8, value_str, "NOW")) return DateTime.now();
const year: u16 = std.fmt.parseInt(u16, value_str[0..4], 10) catch 0;
const month: u16 = std.fmt.parseInt(u16, value_str[5..7], 10) catch 0;
const day: u16 = std.fmt.parseInt(u16, value_str[8..10], 10) catch 0;
const hours: u16 = std.fmt.parseInt(u16, value_str[11..13], 10) catch 0;
const minutes: u16 = std.fmt.parseInt(u16, value_str[14..16], 10) catch 0;
const seconds: u16 = if (value_str.len > 17) std.fmt.parseInt(u16, value_str[17..19], 10) catch 0 else 0;
const month: u8 = std.fmt.parseInt(u8, value_str[5..7], 10) catch 0;
const day: u8 = std.fmt.parseInt(u8, value_str[8..10], 10) catch 0;
const hours: u8 = std.fmt.parseInt(u8, value_str[11..13], 10) catch 0;
const minutes: u8 = std.fmt.parseInt(u8, value_str[14..16], 10) catch 0;
const seconds: u8 = if (value_str.len > 17) std.fmt.parseInt(u8, value_str[17..19], 10) catch 0 else 0;
const milliseconds: u16 = if (value_str.len > 20) std.fmt.parseInt(u16, value_str[20..24], 10) catch 0 else 0;
return DateTime.init(year, month - 1, day - 1, hours, minutes, seconds, milliseconds);