ozone 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -4
- data/lib/ozone/version.rb +1 -1
- data/lib/ozone.rb +50 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6333a6bb89f133a43dcf273dd82f5d7a2de819e6
|
4
|
+
data.tar.gz: 8eeeb170d91b60cc8a6dd40fb580b2469d46f99f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a45fc5be2db9076de08bfc80249dd2daa966cdb65baa6f9b802b36b14e1849a8cdefc07609c7c80855f050238d1c0e98474a5f7fcc2e10e5e1d09fedaea63e8
|
7
|
+
data.tar.gz: 547347a2b563f3191a643f9de83c734f4e6560e15a47ee9da2ecaa827d195998ed22c4b7a643d132c857fa038680eba07a1e806f4a826d5b9f72580ca7595088
|
data/README.md
CHANGED
@@ -3,7 +3,10 @@ Convert time based on offset, with or without dst
|
|
3
3
|
=======
|
4
4
|
# Ozone
|
5
5
|
|
6
|
-
Given an offset (in minutes) and a boolean whether to observe daylight savings,
|
6
|
+
Given a ruby time, an offset (in minutes), and a boolean whether to observe daylight savings, Ozone provides two things:
|
7
|
+
|
8
|
+
1) Convert a ruby time to a string representation, adjusted by offset, and either abiding by or ignoring daylight savings.
|
9
|
+
2) Compare with a ruby time to see whether before or after, either abiding by or ignoring daylight savings.
|
7
10
|
|
8
11
|
## Installation
|
9
12
|
|
@@ -23,16 +26,54 @@ Or install it yourself as:
|
|
23
26
|
|
24
27
|
## Usage
|
25
28
|
|
26
|
-
|
29
|
+
An instance of Ozone::Time can be compared with a ruby time.
|
30
|
+
|
31
|
+
When observes_dst is true, Ozone::Time has the same value as ruby Time when making comparisons.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
dst_time = Time.parse("2014-03-09 10:00:00 UTC")
|
35
|
+
ozone_time = Ozone::Time.new(
|
36
|
+
time: dst_time,
|
37
|
+
offset: -480,
|
38
|
+
observes_dst: true,
|
39
|
+
)
|
40
|
+
|
41
|
+
> ozone_time.before?(1.second.since(dst_time))
|
42
|
+
=> true
|
43
|
+
> ozone_time.before?(1.second.until(dst_time))
|
44
|
+
=> false
|
45
|
+
```
|
46
|
+
|
47
|
+
However, when observes_dst is false:
|
27
48
|
|
28
49
|
```ruby
|
29
|
-
|
50
|
+
ozone_time = Time.new(
|
51
|
+
time: dst_time,
|
52
|
+
offset: -480,
|
53
|
+
observes_dst: false,
|
54
|
+
)
|
55
|
+
|
56
|
+
> ozone_time.before?(1.second.since(dst_time)
|
57
|
+
=> true
|
58
|
+
> ozone_time.before?(1.second.until(dst_time)
|
59
|
+
=> true
|
60
|
+
```
|
61
|
+
|
62
|
+
This is because during daylight savings, times will be adjusted backwards 1 hour if daylight savings
|
63
|
+
is not being observed.
|
64
|
+
|
65
|
+
Ozone::Formatter has one method -- call -- which takes a time, offset (from utc, in minutes), whether to use daylight savings, and a [time format](http://ruby-doc.org/core-2.2.0/Time.html#method-i-strftime). Default format is YYYY-MM-DD HH:MM.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
> Ozone::Formatter.call(time: t, offset: -480, observes_dst: true)
|
30
69
|
=> "2014/07/17 17:30"
|
31
70
|
|
32
|
-
> Ozone.call(time: t, offset: -480, observes_dst: false)
|
71
|
+
> Ozone::Formatter.call(time: t, offset: -480, observes_dst: false)
|
33
72
|
=> "2014/07/17 16:30"
|
34
73
|
```
|
35
74
|
|
75
|
+
This functionality can also be accessed via the Ozone::Time#strftime method.
|
76
|
+
|
36
77
|
## Development
|
37
78
|
|
38
79
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/ozone/version.rb
CHANGED
data/lib/ozone.rb
CHANGED
@@ -3,17 +3,58 @@ require "ozone/version"
|
|
3
3
|
require 'active_support/time'
|
4
4
|
|
5
5
|
module Ozone
|
6
|
-
|
6
|
+
module Formatter
|
7
|
+
module_function
|
7
8
|
|
8
|
-
|
9
|
-
timezone = ActiveSupport::TimeZone[offset/60]
|
10
|
-
time_with_zone = time.in_time_zone(timezone)
|
11
|
-
if time_with_zone.dst? && !observes_dst
|
12
|
-
offset -= 120
|
9
|
+
def call(time:, offset:, observes_dst: true, format: '%Y-%m-%d %H:%M')
|
13
10
|
timezone = ActiveSupport::TimeZone[offset/60]
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
time_with_zone = time.in_time_zone(timezone)
|
12
|
+
if time_with_zone.dst? && !observes_dst
|
13
|
+
# If we're during daylight savings time, but
|
14
|
+
# we are not observing daylight savings,
|
15
|
+
# there is a one-hour window where there is not
|
16
|
+
# an equivalent time between standard and daylight
|
17
|
+
# savings times. In order to deal with this,
|
18
|
+
# we subtract two hours from the offset and then
|
19
|
+
# take 2 hours from the given time. This frees
|
20
|
+
# us to string format the time without ruby
|
21
|
+
# making adjustments internally.
|
22
|
+
offset -= 120
|
23
|
+
timezone = ActiveSupport::TimeZone[offset/60]
|
24
|
+
2.hours.since(time.in_time_zone(timezone)).strftime(format)
|
25
|
+
else
|
26
|
+
time_with_zone.strftime(format)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Time
|
32
|
+
def initialize(time:, offset:, observes_dst: true)
|
33
|
+
@offset = offset
|
34
|
+
@timezone = ActiveSupport::TimeZone[offset/60]
|
35
|
+
@observes_dst = observes_dst
|
36
|
+
@time = time
|
37
|
+
end
|
38
|
+
|
39
|
+
def before?(time_with_zone)
|
40
|
+
adjusted_time < time_with_zone
|
41
|
+
end
|
42
|
+
|
43
|
+
def after?(time_with_zone)
|
44
|
+
!before?(time_with_zone)
|
45
|
+
end
|
46
|
+
|
47
|
+
def strftime(format: '%Y-%m-%d %H:%M')
|
48
|
+
Formatter.call(time: @time, offset: @offset, observes_dst: @observes_dst, format: format)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def adjusted_time
|
54
|
+
time_with_zone = @time.in_time_zone(@timezone)
|
55
|
+
time_with_zone.dst? && !@observes_dst ?
|
56
|
+
1.hour.until(time_with_zone) :
|
57
|
+
time_with_zone
|
17
58
|
end
|
18
59
|
end
|
19
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ozone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hubert Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|