Class: Time

Inherits:
Object
  • Object
show all
Includes:
Date_locale
Defined in:
lib/date_tools/date_time_compare.rb,
lib/date_tools/date_locale.rb

Overview

Adaption to allow comparison of time and date

Constant Summary

Constants included from Date_locale

Date_locale::DATE_TEXTS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Date_locale

get_language_key, locale?, set_target_encoding, #strftime_locale

Class Method Details

.sec2time(sec) ⇒ Object

Build a string with number of days, hours … for the given seconds. Only positive seconds allowed.



37
38
39
40
41
42
43
# File 'lib/date_tools/date_time_compare.rb', line 37

def self.sec2time( sec )
  raise "No negative time" if sec < 0
  days, sec = sec.divmod(86400)  # 60*60*24
  hours, sec = sec.divmod(3600)  # 60*60
  min, sec = sec.divmod(60)  # 60*60
  return "#{days}d #{hours}h #{min}m #{sec}s"
end

Instance Method Details

#cwdayObject

Simulate Date#cwday



13
14
15
# File 'lib/date_tools/date_time_compare.rb', line 13

def cwday()
	return self.to_date.cwday
end

#cweekObject



20
21
22
23
24
# File 'lib/date_tools/date_time_compare.rb', line 20

def cweek()
	#~ return [1,self.strftime_d('%W').to_i].max
	#~ return self.strftime_d('%W').to_i	#Wrong, not  ISO 8601
	return self.to_date.cweek
end

#cwyearObject

Simulate Date#cwyear



26
27
28
# File 'lib/date_tools/date_time_compare.rb', line 26

def cwyear()
	return self.to_date.cwyear
end

#strftime(format = '%F', lang = nil) ⇒ Object

Redefine strftime for locale versions.



276
277
278
# File 'lib/date_tools/date_locale.rb', line 276

def strftime(format='%F', lang = nil )
  return strftime_locale(format, lang )
end

#strftime_origObject



274
# File 'lib/date_tools/date_locale.rb', line 274

alias :strftime_orig :strftime

#timediff(time2) ⇒ Object

Calculate the difference (no negative times) and build the diff-string.



31
32
33
# File 'lib/date_tools/date_time_compare.rb', line 31

def timediff( time2 )
  return Time.sec2time( ( self - time2 ).abs )
end

#to_dateObject

Convert Time object to a Date object.



9
10
11
# File 'lib/date_tools/date_time_compare.rb', line 9

def to_date()
	return Date.new( self.year, self.month, self.day )
end