Class: Time::Zone::Timestamp
- Inherits:
-
Object
- Object
- Time::Zone::Timestamp
- Defined in:
- lib/time/zone/timestamp.rb
Constant Summary collapse
- DEFAULT_FORMAT =
'%Y-%m-%d %H:%M:%S %Z'.freeze
Instance Attribute Summary collapse
-
#day ⇒ Object
readonly
Returns the value of attribute day.
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#minute ⇒ Object
readonly
Returns the value of attribute minute.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#second ⇒ Object
readonly
Returns the value of attribute second.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
Class Method Summary collapse
- .dump(time) ⇒ Object
- .from(time, zone) ⇒ Object
- .load(string) ⇒ Object
- .now(zone) ⇒ Object
- .parse(*args) ⇒ Object
Instance Method Summary collapse
-
#+(duration) ⇒ Object
Add duration in seconds.
-
#-(other) ⇒ Object
Return difference in seconds.
- #<=>(other) ⇒ Object
- #convert(zone) ⇒ Object
-
#initialize(year, month, day, hour, minute, second, zone = "UTC") ⇒ Timestamp
constructor
A new instance of Timestamp.
- #iso8601 ⇒ Object
-
#offset_by(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ Object
Add duration in various units.
- #relative_format(now = Timestamp.now(@zone)) ⇒ Object
- #strftime(format = DEFAULT_FORMAT) ⇒ Object (also: #to_str)
- #to_date ⇒ Object
- #to_datetime ⇒ Object
- #to_s(format = relative_format) ⇒ Object
- #to_time ⇒ Object
-
#with_offset(months = 0, seconds = 0, zone = @zone) ⇒ Object
Generate a new time with the specified changes.
Constructor Details
#initialize(year, month, day, hour, minute, second, zone = "UTC") ⇒ Timestamp
Returns a new instance of Timestamp.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/time/zone/timestamp.rb', line 48 def initialize(year, month, day, hour, minute, second, zone = "UTC") @year = year @month = month @day = day @hour = hour @minute = minute @second = second @zone = zone @time = nil end |
Instance Attribute Details
#day ⇒ Object (readonly)
Returns the value of attribute day.
62 63 64 |
# File 'lib/time/zone/timestamp.rb', line 62 def day @day end |
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
63 64 65 |
# File 'lib/time/zone/timestamp.rb', line 63 def hour @hour end |
#minute ⇒ Object (readonly)
Returns the value of attribute minute.
64 65 66 |
# File 'lib/time/zone/timestamp.rb', line 64 def minute @minute end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
61 62 63 |
# File 'lib/time/zone/timestamp.rb', line 61 def month @month end |
#second ⇒ Object (readonly)
Returns the value of attribute second.
65 66 67 |
# File 'lib/time/zone/timestamp.rb', line 65 def second @second end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
60 61 62 |
# File 'lib/time/zone/timestamp.rb', line 60 def year @year end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
66 67 68 |
# File 'lib/time/zone/timestamp.rb', line 66 def zone @zone end |
Class Method Details
.dump(time) ⇒ Object
32 33 34 |
# File 'lib/time/zone/timestamp.rb', line 32 def self.dump(time) time.strftime end |
.from(time, zone) ⇒ Object
40 41 42 |
# File 'lib/time/zone/timestamp.rb', line 40 def self.from(time, zone) self.new(time.year, time.month, time.day, time.hour, time.minute, time.second, zone) end |
.load(string) ⇒ Object
28 29 30 |
# File 'lib/time/zone/timestamp.rb', line 28 def self.load(string) self.parse(string) end |
.now(zone) ⇒ Object
36 37 38 |
# File 'lib/time/zone/timestamp.rb', line 36 def self.now(zone) self.from(Zone.now(zone), zone) end |
.parse(*args) ⇒ Object
44 45 46 |
# File 'lib/time/zone/timestamp.rb', line 44 def self.parse(*args) self.from(*Zone.parse(*args)) end |
Instance Method Details
#+(duration) ⇒ Object
Add duration in seconds.
100 101 102 |
# File 'lib/time/zone/timestamp.rb', line 100 def + duration with_offset(0, duration) end |
#-(other) ⇒ Object
Return difference in seconds.
105 106 107 108 109 110 111 112 |
# File 'lib/time/zone/timestamp.rb', line 105 def - other if other.is_a? Numeric # We are subtracting a duration: with_offset(0, -other) else to_time - other.to_time end end |
#<=>(other) ⇒ Object
130 131 132 |
# File 'lib/time/zone/timestamp.rb', line 130 def <=> other to_time <=> other.to_time end |
#convert(zone) ⇒ Object
68 69 70 |
# File 'lib/time/zone/timestamp.rb', line 68 def convert(zone) self.class.from(Time::Zone.convert(to_time, zone), zone) end |
#iso8601 ⇒ Object
126 127 128 |
# File 'lib/time/zone/timestamp.rb', line 126 def iso8601 to_time.iso8601 end |
#offset_by(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ Object
Add duration in various units.
92 93 94 95 96 97 |
# File 'lib/time/zone/timestamp.rb', line 92 def offset_by(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0) with_offset( years * 12 + months, (((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + seconds ) end |
#relative_format(now = Timestamp.now(@zone)) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/time/zone/timestamp.rb', line 144 def relative_format(now = Timestamp.now(@zone)) if self.year != now.year "%-l:%M%P, %B %-d, %Y, %Z" elsif self.month != now.month or self.day != now.day "%-l:%M%P, %B %-d, %Z" else "%-l:%M%P, %Z" end end |
#strftime(format = DEFAULT_FORMAT) ⇒ Object Also known as: to_str
140 141 142 |
# File 'lib/time/zone/timestamp.rb', line 140 def strftime(format = DEFAULT_FORMAT) to_s(format) end |
#to_date ⇒ Object
118 119 120 |
# File 'lib/time/zone/timestamp.rb', line 118 def to_date to_time.to_date end |
#to_datetime ⇒ Object
122 123 124 |
# File 'lib/time/zone/timestamp.rb', line 122 def to_datetime to_time.to_datetime end |
#to_s(format = relative_format) ⇒ Object
136 137 138 |
# File 'lib/time/zone/timestamp.rb', line 136 def to_s(format = relative_format) to_time.strftime(format.gsub('%Z', @zone)) end |
#to_time ⇒ Object
114 115 116 |
# File 'lib/time/zone/timestamp.rb', line 114 def to_time @time ||= Time::Zone.new(@year, @month, @day, @hour, @minute, @second, @zone).freeze end |
#with_offset(months = 0, seconds = 0, zone = @zone) ⇒ Object
Generate a new time with the specified changes.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/time/zone/timestamp.rb', line 76 def with_offset(months = 0, seconds = 0, zone = @zone) current = self if months != 0 current = current.to_datetime >> months end if seconds != 0 # https://bugs.ruby-lang.org/issues/14879 current = Time::Zone.convert(current.to_time + seconds, @zone) end self.class.from(current, zone) end |