Class: Time::Zone::Timestamp

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#dayObject (readonly)

Returns the value of attribute day.



64
65
66
# File 'lib/time/zone/timestamp.rb', line 64

def day
  @day
end

#hourObject (readonly)

Returns the value of attribute hour.



65
66
67
# File 'lib/time/zone/timestamp.rb', line 65

def hour
  @hour
end

#minuteObject (readonly)

Returns the value of attribute minute.



66
67
68
# File 'lib/time/zone/timestamp.rb', line 66

def minute
  @minute
end

#monthObject (readonly)

Returns the value of attribute month.



63
64
65
# File 'lib/time/zone/timestamp.rb', line 63

def month
  @month
end

#secondObject (readonly)

Returns the value of attribute second.



67
68
69
# File 'lib/time/zone/timestamp.rb', line 67

def second
  @second
end

#yearObject (readonly)

Returns the value of attribute year.



62
63
64
# File 'lib/time/zone/timestamp.rb', line 62

def year
  @year
end

#zoneObject (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

.now(zone) ⇒ Object



38
39
40
# File 'lib/time/zone/timestamp.rb', line 38

def self.now(zone)
	self.from(Zone.now(zone), zone)
end

.parse(*args) ⇒ Object



46
47
48
# File 'lib/time/zone/timestamp.rb', line 46

def self.parse(*args)
	self.from(*Zone.parse(*args))
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

#iso8601Object



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_dateObject



120
121
122
# File 'lib/time/zone/timestamp.rb', line 120

def to_date
	to_time.to_date
end

#to_datetimeObject



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_timeObject



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.

Parameters:

  • months (Numeric) (defaults to: 0)

    add this many months

  • seconds (Numeric) (defaults to: 0)

    add this many seconds

  • zone (String) (defaults to: @zone)

    forcefully change the timezone without doing any conversion.



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