File: DateTime.pm

package info (click to toggle)
libtypes-datetime-perl 0.002-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: perl: 109; makefile: 2; sh: 1
file content (370 lines) | stat: -rw-r--r-- 8,458 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use 5.008;
use strict;
use warnings;

package Types::DateTime;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.002';

use DateTime;
use DateTime::Duration;
use DateTime::Locale;
use DateTime::TimeZone;

use Module::Runtime qw( use_module );

use Type::Library -base, -declare => qw(
	DateTime Duration TimeZone Locale Now
	DateTimeWithZone DateTimeUTC
);
use Types::Standard qw( Num Str HashRef InstanceOf );
use Type::Utils;

# This stuff for compat with MooseX::Types::DateTime

class_type(DateTime, { class => 'DateTime' });
class_type(Duration, { class => 'DateTime::Duration' });
class_type(TimeZone, { class => 'DateTime::TimeZone' });
declare Locale,
	as InstanceOf['DateTime::Locale::root','DateTime::Locale::FromData'];
enum(Now, ['now']);

coerce DateTime,
	from Num,     q{ 'DateTime'->from_epoch(epoch => $_) },
	from HashRef, q{ exists($_->{epoch}) ? 'DateTime'->from_epoch(%$_) : 'DateTime'->new(%{$_}) },
	from Now,     q{ 'DateTime'->now },
	from InstanceOf['DateTime::Tiny'], q{ $_->DateTime };

coerce Duration,
	from Num,     q{ 'DateTime::Duration'->new(seconds => $_) },
	from HashRef, q{ 'DateTime::Duration'->new(%{$_}) };

coerce TimeZone,
	from Str,     q{ 'DateTime::TimeZone'->new(name => $_) };

coerce Locale,
	from InstanceOf['Locale::Maketext'], q{ 'DateTime::Locale'->load($_->language_tag) },
	from Str,     q{ 'DateTime::Locale'->load($_) };

# Time zone stuff

declare DateTimeWithZone,
	as         DateTime,
	coercion   => 1,  # inherit coercions
	where      {          not($_ ->time_zone->is_floating)   },
	inline_as  { (undef, "not($_\->time_zone->is_floating)") },
	constraint_generator => sub {
		my $zone = TimeZone->assert_coerce(shift);
		sub { $_[0]->time_zone eq $zone };
	},
	coercion_generator => sub {
		my $parent = shift;
		my $child  = shift;
		my $zone   = TimeZone->assert_coerce(shift);
		
		my $c = 'Type::Coercion'->new(type_constraint => $child);
		$c->add_type_coercions(
			$parent->coercibles, sub {
				my $dt = DateTime->coerce($_);
				return $_ unless DateTime->check($dt);
				$dt->set_time_zone($zone);
				return $dt;
			},
		);
		$c;
	};

declare DateTimeUTC, as DateTimeWithZone['UTC'], coercion => 1;

# Stringy coercions. No sugar for this stuff ;-)

__PACKAGE__->meta->add_coercion({
	name               => 'Format',
	type_constraint    => DateTime,
	coercion_generator => sub {
		my ($self, $target, $format) = @_;
		$format = use_module("DateTime::Format::$format")->new
			unless ref($format);
		
		my $timezone;
		if ($target->is_a_type_of(DateTimeWithZone))
		{
			my ($paramd_type) = grep {
				$_->is_parameterized and $_->parent==DateTimeWithZone
			} ($target, $target->parents);
			$timezone = TimeZone->assert_coerce($paramd_type->type_parameter)
				if $paramd_type;
		}
		
		return (
			Str,
			sub {
				my $dt = eval { $format->parse_datetime($_) };
				return $_ unless $dt;
				$dt->set_time_zone($timezone) if $timezone;
				$dt;
			},
		);
	},
});

__PACKAGE__->meta->add_coercion({
	name               => 'Strftime',
	type_constraint    => Str,
	coercion_generator => sub {
		my ($self, $target, $format) = @_;
		my $format_quoted = B::perlstring($format);
		return (
			DateTime->coercibles,
			qq{\$_->strftime($format_quoted)},
		);
	},
});

__PACKAGE__->meta->add_coercion({
	name               => 'ToISO8601',
	type_constraint    => Str,
	type_coercion_map  => [
		DateTime->coercibles,
		q{$_->iso8601},
	],
});

1;

__END__

=pod

=encoding utf-8

=for stopwords datetime timezone

=head1 NAME

Types::DateTime - type constraints and coercions for datetime objects

