2 # This file is part of Time::Fuzzy
3 # Copyright (c) 2007 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
14 use Class
::Accessor
::Fast
;
16 use DateTime
::Duration
;
18 use base qw
[ Exporter Class
::Accessor
::Fast
];
19 our @EXPORT = qw
[ fuzzy
];
20 __PACKAGE__
->mk_accessors( qw
[ dt fuzziness
] );
22 our $VERSION = '0.31';
23 our $FUZZINESS = 'medium';
28 # - for high fuzziness
29 my %weektime = ( # define the periods of the week
30 'start of week' => [ 1 ],
31 'middle of week' => [ 2..4 ],
32 'end of week' => [ 5 ],
33 'week-end!' => [ 6,7 ],
35 my @weektime; # a 7-slots array, one for each days
36 { # init @weektime by walking %weektime
37 foreach my $wt ( keys %weektime ) {
38 my $days = $weektime{$wt};
39 $weektime[$_] = $wt for @
$days;
43 # - for medium fuzziness
44 my %daytime = ( # define the periods of the day
45 'night' => [ 0, 1, 2, 3, 4 ],
46 'early morning' => [ 5, 6, 7 ],
47 'morning' => [ 8, 9, 10 ],
48 'noon' => [ 11, 12, 13 ],
49 'afternoon' => [ 14, 15, 16, 17, 18 ],
50 'evening' => [ 19, 20, 21 ],
51 'late evening' => [ 22, 23 ],
53 my @daytime; # a 24-slots array, one for each hour
54 { # init @daytime by walking %daytime
55 foreach my $dt ( keys %daytime ) {
56 my $hours = $daytime{$dt};
57 $daytime[$_] = $dt for @
$hours;
62 my @hourtime = ( # defining the periods of the hour
63 "%s o'clock", 'five past %s', 'ten past %s',
64 'quarter past %s', 'twenty past %s', 'twenty five past %s',
65 'half past %s', 'twenty five to %2$s', 'twenty to %2$s',
66 'quarter to %2$s', 'ten to %2$s', 'five to %2$s',
67 q{%2$s o'clock}, # needed for 58-59
71 qw[ one two three four five six seven eight nine ten eleven noon ],
72 qw[ one two three four five six seven eight nine ten eleven midnight ],
80 my $dt = $_[0] || DateTime->now( time_zone=>'local' );
83 medium => \&_fuzzy_medium,
84 high => \&_fuzzy_high,
86 return $fuzzysub{$FUZZINESS}->($dt);
96 dt => DateTime->now( time_zone=>'local' ),
97 fuzziness => $FUZZINESS,
100 return bless \%params, $pkg;
103 use overload '""' => \&as_str;
108 medium => \&_fuzzy_medium,
109 high => \&_fuzzy_high,
111 return $fuzzysub{$self->fuzziness}->($self->dt);
119 # my $fuz = _fuzzy_low($dt)
121 # Return a fuzzy time defined by $dt. The fuzziness is a bit low, that
122 # is, 5 minutes in this case.
127 my $sector = int( ($dt1->minute + 2) / 5 );
128 my $hour1 = $hours[$dt1->hour];
130 # compute next hour, for 2nd half of the hour.
131 my $dt2 = $dt1 + DateTime::Duration->new(hours=>1);
132 my $hour2 = $hours[$dt2->hour];
134 # midnight or noon don't need o'clock appended.
136 if ($sector==0 && $dt1->hour==0) # 0:01
137 || ($sector==0 && $dt1->hour==12); # 12:02
139 if ($sector==12 && $dt1->hour==23) # 23:58
140 || ($sector==12 && $dt1->hour==11); # 11:59
143 my $fuzzy = sprintf $hourtime[$sector], $hour1, $hour2;
149 # my $fuz = _fuzzy_medium($dt)
151 # Return a fuzzy time defined by $dt. The fuzziness is medium, that
152 # is, around 3 hours in this case.
156 return $daytime[$dt->hour];
161 # my $fuz = _fuzzy_high($dt)
163 # Return a fuzzy time defined by $dt. The fuzziness is high, that
164 # is, around the day in this case.
168 return $weektime[$dt->dow];
177 Time::Fuzzy - Time read like a human, with some fuzziness
186 $Time::Fuzzy::FUZZINESS = 'low'; # or 'high', 'medium' (default)
187 my $fuz = fuzzy( DateTime->new(...) );
189 my $fuzzy = Time::Fuzzy->new;
190 print $fuzzy->as_str;
196 Nobody will ever say "it's 11:57". People just say "it's noon".
198 This Perl module does just the same: it adds some human fuzziness to the
199 way computer deal with time.
201 By default, C<Time::Fuzzy> is using a medium fuzziness factor. You can
202 change that by modifying C<$Time::Fuzzy::FUZZINESS>. The accepted values
203 are C<low>, C<medium> or C<high>.
210 =head2 my $fuzzy = fuzzy( [ $dt ] )
212 Return the fuzzy time defined by C<$dt>, a C<DateTime> object. If no
213 argument, return the (fuzzy) current time.
219 If you prefer, you can use C<Time::Fuzzy> in a OOP style. In that case,
220 the following methods are available.
224 =head2 my $fuzzy = Item::Fuzzy->new( [dt=>$dt, fuzziness=>fuzziness] )
226 This is the constructor. It accepts the following params:
231 =item . dt => $dt: a C<DateTime> object, defaults to current time.
233 =item . fuzziness => $fuzziness: the wanted fuziness, defaults to
234 current C<$Time::Fuzzy::FUZZINESS>.
239 Additionally, the accessors C<dt> and C<fuzziness> are available.
242 =head2 my $str = $fuzzy->as_str()
244 Return the fuzzy string of the current time of the object.
250 Please report any bugs or feature requests to C<< < bug-time-fuzzy at
251 rt.cpan.org> >>, or through the web interface at
252 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Time-Fuzzy>. I will be
253 notified, and then you'll automatically be notified of progress on your
254 bug as I make changes.
260 C<Time::Fuzzy> development takes place on
261 L<http://time-fuzzy.googlecode.com> - feel free to join us.
264 You can also look for information on this module at:
268 =item * AnnoCPAN: Annotated CPAN documentation
270 L<http://annocpan.org/dist/Time-Fuzzy>
274 L<http://cpanratings.perl.org/d/Time-Fuzzy>
276 =item * RT: CPAN's request tracker
278 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Time-Fuzzy>
286 Jerome Quelin, C<< <jquelin at cpan.org> >>
290 =head1 COPYRIGHT & LICENSE
292 Copyright (c) 2007 Jerome Quelin, all rights reserved.
294 This program is free software; you can redistribute it and/or modify
295 it under the same terms as Perl itself.