Name
SPVM::Go::Time - Time management with monotonic time support (Go-style)
Description
Go::Time class in SPVM is a time management class based on the implementation of time.Time in the Go programming language.
It internally maintains both wall clock time (CLOCK_REALTIME) and monotonic time (CLOCK_MONOTONIC). This dual-structure ensures that time measurements for durations remain accurate even if the system clock is adjusted.
Usage
use Go::Time;
# Get current time
my $now = Go::Time->now;
# 1.5 seconds later
my $later = $now->add_sec(1.5);
Interfaces
Class Methods
NANOSECOND
static method NANOSECOND : long ();
Returns 1 nanosecond.
See Go Time Constants.
MICROSECOND
static method MICROSECOND : long ();
Returns 1,000 nanoseconds.
MILLISECOND
static method MILLISECOND : long ();
Returns 1,000,000 nanoseconds.
SECOND
static method SECOND : long ();
Returns 1,000,000,000 nanoseconds.
MINUTE
static method MINUTE : long ();
Returns 60,000,000,000 nanoseconds.
HOUR
static method HOUR : long ();
Returns 3,600,000,000,000 nanoseconds.
new_from_unix
static method new_from_unix : Go::Time ($sec : long = 0, $nsec : long = 0);
Creates a new Go::Time object from Unix seconds and nanoseconds. See time.Unix.
now
static method now : Go::Time ();
Creates a new Go::Time object representing the current system time. It captures both the wall clock and the monotonic clock. See time.Now.
parse_rfc3339_nano
static method parse_rfc3339_nano : Go::Time ($rfc3339_string : string);
Parses an RFC 3339 formatted string with nanoseconds and returns a new Go::Time object.
Currently, only the UTC timezone (suffix "Z") is supported. If the timezone is not UTC, the method will die.
Nanoseconds are optional and will be normalized to 9 digits.
Example:
my $t = Go::Time->parse_rfc3339_nano("2025-01-16T13:00:00.123456789Z");
See time.Parse with RFC3339Nano.
Instance Methods
unix
method unix : long ();
Returns the Unix "seconds" component of the time. See Time.Unix.
nanosecond
method nanosecond : long ();
Returns the nanoseconds component within the second (0-999,999,999). See Time.Nanosecond.
unix_nano
method unix_nano : long ();
Returns the total elapsed time since the Unix epoch in nanoseconds. See Time.UnixNano.
add
method add : Go::Time ($duration : Go::Duration_1l);
Returns a new Go::Time object with the given duration added. See Time.Add.
add_sec
method add_sec : Go::Time ($seconds : double);
A helper method that adds the specified number of seconds (as a double) and returns a new Go::Time object.
sub
method sub : Go::Duration_1l ($go_time : Go::Time);
Returns the duration t - go_time. It handles monotonic clocks correctly. See Time.Sub.
equal
method equal : int ($go_time : Go::Time);
Reports whether the time and go_time represent the same instant. See Time.Equal.
after
method after : int ($go_time : Go::Time);
Reports whether the time instant is after go_time. See Time.After.
before
method before : int ($go_time : Go::Time);
Reports whether the time instant is before go_time. See Time.Before.
clone
method clone : Go::Time ();
Returns a deep copy of the Go::Time object. This method implements Cloneable.
to_timespec
method to_timespec : Sys::Time::Timespec ();
Converts the internal time to a Sys::Time::Timespec object.
format_rfc3339_nano
method format_rfc3339_nano : string ();
Returns a string representing the time in RFC 3339 format with nanoseconds.
Consistent with Go's time.RFC3339Nano format, trailing zeros in the nanosecond part are removed. If the nanosecond part is zero, it is omitted entirely.
Example:
# 2025-01-16T13:00:00.123456789Z
# 2025-01-16T13:00:00Z (if nsec is 0)
See Time.Format.
See Also
Author
Yuki Kimoto kimoto.yuki@gmail.com
Copyright & License
Copyright (c) 2026 Yuki Kimoto
MIT License