from
The Temporal.Duration.from() static method creates a new Temporal.Duration object from one of the following:
- A Temporal.Duration instance, which creates a copy of the instance.
- An ISO 8601 string representing a duration.
- A @Temporal.Duration.PartialDuration struct containing at least one of the following properties:
- days
- hours
- microseconds
- milliseconds
- minutes
- months
- nanoseconds
- seconds
- weeks
- years The resulting duration must not have mixed signs, so all of these properties must have the same sign (or zero). Missing properties are treated as zero.
Parameters
- info:anytype
- anytype
Source
Implementation
pub fn from(info: anytype) !Duration {
const T = @TypeOf(info);
if (T == Duration) return info.clone();
if (T == PartialDuration) return fromPartialDuration(info);
// Handle string types (both literals and slices)
const type_info = @typeInfo(T);
switch (type_info) {
.pointer => {
const ptr = type_info.pointer;
const ChildType = switch (@typeInfo(ptr.child)) {
.array => |arr| arr.child,
else => ptr.child,
};
if (ChildType == u8) return fromUtf8(info);
if (ChildType == u16) return fromUtf16(info);
},
else => @compileError("from() expects a Duration, []const u8, or []const u16, or Temporal.Duration.PartialDuration"),
}
}