time-zone 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f87514b362f81ee4964adead9a93cd19994225c85e06b8c3584caabe8102123
4
- data.tar.gz: 5a0f736d97c9ad2093d9069a22a043b96e1d91ec53c833b7f8a60ff87e332e3e
3
+ metadata.gz: c45f75eb9cc62be35e1a2ab9a07932bcd26a82fe3b256f6da69a4d8fe436d961
4
+ data.tar.gz: 541336a54e240c1b6f65955f764c08adaaa4a772eeb73fb7c5e38ef1e10f0072
5
5
  SHA512:
6
- metadata.gz: 79db731b27df02f4a817709403dd0450f780697f47818fb6cd77e07c893509c25a436c2dbd8c2c257191a14f075bee4a3fc3f162ba7b10d532446b2e347a2544
7
- data.tar.gz: 2c65c7d6ff558e6b00f3629dff240eb0ae0dd2e99dbd985f99a5d7e678962c7f9b7d96b5ddd338e12fa84592ecb953f17e60fd8910962743dfb9ab2f43d7c3ef
6
+ metadata.gz: d6e8d0229d3a43912e324ddb4c9cf312d9384af9af7c3e76bd19631a1eb0ca9ce40c284cace46cd70324746fc421192d2f8974aee617254500e2443580325164
7
+ data.tar.gz: 6063d30135ae3c588bf10e62891ce1a10b0fd875d26de7052953bcf2035b167ac8fca9c8fe8fbb0c7dad945c2684851f63ae397ad8982c18674cb3c6b8ee0454
@@ -1,9 +1,12 @@
1
1
  # Setup the environment:
2
2
  language: ruby
3
- sudo: false
4
- dist: trusty
3
+ dist: xenial
5
4
  cache: bundler
6
5
 
6
+ before_script:
7
+ - gem update --system
8
+ - gem install bundler
9
+
7
10
  rvm:
8
11
  - 2.1
9
12
  - 2.2
data/Rakefile CHANGED
@@ -8,6 +8,7 @@ task :default => :spec
8
8
  task :console do
9
9
  require 'pry'
10
10
  require_relative 'lib/time/zone'
11
+ require_relative 'lib/time/zone/timestamp'
11
12
 
12
13
  Pry.start
13
14
  end
@@ -22,8 +22,19 @@ require_relative 'zone/version'
22
22
 
23
23
  require_relative 'zone/locking'
24
24
 
25
+ require 'time'
26
+
25
27
  class Time
28
+ alias minute min
29
+ alias second sec
30
+
26
31
  module Zone
32
+ def self.new(year, month, day, hour, minute, second, zone)
33
+ with(zone) do
34
+ return Time.new(year, month, day, hour, minute, second).localtime
35
+ end
36
+ end
37
+
27
38
  def self.now(zone)
28
39
  with(zone) do
29
40
  # Time instances are lazy initialized, so we need to force it to pick up the current TZ by invoking #localtime
@@ -33,7 +44,17 @@ class Time
33
44
 
34
45
  def self.convert(time, zone)
35
46
  with(zone) do
36
- return time.localtime
47
+ return Time.new(time.year, time.month, time.day, time.hour, time.minute, time.second, time.utc_offset).localtime
48
+ end
49
+ end
50
+
51
+ def self.parse(string, zone = "UTC", *args)
52
+ if string.sub!(/\s([a-zA-Z][\w]*(\/[\w]+)?)$/, "")
53
+ zone = $1
54
+ end
55
+
56
+ with(zone) do
57
+ return Time.parse(string, *args).localtime, zone
37
58
  end
38
59
  end
39
60
  end
@@ -24,6 +24,18 @@ class Time
24
24
  module Zone
25
25
  LOCK = Mutex.new
26
26
 
27
+ def self.zone=(zone)
28
+ LOCK.synchronize do
29
+ ENV['TZ'] = zone
30
+ end
31
+ end
32
+
33
+ def self.zone(zone)
34
+ LOCK.synchronize do
35
+ ENV['TZ']
36
+ end
37
+ end
38
+
27
39
  def self.with(zone)
28
40
  LOCK.synchronize do
29
41
  original_zone = ENV['TZ']
