NAME
Time::Moment - Represents a date and time of day with an offset from UTC
SYNOPSIS
$tm = Time::Moment->now;
$tm = Time::Moment->now_utc;
$tm = Time::Moment->from_epoch($seconds [, $nanosecond [, $offset ]]);
$tm = Time::Moment->from_object($object);
$tm = Time::Moment->from_string($string);
$year = $tm->year; # [1, 9999]
$quarter = $tm->quarter; # [1, 4]
$month = $tm->month; # [1, 12]
$week = $tm->week; # [1, 53]
$day = $tm->day_of_year; # [1, 366]
$day = $tm->day_of_quarter; # [1, 92]
$day = $tm->day_of_month; # [1, 31]
$day = $tm->day_of_week; # [1=Monday, 7=Sunday]
$hour = $tm->hour; # [0, 23]
$minute = $tm->minute; # [0, 59]
$second = $tm->second; # [0, 59]
$millisecond = $tm->millisecond; # [0, 999]
$microsecond = $tm->microsecond; # [0, 999_999]
$nanosecond = $tm->nanosecond; # [0, 999_999_999]
$epoch = $tm->epoch;
$offset = $tm->offset; # [-1080, 1080]
$tm2 = $tm1->with_offset($offset);
$tm2 = $tm1->with_nanosecond($nanosecond);
$boolean = $tm1->is_before($tm2);
$boolean = $tm1->is_after($tm2);
$boolean = $tm1->is_equal($tm2);
$integer = $tm1->compare($tm2);
$string = $tm->to_string;
$string = $tm->strftime($format);
@values = $tm->utc_rd_values;
$seconds = $tm->utc_rd_as_seconds;
@values = $tm->local_rd_values;
$seconds = $tm->local_rd_as_seconds;
$boolean = $tm1 == $tm2;
$boolean = $tm1 != $tm2;
$boolean = $tm1 < $tm2;
$boolean = $tm1 > $tm2;
$boolean = $tm1 <= $tm2;
$boolean = $tm1 >= $tm2;
$string = "$tm";
DESCRIPTION
Represents a date and time of day with an offset from UTC in the ISO
8601 calendar system.
IMPORTANT: This is an early preview release available for testing and
feedback. The API is still subject to change.
CONSTRUCTORS
now
$tm = Time::Moment->now;
Constructs an instance of "Time::Moment" that is set to the current date
and time from the system clock in the system time zone, with the offset
set to the system's time zone offset from UTC.
now_utc
$tm = Time::Moment->now_utc;
Constructs an instance of "Time::Moment" that is set to the current date
and time from the system clock in the UTC time zone.
from_epoch
$tm = Time::Moment->from_epoch($seconds);
$tm = Time::Moment->from_epoch($seconds, $nanosecond);
$tm = Time::Moment->from_epoch($seconds, $nanosecond, $offset);
Constructs an instance of "Time::Moment" from the given *seconds* from
the epoch of 1970-01-01T00:00:00Z. The optional parameter *nanosecond*
specifies the nanosecond of the second [0, 999_999_999]. The optional
parameter *offset* specifies the time zone offset from UTC in minutes
[-1080, 1080].
Fractional seconds is supported if the constructor is invoked with
*seconds* only:
$tm = Time::Moment->from_epoch(0.123456); # 1970-01-01T00:00:00.123456Z
from_object
$tm = Time::Moment->from_object($object);
Constructs an instance of "Time::Moment" from the given *object*. If the
given object is an instance of "Time::Moment" it's returned otherwise an
attempt is made to coerce the given object to an instance of
"Time::Moment".
"Time::Moment" implements coercion handlers for the following object
types:
DateTime
$tm = Time::Moment->from_object( DateTime->now );
The given "DateTime" object must be within the supported date range
and must have a time zone or a time zone offset from UTC, coercing
from the 'floating' time zone is not supported.
Time::Piece
$tm = Time::Moment->from_object( Time::Piece::localtime() );
The given "Time::Piece" object must be within the supported date
range.
The coercion scheme is extensible and implemented as documented in
Params::Coerce:
$tm = Params::Coerce::coerce('Time::Moment', Time::Piece::localtime());
$tm = Params::Coerce::coerce('Time::Moment', DateTime->now);
"Time::Moment" implements coercion handler from "Time::Moment" to
"DateTime":
$dt = Params::Coerce::coerce('DateTime', Time::Moment->now);
from_string
$tm = Time::Moment->from_string($string);
Constructs an instance of "Time::Moment" from the given *string*. The
string must consist of a complete representation of a date and time of
day followed by a zone designator. The second part of the time of day
representation may have a decimal fraction.
The following are examples of complete representations of date and time
of day representations:
Basic format: Example:
YYYYMMDDThhmmssZ 20121224T121530Z
YYYYMMDDThhmmss±hhmm 20121224T121530+0100
YYYYMMDDThhmmss±hh 20121224T121530+01
Extended format: Example:
YYYY-MM-DDThh:mm:ssZ 2012-12-24T12:15:30Z
YYYY-MM-DDThh:mm:ss±hh:mm 2012-12-24T12:15:30+01:00
YYYY-MM-DDThh:mm:ss±hh 2012-12-24T12:15:30+01
Where complete representations using calendar dates are shown, ordinal
dates or week dates may be substituted.
METHODS
year
$year = $tm->year;
Returns the year [1, 9999].
quarter
$quarter = $tm->quarter;
Returns the quarter of the year [1, 4].
month
$month = $tm->month;
Returns the month of the year [1, 12].
week
$week = $tm->week;
Returns the week of the year [1, 53].
day_of_year
$day = $tm->day_of_year;
Returns the day of the year [1, 366].
day_of_quarter
$day = $tm->day_of_quarter;
Returns the day of the quarter [1, 92].
day_of_month
$day = $tm->day_of_month;
Returns the day of the month [1, 31].
day_of_week
$day = $tm->day_of_week;
Returns the day of the week [1=Monday, 7=Sunday].
hour
$hour = $tm->hour;
Returns the hour of the day [0, 23].
minute
$minute = $tm->minute;
Returns the minute of the hour [0, 59].
second
$second = $tm->second;
Returns the second of the minute [0, 59].
millisecond
$millisecond = $tm->millisecond;
Returns the millisecond of the second [0, 999].
microsecond
$microsecond = $tm->microsecond;
Returns the microsecond of the second [0, 999_999].
nanosecond
$nanosecond = $tm->nanosecond;
Returns the nanosecond of the second [0, 999_999_999].
epoch
$epoch = $tm->epoch;
Returns the number of seconds from the epoch of 1970-01-01T00:00:00Z.
offset
$offset = $tm->offset;
Returns the time zone offset from UTC in minutes [-1080, 1080].
with_offset
$tm2 = $tm1->with_offset($offset);
Returns a copy of this time with the given time zone *offset* from UTC
in minutes altered. The resulting time is at the same instant.
with_nanosecond
$tm2 = $tm1->with_nanosecond($nanosecond);
Returns a copy of this time with the given *nanosecond* altered.
is_before
$boolean = $tm->is_before($other);
Returns a boolean indicating whether or not the instant of this time is
before the other time.
is_after
$boolean = $tm->is_after($other);
Returns a boolean indicating whether or not the instant of this time is
after the other time.
is_equal
$boolean = $tm->is_equal($other);
Returns a boolean indicating whether or not the instant of this time is
equal the other time.
compare
$integer = $tm->compare($other);
Returns an integer indicating whether the instant of this time is
before, after or equal another time. Returns a value less than zero if
this time is before the other; zero if this date is equal the other
time; a value greater than zero if this time is after the other time.
to_string
$string = $tm->to_string;
$string = $tm->to_string($reduced);
Returns a string representation of the instance. If the optional boolean
parameter *reduced* is true a shorter representation is attempted.
The string will be in one of the following representations:
YYYY-MM-DDThh:mm (only if $reduced = true)
YYYY-MM-DDThh:mm:ss
YYYY-MM-DDThh:mm:ss.fff
YYYY-MM-DDThh:mm:ss.ffffff
YYYY-MM-DDThh:mm:ss.fffffffff
Followed by a zone designator in one of the following representations:
Z
±hh (only if $reduced = true)
±hh:mm
The shortest representation will be used where the omitted parts are
implied to be zero.
strftime
$string = $tm->strftime($format);
Formats time according to the conversion specifications in the given
$format string. The format string consists of zero or more conversion
specifications and ordinary characters. All ordinary characters are
copied directly into the resulting string. A conversion specification
consists of a percent sign "%" and one other character.
The following conversion specifications are supported:
%a Replaced by the C locale's abbreviated day of the week name.
Example: Mon, Tue, ..., Sun.
%A Replaced by the C locale's full day of the week name. Example:
Monday, Tuesday, ..., Sunday.
%b Replaced by the C locale's abbreviated month name. Example: Jan,
Feb, ..., Dec.
%B Replaced by the C locale's full month name. Example: January,
February, ..., December.
%c Replaced by the C locale's date and time representation. Equivalent
to "%a %b %e %H:%M:%S %Y".
%C Replaced by the year divided by 100 and truncated to an integer, as
a decimal number [00, 99].
%d Replaced by the day of the month as a decimal number [01, 31].
%D Equivalent to "%m/%d/%y".
%e Replaced by the day of the month as a decimal number [1, 31]; a
single digit is preceded by a space.
%f Replaced by the fractional second including the preceding decimal
point or by an empty string if no fractional seconds are present.
This conversion specification permits use of an optional maximum
field width [0, 9] where the default field width of 0 will use the
shortest representation.
Example:
%f or %0f is replaced by one of the following if fractional seconds
are present (shortest representation):
.fff (millisecond)
.ffffff (microsecond)
.fffffffff (nanosecond)
%4f is replaced by decimal point and exactly four fractional digits
(zero-padded on the right or truncated if needed) if fractional
seconds are present:
.ffff
*This conversion specification is an extension to the "IEEE Std
1003.1"
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.
html>*.
%F Equivalent to "%Y-%m-%d".
%g Replaced by the last 2 digits of the year of the week as a decimal
number [00, 99].
%G Replaced by the week-based year as a decimal number [0001, 9999].
%h Equivalent to %b.
%H Replaced by the hour of day (24-hour clock) as a decimal number [00,
23].
%I Replaced by the hour of day (12-hour clock) as a decimal number [01,
12].
%j Replaced by the day of the year as a decimal number [001, 366].
%k Replaced by the hour of day (24-hour clock) as a decimal number [1,
23]; a single digit is preceded by a space.
*This conversion specification is an extension to the "IEEE Std
1003.1"
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.
html>*.
%l Replaced by the hour of day (12-hour clock) as a decimal number [1,
12]; a single digit is preceded by a space.
*This conversion specification is an extension to the "IEEE Std
1003.1"
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.
html>*.
%m Replaced by the month of the year as a decimal number [01, 12].
%M Replaced by the minute of hour as a decimal number [00, 59].
%n Replaced by a <newline> character.
%N Replaced by the fractional second as a decimal number. This
conversion specification permits use of an optional maximum field
width [0, 9] where the default field width of 0 will use the
shortest representation.
Example:
%N or %0N is replaced by one of the following (shortest
representation):
fff (millisecond)
ffffff (microsecond)
fffffffff (nanosecond)
%N4 is replaced by exactly four fractional digits (zero-padded on
the right or truncated if needed):
ffff
*This conversion specification is an extension to the "IEEE Std
1003.1"
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.
html>*.
%p Replaced by the C locale's meridian notation. Example: AM, PM.
%r Replaced by the C locale's time in a.m. and p.m. notation.
Equivalent to "%I:%M:%S %p".
%R Replaced by the time in 24-hour notation. Equivalent to "%H:%M".
%s Replaced by the number of seconds from the epoch of
1970-01-01T00:00:00Z as a decimal number.
*This conversion specification is an extension to the "IEEE Std
1003.1"
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.
html>*.
%S Replaced by the second of hour as a decimal number [00, 60].
%t Replaced by a <tab> character.
%T Replaced by the time of day. Equivalent to "%H:%M:%S".
%u Replaced by the day of the week as a decimal number [1, 7], with 1
representing Monday.
%U Replaced by the week number of the year as a decimal number [00,
53]. The first Sunday of January is the first day of week 1; days in
the new year before this are in week 0.
%V Replaced by the week number of the year (Monday as the first day of
the week) as a decimal number [01, 53]. If the week containing 1
January has four or more days in the new year, then it is considered
week 1. Otherwise, it is the last week of the previous year, and the
next week is week 1. Both January 4th and the first Thursday of
January are always in week 1.
%W Replaced by the week number of the year as a decimal number [00,
53]. The first Monday of January is the first day of week 1; days in
the new year before this are in week 0.
%x Replaced by the C locale's date representation. Equivalent to
"%m/%d/%y".
%X Replaced by the C locale's time representation. Equivalent to
"%H:%M:%S".
%y Replaced by the last two digits of the year as a decimal number [00,
99].
%Y Replaced by the year as a decimal number [0001, 9999].
%z Replaced by the offset from UTC in the ISO 8601 basic format
(±hhmm).
%Z Replaced by the offset from UTC in the ISO 8601 extended format or
by UTC designator (±hh:mm or Z).
"%%"
Replaced by %.
utc_rd_values
($rd, $sod, $nanosecond) = $tm->utc_rd_values;
Returns a list of three elements:
$rd The number of days from the Rata Die epoch of 0001-01-01.
$sod
The second of the day [0, 86_399].
$nanosecond
The nano of the second [0, 999_999_999].
utc_rd_as_seconds
$seconds = $tm->utc_rd_as_seconds;
Returns the number of seconds from the Rata Die epoch of
0001-01-01T00:00:00Z.
local_rd_values
($rd, $sod, $nanosecond) = $tm->local_rd_values;
Returns a list of three elements:
$rd The number of days from the Rata Die epoch of 0001-01-01.
$sod
The second of the day [0, 86_399].
$nanosecond
The nano of the second [0, 999_999_999].
local_rd_as_seconds
$seconds = $tm->local_rd_as_seconds;
Returns the number of seconds from the Rata Die epoch of
0001-01-01T00:00:00.
OVERLOADED OPERATORS
stringification
$string = "$tm";
The $string will be in one of the following representations:
YYYY-MM-DDThh:mm:ss
YYYY-MM-DDThh:mm:ss.fff
YYYY-MM-DDThh:mm:ss.ffffff
YYYY-MM-DDThh:mm:ss.fffffffff
Followed by a zone designator in one of the following formats:
Z
±hh:mm
The shortest representation will be used. This representation is
conformant with ISO 8601 profiles, such as:
* RFC 3339 Date and Time on the Internet: Timestamps
<http://tools.ietf.org/html/rfc3339>
* RFC 4287 The Atom Syndication Format
<http://tools.ietf.org/html/rfc4287#section-3.3>
* W3C Date and Time Formats <http://www.w3.org/TR/NOTE-datetime>
* HTML5
<http://www.w3.org/TR/html5/infrastructure.html#global-dates-and-tim
es>
* XML Schema <http://www.w3.org/TR/xmlschema-2/#dateTime>
The "to_string" method or the "strftime" format string "%FT%T%f%Z"
produces an equivalent string representation:
"$tm" eq $tm->to_string;
"$tm" eq $tm->strftime("%FT%T%f%Z");
comparison
$boolean = $tm1 == $tm2;
$boolean = $tm1 != $tm2;
$boolean = $tm1 < $tm2;
$boolean = $tm1 > $tm2;
$boolean = $tm1 <= $tm2;
$boolean = $tm1 >= $tm2;
SERIALIZATION
Storable
The serialized representation of a "Time::Moment" is a string of 16
bytes that contains the MAGIC (2 bytes), time zone offset from UTC (2
bytes), the number of days from Rata Die (4 bytes), second of the day (4
bytes) followed by nanosecond of the second (4 bytes).
The total size of the serialized "Time::Moment" instance using "nfreeze"
is 30 bytes.
JSON
"Time::Moment" implements a "TO_JSON" method that returns the
stringified representation of the instance.
CBOR
"Time::Moment" implements a "TO_CBOR" method that stores the stringified
representation of the instance as a *standard date/time string* using
tag 0.
See CBOR::XS, RFC 7049 Section 2.4.1
<http://tools.ietf.org/html/rfc7049#section-2.4.1> and "eg/cbor.pl" for
an example how to roundtrip instances of "Time::Moment".
DIAGNOSTICS
(F) Usage: %s
Method called with wrong number of arguments.
(F) Parameter 'seconds' is out of supported range
Seconds since the epoch of 1970-01-01T:00:00:00Z (0) is out of the
range:
[ -62135596800 (0001-01-01T00:00:00Z),
253402300799 (9999-12-31T23:59:59Z) ]
(F) Parameter 'nanosecond' is out of the range [0, 999_999_999]
(F) Parameter 'offset' is out of the range [-1080, 1080]
(F) Cannot coerce object of type %s to Time::Moment
(F) Cannot parse the given string
(F) A %s object can only be compared to another %s object ('%s', '%s')
SEE ALSO
SUPPORT
Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker at
<https://github.com/chansen/p5-time-moment/issues>. You will be notified
automatically of any progress on your issue.
SOURCE CODE
This is open source software. The code repository is available for
public review and contribution under the terms of the license.
<https://github.com/chansen/p5-time-moment>
git clone https://github.com/chansen/p5-time-moment
AUTHOR
Christian Hansen "chansen@cpan.org"
COPYRIGHT
Copyright 2013 by Christian Hansen.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.