| 1 | package Net::CalDAV::Utils 0.01; | ||
| 2 | use 5.020; | ||
| 3 | use experimental 'signatures'; | ||
| 4 | use Exporter 'import'; | ||
| 5 | use POSIX 'strftime'; | ||
| 6 | use Time::Local; | ||
| 7 | |||
| 8 | our @EXPORT = (qw(ts tslocal dtend)); | ||
| 9 | |||
| 10 | sub ts( $time ) { | ||
| 11 | return strftime '%Y-%m-%dT%H:%M:%S', localtime( $time ) | ||
| 12 | } | ||
| 13 | |||
| 14 | sub dtend( $ts, $duration ) { | ||
| 15 | # convert $ts to unix timestamp | ||
| 16 | my @parts = reverse split /\D/, $ts; | ||
| 17 | $parts[4]--; # month is zero-based ... | ||
| 18 | my $time = timelocal( @parts ); | ||
| 19 | |||
| 20 | # convert $duration to seconds | ||
| 21 | $duration =~ s/^PT?// | ||
| 22 | or die "Can't parse a duration from '$duration'"; | ||
| 23 | my @units = ($duration =~ /([+-]?\d+)([DHMS])/g); | ||
| 24 | for my( $d, $unit ) (@units) { | ||
| 25 | # Yeah, this doesn't play nice with DST changes | ||
| 26 | $time += $d * { H => 60*60, M => 60, S => 1, D => 24*60*60, W => 7*24*60*60 }->{$unit}; | ||
| 27 | } | ||
| 28 | return ts( $time ) | ||
| 29 | } | ||
| 30 | |||
| 31 | sub tslocal( $ts ) { | ||
| 32 | my @parts = reverse split /\D/, $ts; | ||
| 33 | $parts[4]--; # month is zero-based ... | ||
| 34 | ts( timegm( @parts )); | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | 1; |