Class: Time
- Defined in:
- lib/quickbooks/extlib/time.rb,
lib/quickbooks/extlib/days_and_times/time.rb
Class Method Summary collapse
-
.from_tzid(tzid) ⇒ Object
We aren’t handling the Time Zone part here…
-
.local_time(*args) ⇒ Object
wraps class method time_with_datetime_fallback with utc_or_local == :local.
- .next_month ⇒ Object
-
.time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
Returns a new Time if requested year can be accomodated by Ruby’s Time class (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); otherwise returns a DateTime.
- .today ⇒ Object
- .tomorrow ⇒ Object
-
.utc_time(*args) ⇒ Object
wraps class method time_with_datetime_fallback with utc_or_local == :utc.
- .yesterday ⇒ Object
Instance Method Summary collapse
-
#beginning_of_day ⇒ Object
(also: #midnight, #at_midnight, #at_beginning_of_day)
Returns a new Time representing the start of the day (0:00).
-
#change(options) ⇒ Object
Returns a new Time where one or more of the elements have been changed according to the
options
parameter. - #day_name ⇒ Object
- #for(duration) ⇒ Object
-
#humanize_date(length_profile = 'medium') ⇒ Object
There may be decent reason to change how this works entirely…
- #humanize_date_time ⇒ Object
- #humanize_time ⇒ Object
- #is_today? ⇒ Boolean
- #month_name ⇒ Object
-
#seconds_since_midnight ⇒ Object
Seconds since midnight: Time.now.seconds_since_midnight.
- #strfsql ⇒ Object
- #through(duration) ⇒ Object
-
#to_datetime ⇒ DateTime
Convert to DateTime (for DateTime/Time conversion protocol).
-
#to_json ⇒ String
Convert to ISO 8601 representation.
-
#to_time ⇒ Time
Return receiver (for DateTime/Time conversion protocol).
- #tomorrow ⇒ Object
- #until(end_time) ⇒ Object
- #yesterday ⇒ Object
Class Method Details
.from_tzid(tzid) ⇒ Object
We aren’t handling the Time Zone part here…
99 100 101 102 103 104 105 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 99 def self.from_tzid(tzid) #We aren't handling the Time Zone part here... if tzid =~ /(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)Z/ # yyyymmddThhmmss Time.xmlschema("#{$1}-#{$2}-#{$3}T#{$4}:#{$5}:#{$6}") else return nil end end |
.local_time(*args) ⇒ Object
wraps class method time_with_datetime_fallback with utc_or_local == :local
59 60 61 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 59 def self.local_time(*args) time_with_datetime_fallback(:local, *args) end |
.next_month ⇒ Object
78 79 80 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 78 def self.next_month today.change(:day => 1, :month => today.month + 1) end |
.time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
Returns a new Time if requested year can be accomodated by Ruby’s Time class (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); otherwise returns a DateTime
46 47 48 49 50 51 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 46 def self.time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) rescue offset = if utc_or_local.to_sym == :utc then 0 else ::DateTime.now.offset end ::DateTime.civil(year, month, day, hour, min, sec, offset, 0) end |
.today ⇒ Object
75 76 77 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 75 def self.today Time.now.beginning_of_day end |
.tomorrow ⇒ Object
63 64 65 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 63 def self.tomorrow Time.now.beginning_of_day + 1.day end |
.utc_time(*args) ⇒ Object
wraps class method time_with_datetime_fallback with utc_or_local == :utc
54 55 56 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 54 def self.utc_time(*args) time_with_datetime_fallback(:utc, *args) end |
Instance Method Details
#beginning_of_day ⇒ Object Also known as: midnight, at_midnight, at_beginning_of_day
Returns a new Time representing the start of the day (0:00)
36 37 38 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 36 def beginning_of_day (self - self.seconds_since_midnight).change(:usec => 0) end |
#change(options) ⇒ Object
Returns a new Time where one or more of the elements have been changed according to the options
parameter. The time options (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and minute is passed, then sec and usec is set to 0.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 10 def change() ::Time.send( self.utc? ? :utc_time : :local_time, [:year] || self.year, [:month] || self.month, [:day] || [:mday] || self.day, # mday is deprecated [:hour] || self.hour, [:min] || ([:hour] ? 0 : self.min), [:sec] || (([:hour] || [:min]) ? 0 : self.sec), [:usec] || (([:hour] || [:min] || [:sec]) ? 0 : self.usec) ) end |
#day_name ⇒ Object
23 24 25 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 23 def day_name self.strftime("%A") end |
#for(duration) ⇒ Object
88 89 90 91 92 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 88 def for(duration) raise TypeError, "must be a Duration object." unless duration.is_a?(Duration) duration.start_time = self duration end |
#humanize_date(length_profile = 'medium') ⇒ Object
There may be decent reason to change how this works entirely…
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 109 def humanize_date(length_profile='medium') #There may be decent reason to change how this works entirely... case length_profile when 'abbr' || 'abbreviated' self.strftime("%m/%d/%y") when 'short' self.strftime("%b #{self.strftime("%d").to_i.to_s}") when 'medium' self.strftime("%B #{self.strftime("%d").to_i.to_s}") when 'long' self.strftime("%B #{self.strftime("%d").to_i.to_s}, %Y") end end |
#humanize_date_time ⇒ Object
121 122 123 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 121 def humanize_date_time self.humanize_date + ' ' + self.humanize_time end |
#humanize_time ⇒ Object
106 107 108 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 106 def humanize_time self.strftime("%M").to_i > 0 ? self.strftime("#{self.strftime("%I").to_i.to_s}:%M%p").downcase : self.strftime("#{self.strftime("%I").to_i.to_s}%p").downcase end |
#is_today? ⇒ Boolean
93 94 95 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 93 def is_today? self.beginning_of_day == Time.today end |
#month_name ⇒ Object
26 27 28 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 26 def month_name self.strftime("%B") end |
#seconds_since_midnight ⇒ Object
Seconds since midnight: Time.now.seconds_since_midnight
31 32 33 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 31 def seconds_since_midnight self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6) end |
#strfsql ⇒ Object
96 97 98 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 96 def strfsql self.strftime("%Y-#{self.strftime("%m").to_i.to_s}-#{self.strftime("%d").to_i.to_s}") end |
#through(duration) ⇒ Object
85 86 87 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 85 def through(duration) self.until(duration) end |
#to_datetime ⇒ DateTime
Convert to DateTime (for DateTime/Time conversion protocol).
Time.now.to_datetime #=> #<DateTime: 106046956823/43200,-1/3,2299161>
38 39 40 |
# File 'lib/quickbooks/extlib/time.rb', line 38 def to_datetime DateTime.parse self.to_s end |
#to_json ⇒ String
Convert to ISO 8601 representation
Time.now.to_json #=> "\"2008-03-28T17:54:20-05:00\""
14 15 16 |
# File 'lib/quickbooks/extlib/time.rb', line 14 def to_json(*) self.xmlschema.to_json end |
#to_time ⇒ Time
Return receiver (for DateTime/Time conversion protocol).
Time.now.to_time #=> Wed Nov 19 20:08:28 -0800 2008
26 27 28 |
# File 'lib/quickbooks/extlib/time.rb', line 26 def to_time self end |
#tomorrow ⇒ Object
66 67 68 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 66 def tomorrow self.beginning_of_day + 1.day end |
#until(end_time) ⇒ Object
82 83 84 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 82 def until(end_time) Duration.new(end_time - self, self) end |
#yesterday ⇒ Object
72 73 74 |
# File 'lib/quickbooks/extlib/days_and_times/time.rb', line 72 def yesterday self.beginning_of_day - 1.day end |