Time::Clock(3pm) | User Contributed Perl Documentation | Time::Clock(3pm) |
Time::Clock - Twenty-four hour clock object with nanosecond precision.
$t = Time::Clock->new(hour => 12, minute => 34, second => 56); print $t->as_string; # 12:34:56 $t->parse('8pm'); print "$t"; # 20:00:00 print $t->format('%I:%M %p'); # 08:00 PM $t->add(minutes => 15, nanoseconds => 123000000); print $t->as_string; # 20:15:00.123 $t->subtract(hours => 30); print $t->as_string; # 14:15:00.123 ...
A Time::Clock object is a twenty-four hour clock with nanosecond precision and wrap-around. It is a clock only; it has absolutely no concept of dates. Vagaries of date/time such as leap seconds and daylight savings time are unsupported.
When a Time::Clock object hits 23:59:59.999999999 and at least one more nanosecond is added, it will wrap around to 00:00:00.000000000. This works in reverse when time is subtracted.
Time::Clock objects automatically stringify to a user-definable format.
Note that this method may also be called as an object method, in which case it sets the default format for the individual object only.
$t = Time::Clock->new(hour => 12, minute => 34, second => 56);
If a single argument is passed to new, it is equivalent to calling the parse method. That is, this:
$t = Time::Clock->new('12:34:56');
is equivalent to this:
$t = Time::Clock->new; $t->parse('12:34:56');
Returns the newly constructed Time::Clock object.
If the amount of time added is large enough, the clock will wrap around from 23:59:59.999999999 to 00:00:00.000000000 as needed.
A clock whose hour is greater than 12 cannot be set to AM. Any attempt to do so will cause a fatal error.
Setting a clock whose hour is less than 12 to PM will cause its hour to be increased by 12. Example:
$t = Time::Clock->new('8:00'); print $t->as_string; # 08:00:00 $t->ampm('PM'); print $t->as_string; # 20:00:00
Return the string "AM" if the hour is less than 12, "PM" otherwise.
Note that this method may also be called as a class method, in which case it sets the default format all objects of this class.
$t = Time::Clock->new('12:34:56'); print $t->format('%H:%M:%S%n'); # 12:34:56 $t->nanosecond(0); print $t->format('%H:%M:%S%n'); # 12:34:56 $t->nanosecond(123000000); print $t->format('%H:%M:%S%n'); # 12:34:56.123
$t = Time::Clock->new('12:34:56'); print $t->format('%H:%M:%S%4n'); # 12:34:56 $t->nanosecond(0); print $t->format('%H:%M:%S%4n'); # 12:34:56.0000 $t->nanosecond(123000000); print $t->format('%H:%M:%S%4n'); # 12:34:56.1230
$t = Time::Clock->new('12:34:56'); print $t->format('%H:%M:%S.%4N'); # 12:34:56.0000 $t->nanosecond(123000000); print $t->format('%H:%M:%S.%6N'); # 12:34:56.123000 $t->nanosecond(123000000); print $t->format('%H:%M:%S.%2N'); # 12:34:56.12
Valid string values contain an hour with optional minutes, seconds, fractional seconds, and AM/PM string. There should be a colon (":") between hours, minutes, and seconds, and a decimal point (".") between the seconds and fractional seconds. Fractional seconds may contain up to 9 digits. The AM/PM string is case-insensitive and may have periods after each letter.
The string "now" will initialize the clock object with the current (local) time. If the Time::HiRes module is installed, this time will have fractional seconds.
A time value with an hour of 24 and zero minutes, seconds, and nanoseconds is also accepted by this method.
Here are some examples of valid time strings:
12:34:56.123456789 12:34:56.123 PM 24:00 8:30pm 6 A.m. now
If the amount of time subtracted is large enough, the clock will wrap around from 00:00:00.000000000 to 23:59:59.999999999 as needed.
John C. Siracusa (siracusa@gmail.com)
Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2018-01-14 | perl v5.26.1 |