ice_cube 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ice_cube/time_util.rb +80 -76
- data/lib/ice_cube/validations/day_of_week.rb +4 -1
- data/lib/ice_cube/version.rb +1 -1
- metadata +4 -4
data/lib/ice_cube/time_util.rb
CHANGED
@@ -1,89 +1,93 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
4
|
-
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
1
|
+
module IceCube
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
3
|
+
module TimeUtil
|
4
|
+
|
5
|
+
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
6
|
+
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
7
|
+
|
8
|
+
# this method exists because ActiveSupport will serialize
|
9
|
+
# TimeWithZone's in collections in UTC time instead of
|
10
|
+
# their local time. if +time+ is a TimeWithZone, we move
|
11
|
+
# it to a DateTime
|
12
|
+
# Note: When converting to datetime, you microseconds get set to 0
|
13
|
+
def self.serializable_time(time)
|
14
|
+
if time.respond_to?(:to_datetime)
|
15
|
+
time.to_datetime
|
16
|
+
else
|
17
|
+
time
|
18
|
+
end
|
16
19
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
def self.date_in_n_months(date, month_distance)
|
21
|
-
|
22
|
-
next_mark = date
|
23
|
-
days_in_month_of_next_mark = days_in_month(next_mark)
|
24
|
-
|
25
|
-
month_distance.times do
|
20
|
+
|
21
|
+
# TODO can we improve this more?
|
22
|
+
def self.date_in_n_months(date, month_distance)
|
26
23
|
|
27
|
-
|
28
|
-
|
24
|
+
next_mark = date
|
25
|
+
days_in_month_of_next_mark = days_in_month(next_mark)
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
27
|
+
month_distance.times do
|
28
|
+
|
29
|
+
prev_mark = next_mark
|
30
|
+
next_mark += days_in_month_of_next_mark * IceCube::ONE_DAY
|
31
|
+
|
32
|
+
# only moving one day at a time, so this suffices
|
33
|
+
months_covered = next_mark.month - prev_mark.month
|
34
|
+
months_covered += 12 if months_covered < 0
|
35
|
+
|
36
|
+
# step back to the end of the previous month of months_covered went too far
|
37
|
+
if months_covered == 2
|
38
|
+
next_mark -= next_mark.mday * IceCube::ONE_DAY
|
39
|
+
end
|
40
|
+
|
41
|
+
days_in_month_of_next_mark = days_in_month(next_mark)
|
42
|
+
next_mark = adjust(next_mark, prev_mark)
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
# at the end, there's a chance we're not on the correct day,
|
47
|
+
# but if we're not - we will always be behind it in the correct month
|
48
|
+
# if there exists no proper day in the month for us, return nil - otherwise, return that date
|
49
|
+
|
50
|
+
if days_in_month_of_next_mark >= date.mday
|
51
|
+
next_mark += (date.mday - next_mark.mday) * IceCube::ONE_DAY
|
37
52
|
end
|
38
|
-
|
39
|
-
days_in_month_of_next_mark = days_in_month(next_mark)
|
40
|
-
next_mark = adjust(next_mark, prev_mark)
|
41
53
|
|
42
54
|
end
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
if days_in_month_of_next_mark >= date.mday
|
49
|
-
next_mark += (date.mday - next_mark.mday) * ONE_DAY
|
55
|
+
|
56
|
+
def adjust(goal, date)
|
57
|
+
return goal if goal.utc_offset == date.utc_offset
|
58
|
+
goal - goal.utc_offset + date.utc_offset
|
50
59
|
end
|
51
60
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
def self.is_leap?(year)
|
60
|
-
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.days_in_year(date)
|
64
|
-
is_leap?(date.year) ? 366 : 365
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.days_in_month(date)
|
68
|
-
is_leap?(date.year) ? LeapYearMonthDays[date.month - 1] : CommonYearMonthDays[date.month - 1]
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.ical_format(time)
|
72
|
-
if time.utc?
|
73
|
-
":#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time
|
74
|
-
else
|
75
|
-
";TZID=#{time.strftime('%Z:%Y%m%dT%H%M%S')}" # local time specified
|
61
|
+
def self.is_leap?(year)
|
62
|
+
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.days_in_year(date)
|
66
|
+
is_leap?(date.year) ? 366 : 365
|
76
67
|
end
|
68
|
+
|
69
|
+
def self.days_in_month(date)
|
70
|
+
is_leap?(date.year) ? LeapYearMonthDays[date.month - 1] : CommonYearMonthDays[date.month - 1]
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.ical_format(time)
|
74
|
+
if time.utc?
|
75
|
+
":#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time
|
76
|
+
else
|
77
|
+
";TZID=#{time.strftime('%Z:%Y%m%dT%H%M%S')}" # local time specified
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.ical_duration(duration)
|
82
|
+
hours = duration / 3600; duration %= 3600
|
83
|
+
minutes = duration / 60; duration %= 60
|
84
|
+
repr = ''
|
85
|
+
repr << "#{hours}H" if hours > 0
|
86
|
+
repr << "#{minutes}M" if minutes > 0
|
87
|
+
repr << "#{duration}S" if duration > 0
|
88
|
+
"PT#{repr}"
|
89
|
+
end
|
90
|
+
|
77
91
|
end
|
78
92
|
|
79
|
-
def self.ical_duration(duration)
|
80
|
-
hours = duration / 3600; duration %= 3600
|
81
|
-
minutes = duration / 60; duration %= 60
|
82
|
-
repr = ''
|
83
|
-
repr << "#{hours}H" if hours > 0
|
84
|
-
repr << "#{minutes}M" if minutes > 0
|
85
|
-
repr << "#{duration}S" if duration > 0
|
86
|
-
"PT#{repr}"
|
87
|
-
end
|
88
|
-
|
89
93
|
end
|
@@ -18,7 +18,10 @@ module IceCube
|
|
18
18
|
@days_of_week[date.wday].include?(nth_occurrence_of_weekday) || @days_of_week[date.wday].include?(nth_occurrence_of_weekday - this_weekday_in_month_count - 1)
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
|
22
|
+
# note - temporary implementation
|
23
|
+
# instead - once we know what weekday starts the month, we should be able to figure out
|
24
|
+
# the rest with basic math
|
22
25
|
def closest(date)
|
23
26
|
return nil if !@days_of_week || @days_of_week.empty?
|
24
27
|
goal = date
|
data/lib/ice_cube/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ice_cube
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 2
|
10
|
+
version: 0.5.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Crepezzi
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-25 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|