-
Notifications
You must be signed in to change notification settings - Fork 362
Full Timezone support when parsing ical #408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seejohnrun
wants to merge
16
commits into
master
Choose a base branch
from
full_tz
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8db090e
Parse time zones when creating schedules from ICAL; fix DST test bug …
barelyknown f754169
Add correct offset without changing time
johnhamelink f03e439
Add spec which proves hour doesn't change when timezone is set
johnhamelink f62f55f
Merge pull request #1 from johnhamelink/from_ical_time_zone
barelyknown 7bd414a
Merge remote-tracking branch 'upstream/master' into from_ical_time_zone
dcosson 237e7a9
Use real Timezones in ical serialization, instead of US timezone offsets
dcosson a8f222b
add a test for daylight savings
dcosson 9db2104
add test that the start_time object when parsing UTC is a simple Time
dcosson f5b0025
update test of DST cutover
dcosson ccfe6d1
Merge branch 'fix_timezones' of github.com:dcosson/ice_cube into full_tz
seejohnrun 496fa59
Fix tests
seejohnrun 1072173
AS conditional
seejohnrun 89c45b4
Merge branch 'master' into full_tz
seejohnrun 67f0266
Clean up a bit
seejohnrun 10ed1e4
Use deserialize_time
seejohnrun 602f9f7
Merge branch 'master' into full_tz
seejohnrun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,102 @@ | ||
module IceCube | ||
class IcalParser | ||
def self.schedule_from_ical(ical_string, options = {}) | ||
data = {} | ||
ical_string.each_line do |line| | ||
(property, value) = line.split(':') | ||
(property, tzid) = property.split(';') | ||
case property | ||
when 'DTSTART' | ||
data[:start_time] = Time.parse(value) | ||
when 'DTEND' | ||
data[:end_time] = Time.parse(value) | ||
when 'EXDATE' | ||
data[:extimes] ||= [] | ||
data[:extimes] += value.split(',').map{|v| Time.parse(v)} | ||
when 'DURATION' | ||
data[:duration] # FIXME | ||
when 'RRULE' | ||
data[:rrules] ||= [] | ||
data[:rrules] += [rule_from_ical(value)] | ||
class << self | ||
def schedule_from_ical(ical_string, options = {}) | ||
data = {} | ||
ical_string.each_line do |line| | ||
(property, value) = line.split(':') | ||
(property, tzid) = property.split(';') | ||
case property | ||
when 'DTSTART' | ||
data[:start_time] = parse_in_tzid(value, tzid) | ||
when 'DTEND' | ||
data[:end_time] = parse_in_tzid(value, tzid) | ||
when 'RDATE' | ||
date[:rtimes] ||= [] | ||
data[:rtimes] += value.split(',').map do |v| | ||
parse_in_tzid(v, tzid) | ||
end | ||
when 'EXDATE' | ||
data[:extimes] ||= [] | ||
data[:extimes] += value.split(',').map do |v| | ||
parse_in_tzid(v, tzid) | ||
end | ||
when 'DURATION' | ||
data[:duration] # FIXME | ||
when 'RRULE' | ||
data[:rrules] ||= [] | ||
data[:rrules] += [rule_from_ical(value)] | ||
end | ||
end | ||
Schedule.from_hash data | ||
end | ||
Schedule.from_hash data | ||
end | ||
|
||
def self.rule_from_ical(ical) | ||
raise ArgumentError, 'empty ical rule' if ical.nil? | ||
def rule_from_ical(ical) | ||
raise ArgumentError, 'empty ical rule' if ical.nil? | ||
|
||
validations = {} | ||
params = {validations: validations, interval: 1} | ||
validations = {} | ||
params = {validations: validations, interval: 1} | ||
|
||
ical.split(';').each do |rule| | ||
(name, value) = rule.split('=') | ||
raise ArgumentError, "Invalid iCal rule component" if value.nil? | ||
value.strip! | ||
case name | ||
when 'FREQ' | ||
params[:rule_type] = "IceCube::#{value[0]}#{value.downcase[1..-1]}Rule" | ||
when 'INTERVAL' | ||
params[:interval] = value.to_i | ||
when 'COUNT' | ||
params[:count] = value.to_i | ||
when 'UNTIL' | ||
params[:until] = Time.parse(value).utc | ||
when 'WKST' | ||
params[:week_start] = TimeUtil.ical_day_to_symbol(value) | ||
when 'BYSECOND' | ||
validations[:second_of_minute] = value.split(',').map(&:to_i) | ||
when 'BYMINUTE' | ||
validations[:minute_of_hour] = value.split(',').map(&:to_i) | ||
when 'BYHOUR' | ||
validations[:hour_of_day] = value.split(',').map(&:to_i) | ||
when 'BYDAY' | ||
dows = {} | ||
days = [] | ||
value.split(',').each do |expr| | ||
day = TimeUtil.ical_day_to_symbol(expr.strip[-2..-1]) | ||
if expr.strip.length > 2 # day with occurence | ||
occ = expr[0..-3].to_i | ||
dows[day].nil? ? dows[day] = [occ] : dows[day].push(occ) | ||
days.delete(TimeUtil.sym_to_wday(day)) | ||
else | ||
days.push TimeUtil.sym_to_wday(day) if dows[day].nil? | ||
ical.split(';').each do |rule| | ||
(name, value) = rule.split('=') | ||
raise ArgumentError, "Invalid iCal rule component" if value.nil? | ||
value.strip! | ||
case name | ||
when 'FREQ' | ||
params[:rule_type] = "IceCube::#{value[0]}#{value.downcase[1..-1]}Rule" | ||
when 'INTERVAL' | ||
params[:interval] = value.to_i | ||
when 'COUNT' | ||
params[:count] = value.to_i | ||
when 'UNTIL' | ||
params[:until] = Time.parse(value).utc | ||
when 'WKST' | ||
params[:week_start] = TimeUtil.ical_day_to_symbol(value) | ||
when 'BYSECOND' | ||
validations[:second_of_minute] = value.split(',').map(&:to_i) | ||
when 'BYMINUTE' | ||
validations[:minute_of_hour] = value.split(',').map(&:to_i) | ||
when 'BYHOUR' | ||
validations[:hour_of_day] = value.split(',').map(&:to_i) | ||
when 'BYDAY' | ||
dows = {} | ||
days = [] | ||
value.split(',').each do |expr| | ||
day = TimeUtil.ical_day_to_symbol(expr.strip[-2..-1]) | ||
if expr.strip.length > 2 # day with occurence | ||
occ = expr[0..-3].to_i | ||
dows[day].nil? ? dows[day] = [occ] : dows[day].push(occ) | ||
days.delete(TimeUtil.sym_to_wday(day)) | ||
else | ||
days.push TimeUtil.sym_to_wday(day) if dows[day].nil? | ||
end | ||
end | ||
validations[:day_of_week] = dows unless dows.empty? | ||
validations[:day] = days unless days.empty? | ||
when 'BYMONTHDAY' | ||
validations[:day_of_month] = value.split(',').map(&:to_i) | ||
when 'BYMONTH' | ||
validations[:month_of_year] = value.split(',').map(&:to_i) | ||
when 'BYYEARDAY' | ||
validations[:day_of_year] = value.split(',').map(&:to_i) | ||
when 'BYSETPOS' | ||
else | ||
validations[name] = nil # invalid type | ||
end | ||
validations[:day_of_week] = dows unless dows.empty? | ||
validations[:day] = days unless days.empty? | ||
when 'BYMONTHDAY' | ||
validations[:day_of_month] = value.split(',').map(&:to_i) | ||
when 'BYMONTH' | ||
validations[:month_of_year] = value.split(',').map(&:to_i) | ||
when 'BYYEARDAY' | ||
validations[:day_of_year] = value.split(',').map(&:to_i) | ||
when 'BYSETPOS' | ||
else | ||
validations[name] = nil # invalid type | ||
end | ||
|
||
Rule.from_hash(params) | ||
end | ||
|
||
Rule.from_hash(params) | ||
private | ||
|
||
def parse_in_tzid(value, tzid) | ||
time_parser = Time | ||
if tzid | ||
time_parser = ActiveSupport::TimeZone.new(tzid.split('=')[1]) || Time | ||
|
||
end | ||
time_parser.parse(value) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.