Class: Time

Inherits:
Object show all
Defined in:
lib/days_and_times/time.rb

Class Method Summary collapse

Instance Method Summary collapse

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/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/days_and_times/time.rb', line 59

def self.local_time(*args)
  time_with_datetime_fallback(:local, *args)
end

.next_monthObject



78
79
80
# File 'lib/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/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

.todayObject



75
76
77
# File 'lib/days_and_times/time.rb', line 75

def self.today
  Time.now.beginning_of_day
end

.tomorrowObject



63
64
65
# File 'lib/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/days_and_times/time.rb', line 54

def self.utc_time(*args)
  time_with_datetime_fallback(:utc, *args)
end

.yesterdayObject



69
70
71
# File 'lib/days_and_times/time.rb', line 69

def self.yesterday
  Time.now.beginning_of_day - 1.day
end

Instance Method Details

#beginning_of_dayObject 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/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/days_and_times/time.rb', line 10

def change(options)
  ::Time.send(
    self.utc? ? :utc_time : :local_time,
    options[:year]  || self.year,
    options[:month] || self.month,
    options[:day]   || options[:mday] || self.day, # mday is deprecated
    options[:hour]  || self.hour,
    options[:min]   || (options[:hour] ? 0 : self.min),
    options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
    options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
  )
end

#day_nameObject



23
24
25
# File 'lib/days_and_times/time.rb', line 23

def day_name
  self.strftime("%A")
end

#for(duration) ⇒ Object

Raises:

  • (TypeError)


88
89
90
91
92
# File 'lib/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/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_timeObject



121
122
123
# File 'lib/days_and_times/time.rb', line 121

def humanize_date_time
  self.humanize_date + ' ' + self.humanize_time
end

#humanize_timeObject



106
107
108
# File 'lib/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

Returns:

  • (Boolean)


93
94
95
# File 'lib/days_and_times/time.rb', line 93

def is_today?
  self.beginning_of_day == Time.today
end

#month_nameObject



26
27
28
# File 'lib/days_and_times/time.rb', line 26

def month_name
  self.strftime("%B")
end

#seconds_since_midnightObject

Seconds since midnight: Time.now.seconds_since_midnight



31
32
33
# File 'lib/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

#strfsqlObject



96
97
98
# File 'lib/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/days_and_times/time.rb', line 85

def through(duration)
  self.until(duration)
end

#to_timeObject



3
4
5
# File 'lib/days_and_times/time.rb', line 3

def to_time
  self
end

#tomorrowObject



66
67
68
# File 'lib/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/days_and_times/time.rb', line 82

def until(end_time)
  Duration.new(end_time - self, self)
end

#yesterdayObject



72
73
74
# File 'lib/days_and_times/time.rb', line 72

def yesterday
  self.beginning_of_day - 1.day
end