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