| 1 |
NAME |
| 2 |
Types::DateTime - type constraints and coercions for datetime objects |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
package FroobleGala; |
| 6 |
|
| 7 |
use Moose; |
| 8 |
use Types::DateTime -all; |
| 9 |
|
| 10 |
has start_date => ( |
| 11 |
is => 'ro', |
| 12 |
isa => DateTimeUTC->plus_coercions( Format['ISO8601'] ), |
| 13 |
coerce => 1, |
| 14 |
); |
| 15 |
|
| 16 |
DESCRIPTION |
| 17 |
Types::DateTime is a type constraint library suitable for use with |
| 18 |
Moo/Moose attributes, Kavorka sub signatures, and so forth. |
| 19 |
|
| 20 |
Types |
| 21 |
This module provides some type constraints broadly compatible with those |
| 22 |
provided by MooseX::Types::DateTime, plus a couple of extra type |
| 23 |
constraints. |
| 24 |
|
| 25 |
`DateTime` |
| 26 |
A class type for DateTime. Coercions from: |
| 27 |
|
| 28 |
from `Num` |
| 29 |
Uses "from_epoch" in DateTime. Floating values will be used for |
| 30 |
sub-second precision, see DateTime for details. |
| 31 |
|
| 32 |
from `HashRef` |
| 33 |
Calls "new" in DateTime or "from_epoch" in DateTime as |
| 34 |
appropriate, passing the hash as arguments. |
| 35 |
|
| 36 |
from `Now` |
| 37 |
Uses "now" in DateTime. |
| 38 |
|
| 39 |
from `InstanceOf['DateTime::Tiny']` |
| 40 |
Inflated using "DateTime" in DateTime::Tiny. |
| 41 |
|
| 42 |
`Duration` |
| 43 |
A class type for DateTime::Duration. Coercions from: |
| 44 |
|
| 45 |
from `Num` |
| 46 |
Uses "new" in DateTime::Duration and passes the number as the |
| 47 |
`seconds` argument. |
| 48 |
|
| 49 |
from `HashRef` |
| 50 |
Calls "new" in DateTime::Duration with the hash entries as |
| 51 |
arguments. |
| 52 |
|
| 53 |
`Locale` |
| 54 |
A class type for DateTime::Locale. Coercions from: |
| 55 |
|
| 56 |
from `Str` |
| 57 |
The string is treated as a language tag (e.g. `en` or `he_IL`) and |
| 58 |
given to "load" in DateTime::Locale. |
| 59 |
|
| 60 |
from `InstanceOf['Locale::Maketext']` |
| 61 |
The `Locale::Maketext/language_tag` attribute will be used with |
| 62 |
"load" in DateTime::Locale. |
| 63 |
|
| 64 |
`TimeZone` |
| 65 |
A class type for DateTime::TimeZone. Coercions from: |
| 66 |
|
| 67 |
from `Str` |
| 68 |
Treated as a time zone name or offset. See "USAGE" in |
| 69 |
DateTime::TimeZone for more details on the allowed values. |
| 70 |
|
| 71 |
Delegates to "new" in DateTime::TimeZone with the string as the |
| 72 |
`name` argument. |
| 73 |
|
| 74 |
`Now` |
| 75 |
Type constraint with only one allowed value, the string "now". |
| 76 |
|
| 77 |
This is exported for compatibility with MooseX::Types::DateTime, which |
| 78 |
exports such a constraint, even though it is not documented. |
| 79 |
|
| 80 |
`DateTimeWithZone` |
| 81 |
A subtype of `DateTime` for objects with a defined (non-floating) time |
| 82 |
zone. |
| 83 |
|
| 84 |
This type constraint inherits its coercions from `DateTime`. |
| 85 |
|
| 86 |
`DateTimeWithZone[`a]` |
| 87 |
The `DateTimeWithZone` type constraint may be parameterized with a |
| 88 |
DateTime::TimeZone object, or a string that can be coerced into one. |
| 89 |
|
| 90 |
has start_date => ( |
| 91 |
is => 'ro', |
| 92 |
isa => DateTimeWithZone['Europe/London'], |
| 93 |
coerce => 1, |
| 94 |
); |
| 95 |
|
| 96 |
This type constraint inherits its coercions from `DateTime`, and will |
| 97 |
additionally call "set_time_zone" in DateTime to shift objects into |
| 98 |
the correct timezone. |
| 99 |
|
| 100 |
`DateTimeUTC` |
| 101 |
Shortcut for `DateTimeWithZone["UTC"]`. |
| 102 |
|
| 103 |
Named Coercions |
| 104 |
It is hoped that Type::Tiny will help avoid the proliferation of modules |
| 105 |
like MooseX::Types::DateTimeX, MooseX::Types::DateTime::ButMaintained, and |
| 106 |
MooseX::Types::DateTime::MoreCoercions. It makes it very easy to add |
| 107 |
coercions to a type constraint at the point of use: |
| 108 |
|
| 109 |
has start_date => ( |
| 110 |
is => 'ro', |
| 111 |
isa => DateTime->plus_coercions( |
| 112 |
InstanceOf['MyApp::DT'] => sub { $_->to_DateTime } |
| 113 |
), |
| 114 |
coerce => 1, |
| 115 |
); |
| 116 |
|
| 117 |
Even easier, this module exports some named coercions. |
| 118 |
|
| 119 |
`Format[`a]` |
| 120 |
May be passed an object providing a `parse_datetime` method, or a |
| 121 |
class name from the `DateTime::Format::` namespace (upon which `new` |
| 122 |
will be called). |
| 123 |
|
| 124 |
For example: |
| 125 |
|
| 126 |
DateTime->plus_coercions( Format['ISO8601'] ) |
| 127 |
|
| 128 |
Or: |
| 129 |
|
| 130 |
DateTimeUTC->plus_coercions( |
| 131 |
Format[ |
| 132 |
DateTime::Format::Natural->new(lang => 'en') |
| 133 |
] |
| 134 |
) |
| 135 |
|
| 136 |
`Strftime[`a]` |
| 137 |
A pattern for serializing a DateTime object into a string using |
| 138 |
"strftime" in DateTime. |
| 139 |
|
| 140 |
Str->plus_coercions( Strftime['%a %e %b %Y'] ); |
| 141 |
|
| 142 |
`ToISO8601` |
| 143 |
A coercion for serializing a DateTime object into a string using |
| 144 |
"iso8601" in DateTime. |
| 145 |
|
| 146 |
Str->plus_coercions( ToISO8601 ); |
| 147 |
|
| 148 |
BUGS |
| 149 |
Please report any bugs to |
| 150 |
<http://rt.cpan.org/Dist/Display.html?Queue=Types-DateTime>. |
| 151 |
|
| 152 |
SEE ALSO |
| 153 |
MooseX::Types::DateTime, Type::Tiny::Manual, DateTime, DateTime::Duration, |
| 154 |
DateTime::Locale, DateTime::TimeZone. |
| 155 |
|
| 156 |
AUTHOR |
| 157 |
Toby Inkster <[email protected]>. |
| 158 |
|
| 159 |
COPYRIGHT AND LICENCE |
| 160 |
This software is copyright (c) 2014, 2017 by Toby Inkster. |
| 161 |
|
| 162 |
This is free software; you can redistribute it and/or modify it under the |
| 163 |
same terms as the Perl 5 programming language system itself. |
| 164 |
|
| 165 |
DISCLAIMER OF WARRANTIES |
| 166 |
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 167 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 168 |
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 169 |
|