line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Faker::DateTime; |
2
|
2
|
|
|
2
|
|
13810
|
use strict; |
|
2
|
|
|
2
|
|
3
|
|
|
2
|
|
|
1
|
|
49
|
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
46
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
3
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
2
|
|
2
|
|
|
2
|
|
|
1
|
|
53
|
|
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
59
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
4
|
2
|
|
|
2
|
|
6
|
use vars qw($VERSION); $VERSION = '0.10_04'; |
|
2
|
|
|
2
|
|
3
|
|
|
2
|
|
|
1
|
|
84
|
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
79
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
5
|
2
|
|
|
2
|
|
6
|
use base 'Data::Faker'; |
|
2
|
|
|
2
|
|
2
|
|
|
2
|
|
|
1
|
|
393
|
|
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
58
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
6
|
2
|
|
|
2
|
|
812
|
use POSIX; |
|
2
|
|
|
2
|
|
10000
|
|
|
2
|
|
|
1
|
|
8
|
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Data::Faker::DateTime - Data::Faker plugin |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS AND USAGE |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
See L |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 DATA PROVIDERS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=over 4 |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=item unixtime |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Return a unix time (seconds since the epoch) for a random time between the |
23
|
|
|
|
|
|
|
epoch and now. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
__PACKAGE__->register_plugin('unixtime' => sub { int(rand(time())) }); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=item date |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Return a random date as a string, using a random date format (see date_format). |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
36
|
|
|
|
|
|
|
'date' => sub { timestr(shift()->date_format) }, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=item time |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Return a random time as a string, using a random time format (see time_format). |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
46
|
|
|
|
|
|
|
'time' => sub { timestr(shift()->time_format) }, |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item rfc822 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Return an RFC 822 formatted random date. This method may not work on systems |
52
|
|
|
|
|
|
|
using a non-GNU strftime implementation (kindly let me know if that is the |
53
|
|
|
|
|
|
|
case.) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
58
|
|
|
|
|
|
|
'rfc822' => sub { timestr('%a, %d %b %Y %H:%M:%S %z') }, |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item ampm |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Returns am or pm randomly (in the current locale) using one of the formats |
64
|
|
|
|
|
|
|
specified in ampm_format. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
69
|
|
|
|
|
|
|
'ampm' => sub { timestr(shift()->ampm_format) }, |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item time_format |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Return a random time format. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
79
|
|
|
|
|
|
|
'time_format' => [qw(%R %r %T)], |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item date_format |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Return a random date format. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
89
|
|
|
|
|
|
|
'date_format' => [qw(%D %F)], |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item ampm_format |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Return a random am/pm format. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
99
|
|
|
|
|
|
|
'ampm_format' => [qw(%p %P)], |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item datetime_format |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Return a random date and time format. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
109
|
|
|
|
|
|
|
'datetime_format' => ['%c','%+','%FT%H','%FT%I','%F %H','%F %I'], |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item month |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Return a random month name, unabbreviated, in the current locale. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
119
|
|
|
|
|
|
|
'month' => sub { timestr('%B') }, |
120
|
|
|
|
|
|
|
); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item month_abbr |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Return a random month name, abbreviated, in the current locale. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
129
|
|
|
|
|
|
|
'month_abbr' => sub { timestr('%b') }, |
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item weekday |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Return a random weekday name, unabbreviated, in the current locale. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
139
|
|
|
|
|
|
|
'weekday' => sub { timestr('%A') }, |
140
|
|
|
|
|
|
|
); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item weekday_abbr |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Return a random weekday name, abbreviated, in the current locale. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
149
|
|
|
|
|
|
|
'weekday_abbr' => sub { timestr('%a') }, |
150
|
|
|
|
|
|
|
); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item sqldate |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Return a random date in the ISO8601 format commonly used by SQL servers |
155
|
|
|
|
|
|
|
(YYYY-MM-DD). |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
160
|
|
|
|
|
|
|
'sqldate' => sub { timestr('%F') }, |
161
|
|
|
|
|
|
|
); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item datetime_locale |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Return a datetime string in the preferred date representation for the |
166
|
|
|
|
|
|
|
current locale, for a random date. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
171
|
|
|
|
|
|
|
'datetime_locale' => sub { timestr('%c') }, |
172
|
|
|
|
|
|
|
); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item date_locale |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Return a date string in the preferred date representation for the |
177
|
|
|
|
|
|
|
current locale, for a random date. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
182
|
|
|
|
|
|
|
'date_locale' => sub { timestr('%x') }, |
183
|
|
|
|
|
|
|
); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item time_locale |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Return a time string in the preferred date representation for the |
188
|
|
|
|
|
|
|
current locale, for a random date. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
193
|
|
|
|
|
|
|
'time_locale' => sub { timestr('%X') }, |
194
|
|
|
|
|
|
|
); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item century |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Return a random century number. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
203
|
|
|
|
|
|
|
'century' => sub { timestr('%C') }, |
204
|
|
|
|
|
|
|
); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item dayofmonth |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Return a random day of the month. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
213
|
|
|
|
|
|
|
'dayofmonth' => sub { timestr('%d') }, |
214
|
|
|
|
|
|
|
); |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=back |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 UTILITY METHODS |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=over 4 |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item Data::Faker::DateTime::timestr($format); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Given a strftime format specifier, this method passes it through to |
225
|
|
|
|
|
|
|
L along with a random date to display in that format. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Perl passes this through to the strftime function of your system library, so |
228
|
|
|
|
|
|
|
it is possible that some of the formatting tokens used here will not work on |
229
|
|
|
|
|
|
|
your system. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
{ |
234
|
|
|
|
|
|
|
# timestr here redefines the one from Benchmark, which is only loaded for tests. |
235
|
2
|
|
|
2
|
|
4650
|
no warnings 'redefine'; |
|
2
|
|
|
2
|
|
3
|
|
|
2
|
|
|
1
|
|
193
|
|
|
2
|
|
|
|
|
5265
|
|
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
201
|
|
|
1
|
|
|
|
|
1199
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
88
|
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub timestr { |
238
|
14
|
|
|
14
|
1
|
19
|
my $format = shift; |
|
14
|
|
|
14
|
1
|
18
|
|
239
|
14
|
50
|
|
|
|
25
|
if(ref($format)) { $format = shift } |
|
0
|
50
|
|
|
|
0
|
|
|
14
|
|
|
|
|
22
|
|
|
0
|
|
|
|
|
0
|
|
240
|
14
|
|
|
|
|
72
|
POSIX::strftime($format, localtime(__PACKAGE__->unixtime)); |
|
14
|
|
|
|
|
67
|
|
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=back |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head1 NOTES AND CAVEATS |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=over 4 |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=item Be careful building timestamps from pieces |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Be very careful about building date/time representations in formats that |
253
|
|
|
|
|
|
|
are not already listed here. For example if you wanted to get a date that |
254
|
|
|
|
|
|
|
consists of just the month and day, you should NOT do this: |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
my $faker = Data::Faker->new(); |
257
|
|
|
|
|
|
|
print join(' ',$faker->month,$faker->dayofmonth)."\n"; |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
This is bad because you might end up with 'February 31' for example. Instead |
260
|
|
|
|
|
|
|
you should use the timestr utility function to provide you a formatted time |
261
|
|
|
|
|
|
|
for a valid date, or better still, write a plugin function that does it: |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
my $faker = Data::Faker->new(); |
264
|
|
|
|
|
|
|
print $faker->my_short_date()."\n"; |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
package Data::Faker::MyExtras; |
267
|
|
|
|
|
|
|
use base qw(Data::Faker); |
268
|
|
|
|
|
|
|
use Data::Faker::DateTime; |
269
|
|
|
|
|
|
|
__PACKAGE__->register_plugin( |
270
|
|
|
|
|
|
|
my_short_date => sub { Data::Faker::DateTime::timestr('%M %e') }, |
271
|
|
|
|
|
|
|
); |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=item POSIX::strftime |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
See the documentation above regarding the timestr utility method for some |
276
|
|
|
|
|
|
|
caveats related to strftime and your system library. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=back |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 SEE ALSO |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
L |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head1 AUTHOR |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Jason Kohles, Eemail@jasonkohles.comE |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Copyright 2004-2005 by Jason Kohles |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
293
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=cut |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
1; |