=head1 SYNOPSIS

   package FroobleGala;
   
   use Moose;
   use Types::DateTime -all;
   
   has start_date => (
      is      => 'ro',
      isa     => DateTimeUTC->plus_coercions( Format['ISO8601'] ),
      coerce  => 1,
   );

=head1 DESCRIPTION

L<Types::DateTime> is a type constraint library suitable for use with
L<Moo>/L<Moose> attributes, L<Kavorka> sub signatures, and so forth.

=head2 Types

This module provides some type constraints broadly compatible with
those provided by L<MooseX::Types::DateTime>, plus a couple of extra
type constraints.

=over

=item C<DateTime>

A class type for L<DateTime>. Coercions from:

=over

=item from C<Num>

Uses L<DateTime/from_epoch>. Floating values will be used for sub-second
precision, see L<DateTime> for details.

=item from C<HashRef>

Calls L<DateTime/new> or L<DateTime/from_epoch> as appropriate, passing
the hash as arguments.

=item from C<Now>

Uses L<DateTime/now>.

=item from C<< InstanceOf['DateTime::Tiny'] >>

Inflated using L<DateTime::Tiny/DateTime>.

=back

=item C<Duration>

A class type for L<DateTime::Duration>. Coercions from:

=over

=item from C<Num>

Uses L<DateTime::Duration/new> and passes the number as the C<seconds>
argument.

=item from C<HashRef>

Calls L<DateTime::Duration/new> with the hash entries as arguments.

=back

=item C<Locale>

A class type for L<DateTime::Locale>. Coercions from:

=over

=item from C<Str>

The string is treated as a language tag (e.g. C<en> or C<he_IL>) and
given to L<DateTime::Locale/load>.

=item from C<< InstanceOf['Locale::Maketext'] >>

The C<Locale::Maketext/language_tag> attribute will be used with
L<DateTime::Locale/load>.

=back

=item C<TimeZone>

A class type for L<DateTime::TimeZone>. Coercions from:

=over

=item from C<Str>

Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE>
for more details on the allowed values.

Delegates to L<DateTime::TimeZone/new> with the string as the C<name>
argument.

=back

=item C<Now>

Type constraint with only one allowed value, the string "now".

This is exported for compatibility with L<MooseX::Types::DateTime>, which
exports such a constraint, even though it is not documented.

=item C<DateTimeWithZone>

A subtype of C<DateTime> for objects with a defined (non-floating) time
zone.

This type constraint inherits its coercions from C<DateTime>.

=item C<< DateTimeWithZone[`a] >>

The C<DateTimeWithZone> type constraint may be parameterized with a
L<DateTime::TimeZone> object, or a string that can be coerced into one.

   has start_date => (
      is      => 'ro',
      isa     => DateTimeWithZone['Europe/London'],
      coerce  => 1,
   );

This type constraint inherits its coercions from C<DateTime>, and will
additionally call L<DateTime/set_time_zone> to shift objects into the
correct timezone.

=item C<DateTimeUTC>

Shortcut for C<< DateTimeWithZone["UTC"] >>.

=back

=head2 Named Coercions

It is hoped that Type::Tiny will help avoid the proliferation of
modules like L<MooseX::Types::DateTimeX>,
L<MooseX::Types::DateTime::ButMaintained>, and
L<MooseX::Types::DateTime::MoreCoercions>. It makes it very easy to add
coercions to a type constraint at the point of use:

   has start_date => (
      is      => 'ro',
      isa     => DateTime->plus_coercions(
         InstanceOf['MyApp::DT'] => sub { $_->to_DateTime }
      ),
      coerce  => 1,
   );

Even easier, this module exports some named coercions.

=over

=item C<< Format[`a] >>

May be passed an object providing a C<parse_datetime> method, or a
class name from the C<< DateTime::Format:: >> namespace (upon which
C<new> will be called).

For example:

   DateTime->plus_coercions( Format['ISO8601'] )

Or:

   DateTimeUTC->plus_coercions(
      Format[
         DateTime::Format::Natural->new(lang => 'en')
      ]
   )

=item C<< Strftime[`a] >>

A pattern for serializing a DateTime object into a string using
L<DateTime/strftime>.

   Str->plus_coercions( Strftime['%a %e %b %Y'] );

=item C<ToISO8601>

A coercion for serializing a DateTime object into a string using
L<DateTime/iso8601>.

   Str->plus_coercions( ToISO8601 );

=back

=head1 BUGS

Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Types-DateTime>.

=head1 SEE ALSO

L<MooseX::Types::DateTime>,
L<Type::Tiny::Manual>,
L<DateTime>,
L<DateTime::Duration>,
L<DateTime::Locale>,
L<DateTime::TimeZone>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2014, 2017 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.