@@ -0,0 +1,152 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative '../zone'
22
+
23
+ require 'date'
24
+
25
+ class Time
26
+ module Zone
27
+ class Timestamp
28
+ def self.load(string)
29
+ self.parse(string)
30
+ end
31
+
32
+ def self.dump(time)
33
+ time.strftime
34
+ end
35
+
36
+ def self.now(zone)
37
+ self.from(Zone.now(zone), zone)
38
+ end
39
+
40
+ def self.from(time, zone)
41
+ self.new(time.year, time.month, time.day, time.hour, time.min, time.sec, zone)
42
+ end
43
+
44
+ def self.parse(*args)
45
+ self.from(*Zone.parse(*args))
46
+ end
47
+
48
+ def initialize(year, month, day, hour, minute, second, zone = "UTC")
49
+ @year = year
50
+ @month = month
51
+ @day = day
52
+ @hour = hour
53
+ @minute = minute
54
+ @second = second
55
+ @zone = zone
56
+
57
+ @time = nil
58
+ end
59
+
60
+ attr :year
61
+ attr :month
62
+ attr :day
63
+ attr :hour
64
+ attr :minute
65
+ attr :second
66
+ attr :zone
67
+
68
+ def convert(zone)
69
+ self.class.from(Time::Zone.convert(to_time, zone), zone)
70
+ end
71
+
72
+ private def with_offset(months, seconds = 0)
73
+ current = self
74
+
75
+ if months != 0
76
+ current = current.to_datetime >> months
77
+ end
78
+
79
+ if seconds != 0
80
+ current = current.to_time + seconds
81
+ end
82
+
83
+ self.class.from(current, @zone)
84
+ end
85
+
86
+ # Add duration in various units.
87
+ def offset_by(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0)
88
+ with_offset(
89
+ years * 12 + months,
90
+ (((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + seconds
91
+ )
92
+ end
93
+
94
+ # Add duration in seconds.
95
+ def + duration
96
+ with_offset(0, duration)
97
+ end
98
+
99
+ # Return difference in seconds.
100
+ def - other
101
+ if other.is_a? Numeric
102
+ # We are subtracting a duration:
103
+ with_offset(0, -other)
104
+ else
105
+ to_time - other.to_time
106
+ end
107
+ end
108
+
109
+ def to_time
110
+ @time ||= Time::Zone.new(@year, @month, @day, @hour, @minute, @second, @zone).freeze
111
+ end
112
+
113
+ def to_date
114
+ to_time.to_date
115
+ end
116
+
117
+ def to_datetime
118
+ to_time.to_datetime
119
+ end
120
+
121
+ def iso8601
122
+ to_time.iso8601
123
+ end
124
+
125
+ def <=> other
126
+ to_time <=> other.to_time
127
+ end
128
+
129
+ DEFAULT_FORMAT = '%Y-%m-%d %H:%M:%S %Z'.freeze
130
+
131
+ def to_s(format = relative_format)
132
+ to_time.strftime(format.gsub('%Z', @zone))
133
+ end
134
+
135
+ def strftime(format = DEFAULT_FORMAT)
136
+ to_s(format)
137
+ end
138
+
139
+ def relative_format(now = Timestamp.now(@zone))
140
+ if self.year != now.year
141
+ "%-l:%M%P, %B %-d, %Y, %Z"
142
+ elsif self.month != now.month or self.day != now.day
143
+ "%-l:%M%P, %B %-d, %Z"
144
+ else
145
+ "%-l:%M%P, %Z"
146
+ end
147
+ end
148
+
149
+ alias to_str strftime
150
+ end
151
+ end
152
+ end
@@ -20,6 +20,6 @@
20
20
 
21
21
  class Time
22
22
  module Zone
23
- VERSION = "0.2.0"
23
+ VERSION = "1.0.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time-zone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-25 00:00:00.000000000 Z
11
+ date: 2018-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,6 +67,7 @@ files:
67
67
  - Rakefile
68
68
  - lib/time/zone.rb
69
69
  - lib/time/zone/locking.rb
70
+ - lib/time/zone/timestamp.rb
70
71
  - lib/time/zone/version.rb
71
72
  - time-zone.gemspec
72
73
  homepage: https://github.com/ioquatix/time-zone