File: required_timezones.rb

package info (click to toggle)
mhc 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,452 kB
  • sloc: ruby: 12,700; lisp: 7,577; makefile: 70; sh: 68
file content (55 lines) | stat: -rw-r--r-- 1,595 bytes parent folder | download | duplicates (3)
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
module RiCal
  #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
  #
  # RequireTimezones collects the timezones used by a given calendar component or set of calendar components
  # For each timezone we collect it's id, and the earliest and latest times which reference the zone
  class RequiredTimezones #:nodoc:
    
    
    # A required timezone represents a single timezone and the earliest and latest times which reference it.
    class RequiredTimezone #:nodoc:
      
      attr_reader :first_time, :last_time, :timezone
      
      def initialize(tzid)
        @timezone = RiCal::Component::TZInfoTimezone.new(TZInfo::Timezone.get(tzid))
      end
      
      def tzid
        @timezone.identifier
      end
      
      def add_datetime(date_time)
        if @first_time 
          @first_time = date_time if date_time < @first_time
        else
          @first_time = date_time
        end
        if @last_time 
          @last_time = date_time if date_time > @last_time
        else
          @last_time = date_time
        end
      end
    end
    
    def required_timezones
      @required_zones ||= {}
    end
    
    def required_zones
      required_timezones.values
    end
    
    def export_to(export_stream)
      required_zones.each do |z|
        tzinfo_timezone =z.timezone
        tzinfo_timezone.export_local_to(export_stream, z.first_time, z.last_time)
      end
    end
    
    def add_datetime(date_time, tzid)
      (required_timezones[tzid] ||= RequiredTimezone.new(tzid)).add_datetime(date_time)
    end
  end
end