summaryrefslogtreecommitdiff
path: root/src/Net-ICal-Libical/lib/Net/ICal/Libical/Duration.pm
blob: 93b1852c4e892ecde865faa2e8e542bd895f7dd2 (plain)
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
#=============================================================================
# FILE: Duration.pm
# CREATOR: eric
#
# (C) COPYRIGHT 2000, Eric Busboom <eric@civicknowledge.com>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of either:
#
#    The LGPL as published by the Free Software Foundation, version
#    2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html
#
# Or:
#
#    The Mozilla Public License Version 2.0. You may obtain a copy of
#    the License at https://www.mozilla.org/MPL/
#=============================================================================

=head1 NAME

Net::ICal::Duration -- represent a length of time

=head1 SYNOPSIS

  use Net::ICal;
  $d = Net::ICal::Duration->new("P3DT6H15M10S");
  $d = Net::ICal::Duration->new(3600); # 1 hour in seconds

=head1 DESCRIPTION

I<Duration> Represents a length of time, such a 3 days, 30 seconds or
7 weeks. You would use this for representing an abstract block of
time; "I want to have a 1-hour meeting sometime." If you want a
calendar- and timezone-specific block of time, see Net::ICal::Period.

=cut

#=============================================================================

package Net::ICal::Libical::Duration;
use Net::ICal::Libical::Property;
use strict;
use Carp;
@Net::ICal::Libical::Duration::ISA = qw ( Net::ICal::Libical::Property );

=head1 METHODS

=head2 new

Create a new I<Duration> from:

=over 4

=item * A string in RFC2445 duration format

=item * An integer representing a number of seconds

=cut

sub new {
  my $package = shift;
  my $arg = shift;
  my $self;

  if (ref($arg) == 'HASH'){
    # Construct from dictionary
    $self = Net::ICal::Libical::Property::new($package,$arg);
    my $val=Net::ICal::Libical::icalproperty_get_value_as_string($self->{'ref'});
    $self->{'dur'} = Net::ICal::Libical::icaldurationtype_from_string($val);

   return $self;

  } elsif ($arg =~ /^[-+]?\d+$/){
    # Seconds
    $self = Net::ICal::Libical::Property::new($package,'DURATION');
    $self->{'dur'} = Net::ICal::Libical::icaldurationtype_new_from_int($arg);
  } elsif ($arg) {
    # iCalendar string
    $self = Net::ICal::Libical::Property::new($package,'DURATION');
    $self->{'dur'} = Net::ICal::Libical::icaldurationtype_new_from_string($arg);
  } else {
    die;
  }

  $self->_update_value();
  return $self;

}

sub _update_value {
  my $self = shift;

  die "Can't find internal icalduration reference" if !$self->{'dur'};

  $self->value(Net::ICal::Libical::icaldurationtype_as_ical_string($self->{'dur'}));

}
=head2 clone()

Return a new copy of the duration.

=cut

sub clone {
  die "Not Implemented";

}


=head2 is_valid()

Determine if this is a valid duration (given criteria TBD).

=cut

sub is_valid {

  die "Not Implemented;"

}

=head2 seconds()

Set or Get the length of the duration as seconds.

=cut

sub seconds {
  my $self = shift;
  my $seconds = shift;

  if($seconds){
    $self->{'dur'} =
    Net::ICal::Libical::icaldurationtype_from_int($seconds);
    $self->_update_value();
  }

  return Net::ICal::Libical::icaldurationtype_as_int($self->{'dur'});

}

=head2 add($duration)

Return a new duration that is the sum of this and $duration. Does not
modify this object.

=cut

sub add {
  my ($self, $duration) = @_;

  return new Duration($self->seconds() + $duration->seconds());
}


=head2 subtract($duration)

Return a new duration that is the difference between this and
$duration. Does not modify this object.

=cut

sub subtract {
  my ($self, $duration) = @_;

  return new Duration($self->seconds() - $duration->seconds());
}

1;