A lightweight time library for MoonBit with picosecond precision
moon add SupremeHuaji/ptime// Create Unix epoch time (1970-01-01 00:00:00 UTC)
let epoch_time = epoch()
// Create time point from float
let time_from_float = of_float(1694764200.5)
// Directly construct time point
let custom_time = PTime::{ seconds: 1694764200, picoseconds: 500000000000 }let now = PTime::{ seconds: 1694764200, picoseconds: 0 }
let past = PTime::{ seconds: 1694760000, picoseconds: 0 }
// Compare time points
let comparison = compare(now, past) // 1 (now > past)
// Calculate time difference
let time_diff = diff(now, past)
println("Time difference: \{time_diff.seconds} seconds")// Parse RFC 3339 time string
match of_rfc3339("2023-09-15T08:30:00Z") {
Ok(time) => {
// Convert to RFC 3339 format
let rfc3339_str = to_rfc3339(time)
println("RFC 3339: \{rfc3339_str}")
}
Err(error) => println("Parse error: \{error}")
}
// Support formats with timezone offsets
let time_with_offset = of_rfc3339("2023-09-15T08:30:00+08:00")let time = PTime::{ seconds: 1694764200, picoseconds: 0 }
// Standard formats
println("ISO 8601: \{to_iso8601(time)}")
println("RFC 3339: \{to_rfc3339(time)}")
// Human-readable formats
println("Human readable: \{to_human_readable(time)}")
println("Detailed format: \{to_detailed_human_readable(time)}")
println("Short format: \{to_short_format(time)}")
// Professional formats
println("Log format: \{to_log_format(time)}")
println("HTTP date: \{to_http_date(time)}")
println("JSON time: \{to_json_time(time)}")
println("SQL datetime: \{to_sql_datetime(time)}")
// Special formats
println("Filename safe: \{to_filename_safe(time)}")
println("12-hour format: \{to_12_hour_format(time)}")// Create time span
let now = PTime::{ seconds: 1694764200, picoseconds: 0 }
let span = Span::{ seconds: 3600, picoseconds: 500000000000 } // 1 hour 500 milliseconds
// Time span operations
let doubled = mul_span(span, 2.0)
let half = div_span(span, 2)
let sum = add_spans(span, span)
let diff = sub_spans(span, span)
// Time span formatting
println("Humanized: \{humanize_span(span)}")
println("Approximate: \{approximate_span(span)}")
println("Precise: \{precise_span(span)}")
// Time point operations
let future_time = add_span(now, span)
let past_time = add_span(now, negate_span(span))let now = PTime::{ seconds: 1694764200, picoseconds: 0 }
let past = PTime::{ seconds: 1694760000, picoseconds: 0 }
// Relative time description
let relative = to_relative_time(past, now)
println("Relative time: \{relative}") // "1 hour ago"let time = PTime::{ seconds: 1694764200, picoseconds: 500000000000 }
// Various timestamp formats
println("Unix timestamp: \{to_unix_timestamp(time)}")
println("Milliseconds timestamp: \{to_milliseconds_timestamp(time)}")
println("Microseconds timestamp: \{to_microseconds_timestamp(time)}")
println("Nanoseconds timestamp: \{to_nanoseconds_timestamp(time)}")let time = PTime::{ seconds: 1694764200, picoseconds: 0 }
// Custom format
let custom = to_custom_format(time, "%Y-%m-%d %H:%M:%S")
println("Custom format: \{custom}") // "2023-09-15 08:30:00"let time = PTime::{ seconds: 1694764200, picoseconds: 0 }
// Convert to float
let float_time = to_float(time)
println("Float time: \{float_time}")
// Check if time span is zero
let zero_span = Span::{ seconds: 0, picoseconds: 0 }
let is_zero = is_zero(zero_span)
println("Is zero span: \{is_zero}") // true// Time points can be converted to other numeric types
let time = PTime::{ seconds: 1694764200, picoseconds: 0 }
let float_val = to_float(time)
let back_to_time = of_float(float_val)
// Time spans support various operations
let span1 = Span::{ seconds: 100, picoseconds: 0 }
let span2 = Span::{ seconds: 200, picoseconds: 0 }
let combined = add_spans(span1, span2)fn benchmark() {
let start = now() // Get current time
// Perform some operations
// ...
let end = now()
let duration = diff(end, start)
println("Operation took: \{humanize_span(duration)}")
}fn format_timestamp(timestamp: Double) -> String {
let time = of_float(timestamp)
to_custom_format(time, "%Y-%m-%d %H:%M:%S")
}Install
Download zipA lightweight time library for MoonBit with picosecond precision