diff options
author | Jonas Smedegaard <dr@jones.dk> | 2024-09-01 01:30:03 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2024-09-01 09:18:49 +0200 |
commit | fac3b4444183ec38924cdf5c2878965b33be3992 (patch) | |
tree | 389569c3933f4d1f09d8f9eb2b84b5198613d5db | |
parent | d019a043a95d5b0eef4c2e7ad3a40553a8b0292e (diff) |
streamline span handling
-rw-r--r-- | lib/Object/Groupware/Event.pm | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/lib/Object/Groupware/Event.pm b/lib/Object/Groupware/Event.pm index 4e8ed8e..270028e 100644 --- a/lib/Object/Groupware/Event.pm +++ b/lib/Object/Groupware/Event.pm @@ -15,12 +15,10 @@ field $log = Log::Any->get_logger; field $entry : param; field $dt_locale; +field $span; -field $start : reader = $entry->start; -field $end : reader = $entry->end; -field $datespan : reader; -field $timespan : reader; -field $time_brief : reader; +field $start : reader = $entry->start; +field $end : reader = $entry->end; field $summary : reader = $entry->summary; field $description : reader = $entry->description || ''; field $location : reader = $entry->_simple_property('location'); @@ -46,28 +44,6 @@ ADJUST { $start->set_time_zone($_); $end->set_time_zone($_) if defined $end; } - $datespan - = ( defined $end - and $end->clone->truncate( to => 'day' ) ne - $start->clone->truncate( to => 'day' ) ) - ? ucfirst( - sprintf '%s - %s', - $start->format_cldr( $dt_locale->date_format_medium() ), - $end->format_cldr( $dt_locale->date_format_medium() ) - ) - : ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) ); - $timespan = ( defined $end and not $entry->all_day ) - ? ucfirst( - sprintf '%s-%s', - $start->format_cldr( $dt_locale->datetime_format_medium() ), - $end->format_cldr( $dt_locale->time_format_medium() ) - ) - : undef; - $time_brief - = $entry->all_day - ? $datespan - : ucfirst( - $start->format_cldr( $dt_locale->datetime_format_medium() ) ); $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m; $price = $1; @@ -102,4 +78,60 @@ ADJUST { method attendees { !!@attendees ? [@attendees] : undef } method attachments { !!@attachments ? [@attachments] : undef } +method span () +{ + return $span + if defined($span); + + return $span = '' + unless $end; + + require DateTime::Span; + $span = DateTime::Span->from_datetimes( start => $start, before => $end ); + + return $span; +} + +method datespan () +{ + return ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) ) + if $entry->all_day; + + return '' + if !$span + or $end->clone->truncate( to => 'day' ) eq + $start->clone->truncate( to => 'day' ); + + return ucfirst( + sprintf '%s - %s', + $start->format_cldr( $dt_locale->date_format_medium() ), + $end->format_cldr( $dt_locale->date_format_medium() ) + ); +} + +method timespan () +{ + return '' + if $span + and $end->clone->truncate( to => 'day' ) ne + $start->clone->truncate( to => 'day' ) + or $self->span; + + return ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) ) + if $entry->all_day; + + return $span = ucfirst( + sprintf '%s-%s', + $start->format_cldr( $dt_locale->datetime_format_medium() ), + $end->format_cldr( $dt_locale->time_format_medium() ) + ); +} + +method time_brief () +{ + return $self->datespan + || ucfirst( + $start->format_cldr( $dt_locale->datetime_format_medium() ) ); +} + 1; |