Class: Time::Zone::Timestamp
- Inherits:
-
Object
- Object
- Time::Zone::Timestamp
- Includes:
- Comparable
- 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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/time/zone/timestamp.rb', line 50 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.
64 65 66 |
# File 'lib/time/zone/timestamp.rb', line 64 def day @day end |
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
65 66 67 |
# File 'lib/time/zone/timestamp.rb', line 65 def hour @hour end |
#minute ⇒ Object (readonly)
Returns the value of attribute minute.
66 67 68 |
# File 'lib/time/zone/timestamp.rb', line 66 def minute @minute end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
63 64 65 |
# File 'lib/time/zone/timestamp.rb', line 63 def month @month end |
#second ⇒ Object (readonly)
Returns the value of attribute second.
67 68 69 |
# File 'lib/time/zone/timestamp.rb', line 67 def second @second end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
62 63 64 |
# File 'lib/time/zone/timestamp.rb', line 62 def year @year end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
68 69 70 |
# File 'lib/time/zone/timestamp.rb', line 68 def zone @zone end |
Class Method Details
.dump(time) ⇒ Object
34 35 36 |
# File 'lib/time/zone/timestamp.rb', line 34 def self.dump(time) time.strftime end |
.from(time, zone) ⇒ Object
42 43 44 |
# File 'lib/time/zone/timestamp.rb', line 42 def self.from(time, zone) self.new(time.year, time.month, time.day, time.hour, time.minute, time.second, zone) end |
.load(string) ⇒ Object
30 31 32 |
# File 'lib/time/zone/timestamp.rb', line 30 def self.load(string) self.parse(string) end |
Instance Method Details
#+(duration) ⇒ Object
Add duration in seconds.
102 103 104 |
# File 'lib/time/zone/timestamp.rb', line 102 def + duration with_offset(0, duration) end |
#-(other) ⇒ Object
Return difference in seconds.
107 108 109 110 111 112 113 114 |
# File 'lib/time/zone/timestamp.rb', line 107 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
132 133 134 |
# File 'lib/time/zone/timestamp.rb', line 132 def <=> other to_time <=> other.to_time end |
#convert(zone) ⇒ Object
70 71 72 |
# File 'lib/time/zone/timestamp.rb', line 70 def convert(zone) self.class.from(Time::Zone.convert(to_time, zone), zone) end |
#iso8601 ⇒ Object
128 129 130 |
# File 'lib/time/zone/timestamp.rb', line 128 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.
94 95 96 97 98 99 |
# File 'lib/time/zone/timestamp.rb', line 94 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
146 147 148 149 150 151 152 153 154 |
# File 'lib/time/zone/timestamp.rb', line 146 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
142 143 144 |
# File 'lib/time/zone/timestamp.rb', line 142 def strftime(format = DEFAULT_FORMAT) to_s(format) end |
#to_date ⇒ Object
120 121 122 |
# File 'lib/time/zone/timestamp.rb', line 120 def to_date to_time.to_date end |
#to_datetime ⇒ Object
124 125 126 |
# File 'lib/time/zone/timestamp.rb', line 124 def to_datetime to_time.to_datetime end |
#to_s(format = relative_format) ⇒ Object
138 139 140 |
# File 'lib/time/zone/timestamp.rb', line 138 def to_s(format = relative_format) to_time.strftime(format.gsub('%Z', @zone)) end |
#to_time ⇒ Object
116 117 118 |
# File 'lib/time/zone/timestamp.rb', line 116 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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/time/zone/timestamp.rb', line 78 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 |