Class: Datet
Overview
This class handels various time- and date-specific behaviour in a friendly way.
Examples
datet = Datet.new #=> 2012-05-03 20:35:16 0200 datet = Datet.new(Time.now) #=> 2012-05-03 20:35:16 0200 datet.months + 5 #=> 2012-10-03 20:35:16 0200 datet.days 64 #=> 2012-12-06 20:35:16 +010
Constant Summary collapse
- DBSTR_ALLOWED_ARGS =
Returns a database-string for the given date. Can also return nil, if the :allow_null argument is true.
Examples
Datet.dbstr(:date => “0000-00-00 00:00:00”, :allow_null => true) #=> nil Datet.dbstr(:date => “1985-06-17”) #=> “1985-06-17 00:00:00”)
[:date, :allow_null]
- @@months_lcase =
{ "jan" => 1, "january" => 1, "feb" => 2, "february" => 2, "mar" => 3, "march" => 3, "apr" => 4, "april" => 4, "may" => 5, "maj" => 5, "jun" => 6, "june" => 6, "jul" => 7, "july" => 7, "aug" => 8, "august" => 8, "sep" => 9, "september" => 9, "oct" => 10, "october" => 10, "okt" => 10, "nov" => 11, "november" => 11, "dec" => 12, "december" => 12 }
- @@days_lcase =
{ "sunday" => 0, "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6 }
- @@day_names =
{ 0 => "Sunday", 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday" }
- @@month_names =
{ 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December" }
- @@days_in_months =
Thanks to ActiveSupport: rubydoc.info/docs/rails/2.3.8/ActiveSupport/CoreExtensions/Time/Calculations
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Class Method Summary collapse
-
.arg_to_datet(datet) ⇒ Object
Turns the given argument into a new Datet-object.
-
.arg_to_time(datet) ⇒ Object
Turns the given argument into a new Time-object.
-
.day_str_to_no(day_str, args = nil) ⇒ Object
Converts a given day-name to the right day number.
-
.days(args = nil) ⇒ Object
Returns a hash with the day-number as value (starting with 1 for monday).
-
.days_between(t1, t2) ⇒ Object
Returns how many days there is between the two timestamps given as an integer.
-
.days_in_month(year, month) ⇒ Object
Class-method for days in month.
-
.days_in_year(year) ⇒ Object
Returns the amount of days in the given year.
- .dbstr(args) ⇒ Object
-
.from_ut(unix_timestamp) ⇒ Object
Creates a new Datet-object from the given Unix Timestamp.
-
.gregorian_leap?(y) ⇒ Boolean
Is a year a leap year in the Gregorian calendar? Copied from Date-class.
-
.in(timestr) ⇒ Object
Parses various objects into Datet-objects.
-
.is_nullstamp?(stamp) ⇒ Boolean
Returns true of the given stamp is a ‘nullstamp’.
-
.month_str_to_no(str) ⇒ Object
Returns the month-number for a given string (starting with 1 for january).
-
.months(args = nil) ⇒ Object
Returns a hash with the month-no as key and month-name as value.
-
.random(args = {}) ⇒ Object
Returns a random Datet-object within given parameters.
Instance Method Summary collapse
-
#+(val) ⇒ Object
Add something.
-
#-(val) ⇒ Object
Minus something.
- #<=>(timeobj) ⇒ Object
-
#add_days(days = 1) ⇒ Object
Adds a given amount of days to the object.
-
#add_hours(hours = 1) ⇒ Object
Adds a given amount of hours to the object.
-
#add_mins(mins = 1) ⇒ Object
Add a given amount of minutes to the object.
-
#add_months(months = 1) ⇒ Object
Adds a given amount of months to the object.
-
#add_secs(secs = 1) ⇒ Object
Add a given amount of seconds to the object.
-
#add_something(val) ⇒ Object
This method is used for adding values to the object based on the current set mode.
-
#add_usecs(usecs = 1) ⇒ Object
Add a given amount of micro-seconds to the object.
-
#add_years(years = 1) ⇒ Object
Adds a given amount of years to the object.
-
#age_in_years(cur = Datet.new) ⇒ Object
Returns the age in years calculated from the current time and back.
-
#ago_str(args = {}) ⇒ Object
Returns a human readable string based on the difference from the current time and date.
-
#ampm ⇒ Object
Returns “am” or “pm” based on the hours values.
-
#code ⇒ Object
This returns a code-string that can be used to recreate the Datet-object.
-
#day ⇒ Object
Returns the day in month as an integer.
-
#day=(newday) ⇒ Object
Changes the day to a given day.
-
#day_in_week(args = nil) ⇒ Object
Returns the day in the week.
-
#day_in_year ⇒ Object
Returns the current day in the year.
-
#day_name ⇒ Object
Returns the days name as a string.
-
#day_of_year ⇒ Object
Returns the day of the year (0-365) as an integer.
-
#day_str(args = nil) ⇒ Object
Returns the day as a localized string.
-
#days ⇒ Object
Sets the mode to days and gets ready to plus or minus.
-
#days_in_month ⇒ Object
Returns the number of days in the month.
-
#days_in_year ⇒ Object
Returns the amount of days in the current year.
-
#dbstr(args = nil) ⇒ Object
Returns the time as a database-valid string.
-
#find(args) ⇒ Object
Goes forward day-by-day and stops at a date matching the criteria given.
-
#hour ⇒ Object
Returns the hour as an integer.
-
#hour=(newhour) ⇒ Object
Changes the hour to a given new hour.
-
#hour_thc ⇒ Object
Returns the hour as an integer in the twelve-hour-clock.
-
#hours ⇒ Object
Sets the mode to hours and gets ready to plus or minus.
-
#httpdate ⇒ Object
Returns the HTTP-date that can be used in headers and such.
-
#human_str(args = {}) ⇒ Object
Returns the object as a human understandable string.
-
#initialize(*args) ⇒ Datet
constructor
Initializes the object.
-
#lazy_hour=(newhour) ⇒ Object
Changes the hour to a new hour.
-
#lazy_min=(newmin) ⇒ Object
Changes the minute to a new minute.
-
#lazy_sec=(newsec) ⇒ Object
Changes the second to a given new second.
-
#localtime_str ⇒ Object
Returns ‘localtime’ as of 1.9 - even in 1.8 which does it different.
-
#min ⇒ Object
Returns the minute as an integer.
-
#min=(newmin) ⇒ Object
Changes the minute to a given new minute.
-
#mins ⇒ Object
Sets the mode to minutes and gets ready to plus or minus.
-
#month ⇒ Object
Returns the month as an integer.
-
#month=(newmonth) ⇒ Object
Changes the month to a given new month.
-
#month_name ⇒ Object
Returns the months name as a string.
-
#month_str ⇒ Object
Returns the month as a localized string.
-
#months ⇒ Object
Sets the mode to months and gets ready to plus or minus.
-
#nullstamp? ⇒ Boolean
Returns true if this date is a nullstamp (0000-00-00 or something like it).
-
#offset_info ⇒ Object
Returns various information about the offset as a hash.
-
#offset_str ⇒ Object
Returns the offset as a string.
-
#out(args = {}) ⇒ Object
Returns a string based on the date and time.
-
#sec ⇒ Object
Returns the seconds as an integer.
-
#sec=(newsec) ⇒ Object
Changes the second to a given new second.
-
#secs ⇒ Object
Sets the mode to seconds and gets ready to plus or minus.
-
#stamp(args) ⇒ Object
Returns a new Datet- or Time-object based on the arguments.
-
#strftime(str) ⇒ Object
See the documentation for Time#strftime.
-
#time ⇒ Object
(also: #to_time)
Returns a new ‘Time’-object based on the data of the ‘Datet’-object.
-
#to_a ⇒ Object
Returns arguments in an array.
-
#to_f ⇒ Object
Returns the unix timestamp for this object as a float (uses ‘Time’ to calculate).
-
#to_i ⇒ Object
Returns the unix timestamp for this object (uses ‘Time’ to calculate).
-
#to_s ⇒ Object
Returns a string that describes the object.
-
#update_from_str(str) ⇒ Object
Updates the date-data from a given string.
-
#update_from_time(time) ⇒ Object
Updates the current variables to the given time.
-
#usec ⇒ Object
Returns the microsecond as an integer.
-
#usec=(newusec) ⇒ Object
Changes the usecond of the object.
-
#usecs ⇒ Object
Sets the mode to mili-seconds and gets ready to plus or minus.
-
#week_no(args = nil) ⇒ Object
Returns the week-number of the year (1..53).
-
#year ⇒ Object
Returns the year as an integer.
-
#year=(newyear) ⇒ Object
Changes the year to the given year.
-
#years ⇒ Object
Sets the mode to years and gets ready to plus or minus.
Constructor Details
#initialize(*args) ⇒ Datet
Initializes the object. Default is the current time. A time-object can be given.
Examples
datet = Datet.new #=> Datet-object with the current date and time.
time = Time.new datet = Datet.new(time) #=> Datet-object with the date and time from the given Time-object.
datet = Datet.new(1985, 06, 17) #=> Datet-object with the date 1985-06-17. datet = Datet.new(1985, 06, 17, 10) #=> Datet-object with the date 1985-06-17 10:00:00
datet = Datet.new(1985, 06, 35) #=> Datet-object with the date 1985-07-05 00:00:00. Notice the invalid day of 35 was automatically converted to the right date.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/datet.rb', line 90 def initialize(*args) if args.length == 1 and args.first.is_a?(Time) self.update_from_time(args.first) return nil elsif args.empty? self.update_from_time(Time.now) return nil end #Handle nil-support. all_nil = true args.each do |arg| if arg != 0 and arg all_nil = false break end end if all_nil @t_nil = true @t_year = 0 @t_month = 0 @t_day = 0 @t_hour = 0 @t_min = 0 @t_sec = 0 @t_usec = 0 return nil end #Normal date-calculation support. days_left = 0 months_left = 0 hours_left = 0 mins_left = 0 secs_left = 0 usecs_left = 0 #Check larger month the allowed. if args[1] and args[1] > 12 months_left = args[1] - 12 args[1] = 12 end #Check larger date than allowed. if args[1] dim = Datet.days_in_month(args[0], args[1]) if args[2] and args[2] > dim days_left = args[2] - dim args[2] = dim if days_left > 0 end end #Check larger hour than allowed. if args[3] and args[3] >= 24 hours_left = args[3] + 1 args[3] = 0 end #Check larger minute than allowed. if args[4] and args[4] >= 60 mins_left = args[4] + 1 args[4] = 0 end #Check larger secs than allowed. if args[5] and args[5] >= 60 secs_left = args[5] + 1 args[5] = 0 end #Check larger usecs than allowed. if args[6] and args[6] >= 1000000 usecs_left = args[6] + 1 args[6] = 0 end #Generate new stamp. if args[0] @t_year = args[0] else @t_year = Time.now.year end if args[1] @t_month = args[1] else @t_month = 1 end if args[2] @t_day = args[2] else @t_day = 1 end if args[3] @t_hour = args[3] else @t_hour = 0 end if args[4] @t_min = args[4] else @t_min = 0 end if args[5] @t_sec = args[5] else @t_sec = 0 end if args[6] @t_usec = args[6] else @t_usec = 0 end self.add_mins(mins_left) if mins_left > 0 self.add_hours(hours_left) if hours_left > 0 self.add_days(days_left) if days_left > 0 self.add_months(months_left) if months_left > 0 self.add_secs(secs_left) if secs_left > 0 self.add_usecs(usecs_left) if usecs_left > 0 end |
Class Method Details
.arg_to_datet(datet) ⇒ Object
Turns the given argument into a new Datet-object. Helps compare datet- to time-objects.
Examples
time = Datet.arg_to_time(datet) #=> <Datet>-object time = Datet.arg_to_time(Datet.now) #=> <Datet>-object
789 790 791 792 793 794 795 796 797 |
# File 'lib/datet.rb', line 789 def self.arg_to_datet(datet) if datet.is_a?(Datet) return datet elsif datet.is_a?(Time) return Datet.new(datet) else raise ArgumentError, "Could not handle object of class: '#{datet.class.name}'." end end |
.arg_to_time(datet) ⇒ Object
Turns the given argument into a new Time-object.
Examples
time = Datet.arg_to_time(datet) #=> <Time>-object time = Datet.arg_to_time(Time.now) #=> <Time>-object
775 776 777 778 779 780 781 782 783 |
# File 'lib/datet.rb', line 775 def self.arg_to_time(datet) if datet.is_a?(Datet) return datet.time elsif datet.is_a?(Time) return datet else raise ArgumentError, "Could not handle object of class: '#{datet.class.name}'." end end |
.day_str_to_no(day_str, args = nil) ⇒ Object
Converts a given day-name to the right day number.
Examples
Datet.day_str_to_no(‘wed’) #=> 3
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 |
# File 'lib/datet.rb', line 1246 def self.day_str_to_no(day_str, args = nil) day_str = day_str.to_s.strip[0, 3] if no = @@days_lcase[day_str] if args and args[:mfirst] if no == 0 no = 6 else no -= 1 end end return no end raise ArgumentError, "Invalid day-string: '#{day_str}'." end |
.days(args = nil) ⇒ Object
Returns a hash with the day-number as value (starting with 1 for monday). It uses the method “_” to translate the months names.
Examples
Datet.days #=> 1=>“Monday”, 2=>“Tuesday”, 3=>“Wednesday”, 4=>“Thursday”, 5=>“Friday”, 6=>“Saturday” Datet.days(:mfirst => true) #=> 1=>“Tuesday”, 2=>“Wednesday”, 3=>“Thursday”, 4=>“Friday”, 5=>“Saturday”, 6=>“Sunday”
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 |
# File 'lib/datet.rb', line 1200 def self.days(args = nil) if args args.each do |key, val| case key when :mfirst, :short, :trans else raise ArgumentError, "Unknown key in arguments: '#{key}'." end end end ret = @@day_names.clone if args if args[:trans] ret.each do |key, val| ret[key] = _(val) end end if args[:mfirst] newret = {} ret.each do |key, val| next if key == 0 newret[key - 1] = val end newret[6] = ret[0] ret = newret end if args[:short] ret_short = {} ret.each do |key, val| ret_short[key] = val[0..2] end ret = ret_short end end return ret end |
.days_between(t1, t2) ⇒ Object
Returns how many days there is between the two timestamps given as an integer.
Examples
d1 = Datet.new #=> 2012-05-03 18:04:12 0200 d2 = Datet.new #=> 2012-05-03 18:04:16 0200 d2.months + 5 #=> 2012-10-03 18:04:16 +0200 Datet.days_between(d1, d2) #=> 153
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'lib/datet.rb', line 1012 def self.days_between(t1, t2) raise ArgumentError, "Timestamp 2 should be larger than timestamp 1." if t2 < t1 doy1 = t1.day_of_year doy2 = t2.day_of_year yot1 = t1.year yot2 = t2.year days = 0 #If there is a years-difference, then neutralize the first year by counting up to next year and starting from 1/1. if yot1 < yot2 diy = Datet.days_in_year(yot1) days += diy - doy1 + 1 doy1 = 1 yot1 += 1 end #For each year calculate the amount of days in that year and add them to the count. while yot1 < yot2 diy = Datet.days_in_year(yot1) days += diy yot1 += 1 end #Last calculate the difference between the two dates and add the count to that. days_between = doy2 - doy1 return days_between + days end |
.days_in_month(year, month) ⇒ Object
Class-method for days in month.
480 481 482 483 484 |
# File 'lib/datet.rb', line 480 def self.days_in_month(year, month) raise ArgumentError, "Invalid month: '#{month}'." if month.to_i <= 0 return 29 if month == 2 and Datet.gregorian_leap?(year) return @@days_in_months[month] end |
.days_in_year(year) ⇒ Object
Returns the amount of days in the given year.
492 493 494 495 |
# File 'lib/datet.rb', line 492 def self.days_in_year(year) return 366 if Datet.gregorian_leap?(year) return 365 end |
.dbstr(args) ⇒ Object
955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
# File 'lib/datet.rb', line 955 def self.dbstr(args) args.each do |key, val| raise "Invalid argument: '#{key}'." if !DBSTR_ALLOWED_ARGS.include?(key) end datet = Datet.in(args[:date]) if !datet return nil if args[:allow_null] return "0000-00-00 00:00:00" else return datet.dbstr end end |
.from_ut(unix_timestamp) ⇒ Object
Creates a new Datet-object from the given Unix Timestamp.
1158 1159 1160 |
# File 'lib/datet.rb', line 1158 def self.from_ut() return Datet.new(Time.at(.to_i)) end |
.gregorian_leap?(y) ⇒ Boolean
Is a year a leap year in the Gregorian calendar? Copied from Date-class.
Examples
if Datet.gregorian_leap?(2005)
print "2005 is a gregorian-leap year."
else
print "2005 is not a gregorian-leap year."
end
461 462 463 464 465 466 467 468 469 |
# File 'lib/datet.rb', line 461 def self.gregorian_leap?(y) if y % 4 == 0 && y % 100 != 0 return true elsif y % 400 == 0 return true else return false end end |
.in(timestr) ⇒ Object
Parses various objects into Datet-objects.
Examples
datet = Datet.in(“1985-06-17”) #=> 1985-06-17 00:00:00 0200 datet = Datet.in(“1985-06-17 10:00:00”) #=> 1985-06-17 10:00:00 0200 datet = Datet.in(“17/06 1985 10:00”) #=> 1985-06-17 10:00:00 +0200
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 |
# File 'lib/datet.rb', line 1086 def self.in(timestr) if timestr.is_a?(Time) return Datet.new(timestr) elsif timestr.is_a?(Date) return Datet.new(timestr.to_time) elsif timestr.is_a?(Datet) return timestr elsif Datet.is_nullstamp?(timestr) return false end timestr_t = timestr.to_s.downcase.strip if match = timestr_t.match(/^(\d+)\/(\d+) (\d+)/) #MySQL date format timestr = timestr.gsub(match[0], "") date = match[1].to_i month = match[2].to_i year = match[3].to_i if match = timestr.match(/\s*(\d+):(\d+)/) #MySQL datetime format hour = match[1].to_i minute = match[2].to_i end return Datet.new(year, month, date, hour, minute) elsif match = timestr_t.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/) return Datet.new(match[3], match[2], match[1]) elsif match = timestr_t.match(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{5,6})$/) #Datet.code format return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[4].to_i, match[5].to_i, match[6].to_i, match[7].to_i) elsif match = timestr_t.match(/^\s*(\d{4})-(\d{1,2})-(\d{1,2})(|\s+(\d{2}):(\d{2}):(\d{2})(|\.\d+)\s*)(|\s+(utc))(|\s+(\+|\-)(\d{2})(\d{2}))$/) #Database date format (with possibility of .0 in the end - microseconds? -knj. if match[11] and match[13] and match[14] if match[12] == "+" or match[12] == "-" sign = match[12] else sign = "+" end utc_str = "#{sign}#{match[13]}:#{match[14]}" elsif match[8] utc_str = match[8].to_i else utc_str = nil end if utc_str time = Time.local(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i, utc_str) return Datet.new(time) else return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i) end elsif match = timestr_t.match(/^\s*(\d{2,4})-(\d{1,2})-(\d{1,2})(|\s+(\d{1,2}):(\d{1,2}):(\d{1,2})(:(\d{1,2})|)\s*)$/) return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i) elsif match = timestr_t.match(/^([A-z]+),\s*(\d+)\s+([A-z]+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s*([A-z]+)$/) return Datet.new(match[4].to_i, Datet.month_str_to_no(match[3]), match[2].to_i, match[5].to_i, match[6].to_i, match[7].to_i) elsif match = timestr_t.match(/^(\d{4})-(\d{2})-(\d{2})t(\d{2}):(\d{2}):(\d{2})\+(\d{4})$/i) return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[4].to_i, match[5].to_i, match[6].to_i) elsif match = timestr_t.match(/^(\d{4})-(\d{2})-(\d{2})(t|\s+)(\d{2}):(\d{2}):(\d{2})\.(\d+)(z|)$/i) #FIXME: Parse the weird usec at the end. return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i) elsif match = timestr_t.match(/^(\d{4})-(\d{2})-(\d{2})(t|\s+)(\d{2}):(\d{2}):(\d{2})(z|)$/i) return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i) end raise ArgumentError, "Wrong format: '#{timestr}', class: '#{timestr.class.name}'" end |
.is_nullstamp?(stamp) ⇒ Boolean
Returns true of the given stamp is a ‘nullstamp’.
Examples
Datet.is_nullstamp?(“0000-00-00”) #=> true Datet.is_nullstamp?(“0000-00-00 00:00:00”) #=> true Datet.is_nullstamp?(“”) #=> true Datet.is_nullstamp?(“1985-06-17”) #=> false
976 977 978 979 |
# File 'lib/datet.rb', line 976 def self.is_nullstamp?(stamp) return true if !stamp or stamp == "0000-00-00" or stamp == "0000-00-00 00:00:00" or stamp.to_s.strip == "" return false end |
.month_str_to_no(str) ⇒ Object
Returns the month-number for a given string (starting with 1 for january).
Examples
Datet.month_str_to_no(“JaNuArY”) #=> 1 Datet.month_str_to_no(“DECEMBER”) #=> 12 Datet.month_str_to_no(“kasper”) #=> <Error>-raised
1269 1270 1271 1272 1273 |
# File 'lib/datet.rb', line 1269 def self.month_str_to_no(str) str = str.to_s.downcase.strip[0, 3].strip return @@months_lcase[str] if @@months_lcase.key?(str) raise ArgumentError, "No month to return from that string: '#{str}'." end |
.months(args = nil) ⇒ Object
Returns a hash with the month-no as key and month-name as value. It uses the method “_” to translate the months names. So GetText or another method has to be defined.
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 |
# File 'lib/datet.rb', line 1163 def self.months(args = nil) if args args.each do |key, val| case key when :short, :trans else raise ArgumentError, "Invalid key in arguments: '#{key}'." end end end ret = @@month_names.clone if args if args[:trans] ret.each do |key, val| ret[key] = _(val) end end if args[:short] ret_short = {} ret.each do |key, val| ret_short[key] = val[0..2] end return ret_short end end return ret end |
.random(args = {}) ⇒ Object
Returns a random Datet-object within given parameters.
Examples
datet_rand = Datet.random(:years => 2003..2005) #=> Datet-object
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 |
# File 'lib/datet.rb', line 1588 def self.random(args = {}) #Test given arguments. raise "Given argument wasnt a hash: '#{args.class.name}'." if !args.is_a?(Hash) args.each do |key, val| raise "Invalid key: '#{key}'." if ![:years, :months, :days, :hours, :mins, :secs].include?(key) end #Calculate random year. years = args[:years] || (1971..Time.now.year) year = years.to_a.sample #Calculate random month. months = args[:months] || (1..12) month = months.to_a.sample month = 1 if month < 1 month = 12 if month > 12 #Calculate random day. days_in_month = Datet.days_in_month(year, month) days = args[:days] || (1..days_in_month) day = days.to_a.sample day = days_in_month if day > days_in_month day = 1 if day < 1 #Calculate random hour. hours = args[:hours] || (0..23) hour = hours.to_a.sample hour = 0 if hour < 0 hour = 23 if hour > 23 #Calculate random minute. mins = args[:mins] || (0..59) min = mins.to_a.sample min = 0 if min < 0 min = 59 if min > 59 #Calculate random second. secs = args[:secs] || (0..59) sec = secs.to_a.sample sec = 0 if sec < 0 sec = 59 if sec > 59 #Spawn object with the given random values. return Datet.new(year, month, day, hour, min, sec) end |
Instance Method Details
#+(val) ⇒ Object
Add something.
Examples
datet.months + 5 datet.months + 2
849 850 851 852 |
# File 'lib/datet.rb', line 849 def +(val) Thread.current[:datet_addmode] = "+" self.add_something(val) end |
#-(val) ⇒ Object
Minus something.
Examples
datet.months - 5 datet.years - 2
840 841 842 843 |
# File 'lib/datet.rb', line 840 def -(val) Thread.current[:datet_addmode] = "-" self.add_something(val) end |
#<=>(timeobj) ⇒ Object
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 |
# File 'lib/datet.rb', line 801 def <=>(timeobj) timeobj = Datet.arg_to_datet(timeobj) tries = [:year, :month, :day, :hour, :min, :sec, :usec] tries.each do |try| res1 = timeobj.__send__(try) res2 = self.__send__(try) if res1 > res2 return -1 elsif res1 < res2 return 1 end end return 0 end |
#add_days(days = 1) ⇒ Object
Adds a given amount of days to the object.
Examples
datet = Datet.new #=> 2012-05-03 17:42:27 0200 datet.add_days(29) datet.time #=> 2012-06-01 17:42:27 0200
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/datet.rb', line 379 def add_days(days = 1) days = days.to_i dim = self.days_in_month cur_day = @t_day next_day = cur_day + days if next_day > dim @t_day = 1 self.add_months(1) days_left = (days - 1) - (dim - cur_day) self.add_days(days_left) if days_left != 0 elsif next_day <= 0 self.add_months(-1) @t_day = self.days_in_month days_left = days + cur_day self.add_days(days_left) if days_left != 0 else @t_day = next_day end return self end |
#add_hours(hours = 1) ⇒ Object
Adds a given amount of hours to the object.
Examples
datet = Datet.new datet.add_hours(2)
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/datet.rb', line 352 def add_hours(hours = 1) hours = hours.to_i cur_hour = @t_hour next_hour = cur_hour + hours if next_hour >= 24 @t_hour = 0 self.add_days(1) hours_left = (hours - 1) - (24 - cur_hour) self.add_hours(hours_left) if hours_left > 0 elsif next_hour < 0 @t_hour = 23 self.add_days(-1) hours_left = hours + cur_hour + 1 self.add_hours(hours_left) if hours_left < 0 else @t_hour = next_hour end return self end |
#add_mins(mins = 1) ⇒ Object
Add a given amount of minutes to the object.
Examples
datet = Datet.new #=> 2012-05-03 17:39:45 0200 datet.add_mins(30) datet.time #=> 2012-05-03 18:08:45 0200
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/datet.rb', line 326 def add_mins(mins = 1) mins = mins.to_i cur_mins = @t_min next_min = cur_mins + mins if next_min >= 60 @t_min = 0 self.add_hours(1) mins_left = (mins - 1) - (60 - cur_mins) self.add_mins(mins_left) if mins_left > 0 elsif next_min < 0 @t_min = 59 self.add_hours(-1) mins_left = mins + cur_mins + 1 self.add_mins(mins_left) if mins_left > 0 else @t_min = next_min end return self end |
#add_months(months = 1) ⇒ Object
Adds a given amount of months to the object.
Examples
datet.time #=> 2012-06-01 17:42:27 0200 datet.add_months(2) datet.time #=> 2012-08-01 17:42:27 0200
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/datet.rb', line 407 def add_months(months = 1) months = months.to_i cur_month = @t_month cur_day = @t_day next_month = cur_month + months.to_i #Check if we have to alter the amount of years based on the month-change. if next_month > 12 or next_month <= 0 years = (next_month.to_f / 12.0).floor newmonth = next_month - (years * 12) if newmonth == 0 newmonth = 12 years -= 1 end self.month = newmonth self.add_years(years) if years != 0 else raise "Invalid month: '#{next_month}'." if next_month <= 0 or next_month > 12 @t_month = next_month @t_day = 1 end #If the month changed, and the day was the last day of the previous month, and there isnt that many days in the new month, set the day to the last day of the current month. dim = self.days_in_month if dim < cur_day @t_day = dim else @t_day = cur_day end return self end |
#add_secs(secs = 1) ⇒ Object
Add a given amount of seconds to the object.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/datet.rb', line 305 def add_secs(secs = 1) secs = secs.to_i cur_secs = @t_sec next_sec = cur_secs + secs if next_sec >= 60 or next_sec <= -60 mins = (next_sec.to_f / 60.0).floor @t_sec = next_sec - (mins * 60) self.add_mins(mins) else @t_sec = next_sec end return self end |
#add_something(val) ⇒ Object
This method is used for adding values to the object based on the current set mode.
Examples
Add two months to the datet. datet.months datet.add_something(2)
824 825 826 827 828 829 830 831 832 833 834 |
# File 'lib/datet.rb', line 824 def add_something(val) val = -val if Thread.current[:datet_addmode] == "-" return self.add_years(val) if Thread.current[:datet_mode] == :years return self.add_hours(val) if Thread.current[:datet_mode] == :hours return self.add_days(val) if Thread.current[:datet_mode] == :days return self.add_months(val) if Thread.current[:datet_mode] == :months return self.add_mins(val) if Thread.current[:datet_mode] == :mins return self.add_secs(val) if Thread.current[:datet_mode] == :secs return self.add_usecs(val) if Thread.current[:datet_mode] == :usecs raise "No such mode: '#{Thread.current[:datet_mode]}'." end |
#add_usecs(usecs = 1) ⇒ Object
Add a given amount of micro-seconds to the object.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/datet.rb', line 288 def add_usecs(usecs = 1) usecs = usecs.to_i cur_usecs = @t_usec next_usec = cur_usecs + usecs if next_usec >= 1000000 or next_usec <= -1000000 secs = (next_usec.to_f / 1000000.0).to_f.floor @t_usec = next_usec - (secs * 1000000) self.add_secs(secs) else @t_usec = next_usec end return self end |
#add_years(years = 1) ⇒ Object
Adds a given amount of years to the object.
Examples
datet.time #=> 2012-08-01 17:42:27 0200 datet.add_years(3) datet.time #> 2014-08-01 17:42:27 0200
449 450 451 452 |
# File 'lib/datet.rb', line 449 def add_years(years = 1) @t_year = @t_year + years.to_i return self end |
#age_in_years(cur = Datet.new) ⇒ Object
Returns the age in years calculated from the current time and back.
1646 1647 1648 1649 1650 |
# File 'lib/datet.rb', line 1646 def age_in_years(cur = Datet.new) years = (cur.year - self.year) years -= 1 if cur.day_in_year < self.day_in_year return years end |
#ago_str(args = {}) ⇒ Object
Returns a human readable string based on the difference from the current time and date.
Examples
datet.time #=> 1985-06-17 10:00:00 0200 datet.ago_str #=> “27 years ago” datet = Datet.new #=> 2012-05-03 20:31:58 0200 datet.ago_str #=> “18 seconds ago”
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 |
# File 'lib/datet.rb', line 1362 def ago_str(args = {}) args = { :year_ago_str => "%s year ago", :years_ago_str => "%s years ago", :month_ago_str => "%s month ago", :months_ago_str => "%s months ago", :day_ago_str => "%s day ago", :days_ago_str => "%s days ago", :hour_ago_str => "%s hour ago", :hours_ago_str => "%s hours ago", :min_ago_str => "%s minute ago", :mins_ago_str => "%s minutes ago", :sec_ago_str => "%s second ago", :secs_ago_str => "%s seconds ago", :right_now_str => "right now" }.merge(args) secs_ago = Time.now.to_i - self.to_i mins_ago = secs_ago.to_f / 60.0 hours_ago = mins_ago / 60.0 days_ago = hours_ago / 24.0 months_ago = days_ago / 30.0 years_ago = months_ago / 12.0 if years_ago > 0.9 and years_ago < 1.5 return sprintf(args[:year_ago_str], years_ago.to_i) elsif years_ago >= 1.5 return sprintf(args[:years_ago_str], years_ago.to_i) elsif months_ago > 0.9 and months_ago < 1.5 return sprintf(args[:month_ago_str], months_ago.to_i) elsif months_ago >= 1.5 return sprintf(args[:months_ago_str], months_ago.to_i) elsif days_ago > 0.9 and days_ago < 1.5 return sprintf(args[:day_ago_str], days_ago.to_i) elsif days_ago >= 1.5 return sprintf(args[:days_ago_str], days_ago.to_i) elsif hours_ago > 0.9 and hours_ago < 1.5 return sprintf(args[:hour_ago_str], hours_ago.to_i) elsif hours_ago >= 1.5 return sprintf(args[:hours_ago_str], hours_ago.to_i) elsif mins_ago > 0.9 and mins_ago < 1.5 return sprintf(args[:min_ago_str], mins_ago.to_i) elsif mins_ago >= 1.5 return sprintf(args[:mins_ago_str], mins_ago.to_i) elsif secs_ago >= 0.1 and secs_ago < 1.5 return sprintf(args[:sec_ago_str], secs_ago.to_i) elsif secs_ago >= 1.5 return sprintf(args[:secs_ago_str], secs_ago.to_i) end return args[:right_now_str] end |
#ampm ⇒ Object
Returns “am” or “pm” based on the hours values.
620 621 622 623 624 625 626 |
# File 'lib/datet.rb', line 620 def ampm if @t_hour <= 12 return "am" else return "pm" end end |
#code ⇒ Object
This returns a code-string that can be used to recreate the Datet-object.
Examples
code = datet.code #=> “1985061710000000000” newdatet = Datet.in(code) #=> 1985-06-17 10:00:00 +0200
1279 1280 1281 |
# File 'lib/datet.rb', line 1279 def code return "#{"%04d" % @t_year}#{"%02d" % @t_month}#{"%02d" % @t_day}#{"%02d" % @t_hour}#{"%02d" % @t_min}#{"%02d" % @t_sec}#{"%05d" % @t_usec}" end |
#day ⇒ Object
Returns the day in month as an integer.
643 644 645 646 |
# File 'lib/datet.rb', line 643 def day Thread.current[:datet_mode] = :days return @t_day end |
#day=(newday) ⇒ Object
Changes the day to a given day.
Examples
datet.time #=> 2005-05-03 17:46:11 0200 datet.day = 8 datet.time #=> 2005-05-08 17:46:11 0200
653 654 655 656 657 658 |
# File 'lib/datet.rb', line 653 def day=(newday) newday = newday.to_i raise ArgumentError, "Invalid day: '#{newday}'." if newday < 0 or newday > self.days_in_month @t_day = newday return self end |
#day_in_week(args = nil) ⇒ Object
Returns the day in the week.
Examples
Get a monday: datet = Datet.new(1970, 1, 4) datet.day_in_week #=> 1 datet.day_in_week(:mfirst = true) #=> 0
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/datet.rb', line 503 def day_in_week(args = nil) if args args.each do |key, val| case key when :mfirst else raise ArgumentError, "Invalid key in arguments: '#{key}'." end end end #This is a monday - 0. Use this date to calculate up against. def_date = Datet.new(1970, 1, 4) if self > def_date days = Datet.days_between(def_date, self) factor = days.to_f / 7.0 diw = days - (factor.floor * 7) else days = Datet.days_between(self, def_date) factor = days.to_f / 7.0 diw = days - (factor.floor * 7) diw = 7 - diw diw = 0 if diw == 7 end #Monday should be the first day in the week. if args and args[:mfirst] if diw == 0 diw = 6 else diw -= 1 end end return diw end |
#day_in_year ⇒ Object
Returns the current day in the year.
1635 1636 1637 1638 1639 1640 1641 1642 1643 |
# File 'lib/datet.rb', line 1635 def day_in_year day = self.day 1.upto(self.month - 1) do |month_no| day += Datet.days_in_month(self.year, month_no) end return day end |
#day_name ⇒ Object
Returns the days name as a string.
553 554 555 |
# File 'lib/datet.rb', line 553 def day_name return @@day_names[self.day_in_week] end |
#day_of_year ⇒ Object
Returns the day of the year (0-365) as an integer.
Examples
Datet.new.day_of_year #=> 123
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 |
# File 'lib/datet.rb', line 990 def day_of_year count = @t_day @@days_in_months.each_index do |key| break if key >= @t_month val = @@days_in_months[key] if key == 2 and Datet.gregorian_leap?(@t_year) count += 29 else count += val.to_i end end return count end |
#day_str(args = nil) ⇒ Object
Returns the day as a localized string.
Examples
Datet.new.day_str #=> “Monday” Datet.new.day_str(:short => true) #=> “Mon”
561 562 563 564 565 566 567 568 |
# File 'lib/datet.rb', line 561 def day_str(args = nil) ret = Datet.days(:trans => true)[self.day_in_week] if args and args[:short] ret = ret.slice(0, 3) end return ret end |
#days ⇒ Object
Sets the mode to days and gets ready to plus or minus.
Examples
datet.time #=> 2005-05-08 22:51:11 0200 datet.days 26 datet.time #=> 2005-06-03 22:51:11 +0200
891 892 893 894 |
# File 'lib/datet.rb', line 891 def days Thread.current[:datet_mode] = :days return self end |
#days_in_month ⇒ Object
Returns the number of days in the month.
Examples
datet = Datet.new print “There are #Datet.datetdatet.days_in_month days in the current month.”
475 476 477 |
# File 'lib/datet.rb', line 475 def days_in_month return Datet.days_in_month(@t_year, @t_month) end |
#days_in_year ⇒ Object
Returns the amount of days in the current year.
487 488 489 |
# File 'lib/datet.rb', line 487 def days_in_year return Datet.days_in_year(@t_year) end |
#dbstr(args = nil) ⇒ Object
Returns the time as a database-valid string.
Examples
datet.time #=> 2011-08-01 22:51:11 +0200 datet.dbstr #=> “2011-08-01 22:51:11” datet.dbstr(:time => false) #=> “2011-08-01”
940 941 942 943 944 945 946 947 948 |
# File 'lib/datet.rb', line 940 def dbstr(args = nil) str = "#{"%04d" % @t_year}-#{"%02d" % @t_month}-#{"%02d" % @t_day}" if !args or (!args.key?(:time) or args[:time]) str << " #{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec}" end return str end |
#find(args) ⇒ Object
Goes forward day-by-day and stops at a date matching the criteria given.
Examples
datet.time #=> 2012-05-03 19:36:08 +0200
Try to find next saturday. datet.find(:day, :day_in_week => 5) #=> 2012-05-05 19:36:08 +0200
Try to find next wednesday by Time’s wday-method. datet.find(:day, :wday => 3) #=> 2012-05-09 19:36:08 +0200
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/datet.rb', line 253 def find(args) #Test arguments. raise ArgumentError, "Too little arguments given: #{args.length}" if args.length < 2 args.each do |key, val| case key when :wday_mfirst, :wday, :incr else raise ArgumentError, "Invalid key in arguments: '#{key}'." end end #Find the date. count = 0 while true if args[:wday_mfirst] and self.day_in_week(:mfirst => true) == args[:wday_mfirst] return self elsif args[:wday] and self.day_in_week == args[:wday] return self end if args[:incr] == :day self.add_days(1) elsif args[:incr] == :month self.add_months(1) else raise ArgumentError, "Invalid increment: #{incr}." end count += 1 raise "Endless loop?" if count > 999 end end |
#hour ⇒ Object
Returns the hour as an integer.
593 594 595 |
# File 'lib/datet.rb', line 593 def hour return @t_hour end |
#hour=(newhour) ⇒ Object
Changes the hour to a given new hour.
Examples
datet.time #=> 2012-05-09 19:36:08 0200 datet.hour = 5 datet.time #=> 2012-05-09 05:36:08 0200
665 666 667 668 669 670 |
# File 'lib/datet.rb', line 665 def hour=(newhour) newhour = newhour.to_i raise ArgumentError, "Invalid hour: '#{newhour}'." if newhour < 0 or newhour > 24 @t_hour = newhour return self end |
#hour_thc ⇒ Object
Returns the hour as an integer in the twelve-hour-clock.
598 599 600 601 602 |
# File 'lib/datet.rb', line 598 def hour_thc hour = @t_hour hour -= 12 if hour > 12 return hour end |
#hours ⇒ Object
Sets the mode to hours and gets ready to plus or minus.
Examples
datet.time #=> 2005-05-08 17:46:11 0200 datet.hours 5 datet.time #=> 2005-05-08 22:46:11 +0200
859 860 861 862 |
# File 'lib/datet.rb', line 859 def hours Thread.current[:datet_mode] = :hours return self end |
#httpdate ⇒ Object
Returns the HTTP-date that can be used in headers and such.
Examples
datet.httpdate #=> “Mon, 17 Jun 1985 08:00:00 GMT”
1310 1311 1312 |
# File 'lib/datet.rb', line 1310 def httpdate return "#{self.day_name[0, 3]}, #{@t_day} #{self.month_name[0, 3]} #{@t_year} #{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec} GMT" end |
#human_str(args = {}) ⇒ Object
Returns the object as a human understandable string.
Examples
datet.time #=> 2012-05-03 20:31:58 +0200 datet.human_str #=> “20:31”
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 |
# File 'lib/datet.rb', line 1420 def human_str(args = {}) args = { :time => true, :number_endings => { 0 => "th", 1 => "st", 2 => "nd", 3 => "rd", 4 => "th", 5 => "th", 6 => "th", 7 => "th", 8 => "th", 9 => "th" } }.merge(args) now = Time.now #Generate normal string. date_str = "" if now.day != @t_day and now.month == @t_month and now.year == @t_year last_digit = @t_day.to_s[-1, 1].to_i if ending = args[:number_endings][last_digit] #ignore. else ending = "." end date_str << "#{@t_day}#{ending} " elsif now.day != @t_day or now.month != @t_month or now.year != @t_year date_str << "#{@t_day}/#{@t_month} " end if now.year != @t_year date_str << "#{@t_year} " end if args[:time] date_str << "#{@t_hour}:#{"%02d" % @t_min}" end return date_str end |
#lazy_hour=(newhour) ⇒ Object
Changes the hour to a new hour. If more than 60 is given, then the difference is converted into added days. If decimals are given, the minute of the timestamp is sat based on those decimals.
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/datet.rb', line 673 def lazy_hour=(newhour) newhour = newhour.to_f #Add days for every 24 hours given. if newhour > 24 or newhour < 0 days = (newhour.to_f / 24.0).floor newhour -= (days.to_f * 24.0) self.add_days(days) end #Convert any decimals to setting minute. diff = newhour - newhour.floor self.lazy_min = diff * 60 if diff > 0.0 #Set the actual hour. self.hour = newhour.floor self end |
#lazy_min=(newmin) ⇒ Object
Changes the minute to a new minute. If more than 60 is given, then the difference is converted into added hours. If decimals are given, the second of the timestamp is sat based on those decimals.
Examples
datet = Datet.new #=> 2012-07-16 19:53:07 datet.lazy_min = 30.5 #=> 2012-07-16 19:30:30 datet.lazy_min = 90.5 #=> 2012-07-16 20:30:30
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 |
# File 'lib/datet.rb', line 709 def lazy_min=(newmin) newmin = newmin.to_f #Add hours for every 60 minutes given. if newmin > 60 or newmin < 0 hours = (newmin.to_f / 60.0).floor newmin -= (hours.to_f * 60.0) self.add_hours(hours) end #Convert any decimals to setting the second. diff = newmin - newmin.floor self.lazy_sec = diff * 60 if diff > 0.0 #Set the actual minute. @t_min = newmin.floor self end |
#lazy_sec=(newsec) ⇒ Object
Changes the second to a given new second. If more than 60 is given, then the difference is converted into added minutes.
741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/datet.rb', line 741 def lazy_sec=(newsec) newsec = newsec.to_i if newsec > 60 or newsec < 0 mins = (newsec.to_f / 60.0).floor newsec -= (mins * 60) self.add_mins(mins) end @t_sec = newsec end |
#localtime_str ⇒ Object
Returns ‘localtime’ as of 1.9 - even in 1.8 which does it different.
Examples
datet.localtime_str #=> “1985-06-17 10:00:00 +0200”
1352 1353 1354 |
# File 'lib/datet.rb', line 1352 def localtime_str return "#{"%04d" % @t_year}-#{"%02d" % @t_month}-#{"%02d" % @t_day} #{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec} #{self.offset_str}" end |
#min ⇒ Object
Returns the minute as an integer.
605 606 607 |
# File 'lib/datet.rb', line 605 def min return @t_min end |
#min=(newmin) ⇒ Object
Changes the minute to a given new minute.
Examples
datet.time #=> 2012-05-09 05:36:08 0200 datet.min = 35 datet.time #=> 2012-05-09 05:35:08 0200
698 699 700 701 702 |
# File 'lib/datet.rb', line 698 def min=(newmin) newmin = newmin.to_i raise ArgumentError, "Invalid minute: '#{newmin}'." if newmin < 0 or newmin > 60 @t_min = newmin end |
#mins ⇒ Object
Sets the mode to minutes and gets ready to plus or minus.
Examples
datet.time #=> 2005-05-08 22:46:11 0200 datet.mins 5 datet.mins #=> 2005-05-08 22:51:11 +0200
869 870 871 872 |
# File 'lib/datet.rb', line 869 def mins Thread.current[:datet_mode] = :mins return self end |
#month ⇒ Object
Returns the month as an integer.
637 638 639 640 |
# File 'lib/datet.rb', line 637 def month Thread.current[:datet_mode] = :months return @t_month end |
#month=(newmonth) ⇒ Object
Changes the month to a given new month.
Examples
datet.time #=> 2012-05-09 05:35:20 0200 datet.month = 7 datet.time #=> 2012-07-09 05:35:20 0200
765 766 767 768 769 |
# File 'lib/datet.rb', line 765 def month=(newmonth) newmonth = newmonth.to_i raise ArgumentError, "Invalid month: '#{newmonth}'." if newmonth <= 0 or newmonth > 12 @t_month = newmonth end |
#month_name ⇒ Object
Returns the months name as a string.
583 584 585 |
# File 'lib/datet.rb', line 583 def month_name return @@month_names[@t_month] end |
#month_str ⇒ Object
Returns the month as a localized string.
Examples
Datet.new.month_str #=> “January”
573 574 575 576 577 578 579 580 |
# File 'lib/datet.rb', line 573 def month_str ret = Datet.months(:trans => true)[@t_month] if args and args[:short] ret = ret.slice(0, 3) end return ret end |
#months ⇒ Object
Sets the mode to months and gets ready to plus or minus.
Examples
datet.time #=> 2005-06-03 22:51:11 0200 datet.months 14 datet.time #=> 2006-08-01 22:51:11 +0200
901 902 903 904 |
# File 'lib/datet.rb', line 901 def months Thread.current[:datet_mode] = :months return self end |
#nullstamp? ⇒ Boolean
Returns true if this date is a nullstamp (0000-00-00 or something like it).
982 983 984 985 |
# File 'lib/datet.rb', line 982 def nullstamp? return true if @t_nil return false end |
#offset_info ⇒ Object
Returns various information about the offset as a hash.
Examples
datet.time #=> 1985-06-17 10:00:00 0200 datet.offset_info #=> {:sign=>“”, :hours=>2, :mins=>0, :secs=>0}
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 |
# File 'lib/datet.rb', line 1318 def offset_info offset_secs = self.time.gmt_offset offset_hours = (offset_secs.to_f / 3600.0).floor offset_secs -= offset_hours * 3600 offset_minutes = (offset_secs.to_f / 60.0).floor offset_secs -= offset_minutes * 60 if offset_hours > 0 sign = "+" else sign = "" end return { :sign => sign, :hours => offset_hours, :mins => offset_minutes, :secs => offset_secs } end |
#offset_str ⇒ Object
Returns the offset as a string.
Examples
datet.offset_str #=> “+0200”
1344 1345 1346 1347 |
# File 'lib/datet.rb', line 1344 def offset_str offset_info_data = self.offset_info return "#{offset_info_data[:sign]}#{"%02d" % offset_info_data[:hours]}#{"%02d" % offset_info_data[:mins]}" end |
#out(args = {}) ⇒ Object
Returns a string based on the date and time.
Examples
datet.out #=> “03/05 2012 - 18:04” datet.out(:time => false) #=> “03/05 2012” datet.out(:date => false) #=> “18:04”
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 |
# File 'lib/datet.rb', line 1048 def out(args = {}) str = "" date_shown = false time_shown = false if !args.key?(:date) or args[:date] date_shown = true str << "#{"%02d" % @t_day}/#{"%02d" % @t_month}" if !args.key?(:year) or args[:year] str << " #{"%04d" % @t_year}" end end if !args.key?(:time) or args[:time] show_time = true if args.key?(:zerotime) and !args[:zerotime] if @t_hour == 0 and @t_min == 0 show_time = false end end if show_time time_shown = true str << " - " if date_shown str << "#{"%02d" % @t_hour}:#{"%02d" % @t_min}" end end return str end |
#sec ⇒ Object
Returns the seconds as an integer.
610 611 612 |
# File 'lib/datet.rb', line 610 def sec return @t_sec end |
#sec=(newsec) ⇒ Object
Changes the second to a given new second.
Examples
datet.time #=> 2012-05-09 05:35:08 0200 datet.sec = 20 datet.time #=> 2012-05-09 05:35:20 0200
734 735 736 737 738 |
# File 'lib/datet.rb', line 734 def sec=(newsec) newsec = newsec.to_i raise ArgumentError, "Invalid second: '#{newsec}'." if newsec < 0 or newsec > 60 @t_sec = newsec.to_i end |
#secs ⇒ Object
Sets the mode to seconds and gets ready to plus or minus.
875 876 877 878 |
# File 'lib/datet.rb', line 875 def secs Thread.current[:datet_mode] = :secs return self end |
#stamp(args) ⇒ Object
Returns a new Datet- or Time-object based on the arguments.
Examples
time = datet.stamp(:datet => false, :min => 15, :day => 5) #=> 2012-07-05 05:15:20 +0200
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 |
# File 'lib/datet.rb', line 919 def stamp(args) vars = {:year => @t_year, :month => @t_month, :day => @t_day, :hour => @t_hour, :min => @t_min, :sec => @t_sec, :usec => @t_usec} args.each do |key, value| vars[key.to_sym] = value.to_i if key != :datet end time = Time.local(vars[:year], vars[:month], vars[:day], vars[:hour], vars[:min], vars[:sec], vars[:usec]) if !args.key?(:datet) or args[:datet] return Datet.new(time) end return time end |
#strftime(str) ⇒ Object
See the documentation for Time#strftime. It emulates that method. This method is not complete yet and only contains some functionality.
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 |
# File 'lib/datet.rb', line 1469 def strftime(str) replaces = {} res = "#{str}" str.scan(/(%%|%(\^|)([A-z]))/) do |match| #The does the functionality of actually having a '%' in the string. if match[0] == "%%" replaces["%%"] = "%" next end #Set up replace-hash for later replace. bigsign = match[1] letter = match[2] le = "%#{bigsign}#{letter}" #Skip if letter has already been set. next if replaces.key?(le) case letter when "Y" replaces[le] = @t_year when "m" replaces[le] = "%02d" % @t_month when "d" replaces[le] = "%02d" % @t_day when "e" replaces[le] = @t_day when "H" replaces[le] = "%02d" % @t_hour when "k" replaces[le] = @t_hour when "l" replaces[le] = self.hour_thc when "I" replaces[le] = "%02d" % self.hour_thc when "M" replaces[le] = "%02d" % @t_min when "S" replaces[le] = "%02d" % @t_sec when "T" replaces[le] = "#{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec}" when "R" replaces[le] = "#{"%02d" % @t_hour}:#{"%02d" % @t_min}" when "r" replaces[le] = "#{"%02d" % self.hour_thc}:#{"%02d" % @t_min}:#{"%02d" % @t_sec} #{self.ampm.upcase}" when "B" replaces[le] = self.month_name when "b", "h" replaces[le] = self.month_name[0, 3] when "j" replaces[le] = self.day_of_year when "A" replaces[le] = self.day_name when "a" replaces[le] = self.day_name[0, 3] when "w" replaces[le] = self.day_in_week when "u" replaces[le] = self.day_in_week(:mfirst => true) + 1 when "s" replaces[le] = self.to_i when "p" replaces[le] = self.ampm.upcase when "P" replaces[le] = self.ampm when "V" replaces[le] = self.week_no when "W" replaces[le] = self.week_no(:mfirst => true) end #Replace should be uppercase. if bigsign == "^" and replaces.key?(le) replaces[le] = replaces[le].to_s.upcase end end #Do the actual replaces. replaces.each do |key, val| res = res.gsub(key, val.to_s) end #Return the replaced string. return res end |
#time ⇒ Object Also known as: to_time
Returns a new ‘Time’-object based on the data of the ‘Datet’-object.
Examples
Datet.new.time #=> 2012-07-13 16:14:27 +0200
237 238 239 |
# File 'lib/datet.rb', line 237 def time return Time.new(@t_year, @t_month, @t_day, @t_hour, @t_min, @t_sec) end |
#to_a ⇒ Object
Returns arguments in an array.
1303 1304 1305 |
# File 'lib/datet.rb', line 1303 def to_a return [@t_year, @t_month, @t_day, @t_hour, @t_min, @t_sec, @t_usec] end |
#to_f ⇒ Object
Returns the unix timestamp for this object as a float (uses ‘Time’ to calculate).
Examples
Datet.new.to_f #=> 1342381158.0
1293 1294 1295 |
# File 'lib/datet.rb', line 1293 def to_f return self.time.to_f end |
#to_i ⇒ Object
Returns the unix timestamp for this object (uses ‘Time’ to calculate).
Examples
datet.to_i #=> 487843200
1286 1287 1288 |
# File 'lib/datet.rb', line 1286 def to_i return self.time.to_i end |
#to_s ⇒ Object
Returns a string that describes the object.
1298 1299 1300 |
# File 'lib/datet.rb', line 1298 def to_s return "#{"%04d" % @t_year}-#{"%02d" % @t_month}-#{"%02d" % @t_day} #{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec}" end |
#update_from_str(str) ⇒ Object
Updates the date-data from a given string.
Examples
datet.dbstr #=> '2012-09-05 01:00:00'
datet.update_from_str("10:00")
datet.dbstr #=> '2012-09-05 10:00:00'
datet.update_from_str("1/2")
datet.dbstr #=> '2012-02-01 10:00:00'
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 |
# File 'lib/datet.rb', line 1564 def update_from_str(str) str = str.to_s if match = str.match(/^\s*(\d+)\s*:\s*(\d+)\s*$/) self.hour = match[1].to_i self.min = match[2].to_i elsif match = str.match(/^\s*(\d+)\s*\/\s*(\d+)(\s*|\s+(\d+)\s*)$/) self.day = match[1].to_i self.month = match[2].to_i year_no = match[4].to_i self.year = year_no if year_no > 0 elsif datet = Datet.in(str) rescue false self.update_from_time(datet.time) else raise "Could not understand given string: '#{str}'." end return self end |
#update_from_time(time) ⇒ Object
Updates the current variables to the given time.
Examples
datet.update_from_time(Time.now)
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/datet.rb', line 222 def update_from_time(time) @t_year = time.year @t_month = time.month @t_day = time.day @t_hour = time.hour @t_min = time.min @t_sec = time.sec @t_usec = time.usec nil end |
#usec ⇒ Object
Returns the microsecond as an integer.
615 616 617 |
# File 'lib/datet.rb', line 615 def usec return @t_usec end |
#usec=(newusec) ⇒ Object
Changes the usecond of the object.
754 755 756 757 758 |
# File 'lib/datet.rb', line 754 def usec=(newusec) newusec = newusec.to_i raise ArgumentError, "Invalid usecond: '#{newusec}'." if newusec < 0 or newusec > 1000000 @t_usec = newusec end |
#usecs ⇒ Object
Sets the mode to mili-seconds and gets ready to plus or minus.
881 882 883 884 |
# File 'lib/datet.rb', line 881 def usecs Thread.current[:datet_mode] = :usecs return self end |
#week_no(args = nil) ⇒ Object
Returns the week-number of the year (1..53).
542 543 544 545 546 547 548 549 550 |
# File 'lib/datet.rb', line 542 def week_no(args = nil) week_no = (self.day_of_year.to_f / 7).to_i + 1 if args and args[:mfirst] and self.day_in_week == 1 week_no -= 1 end return week_no end |
#year ⇒ Object
Returns the year as an integer.
588 589 590 |
# File 'lib/datet.rb', line 588 def year return @t_year end |
#year=(newyear) ⇒ Object
Changes the year to the given year. datet = Datet.now #=> 2014-05-03 17:46:11 0200 datet.year = 2005 datet.time #=> 2005-05-03 17:46:11 0200
632 633 634 |
# File 'lib/datet.rb', line 632 def year=(newyear) @t_year = newyear.to_i end |
#years ⇒ Object
Sets the mode to years and gets ready to plus or minus.
Examples
datet.time #=> 2006-08-01 22:51:11 0200 datet.years 5 datet.time #=> 2011-08-01 22:51:11 +0200
911 912 913 914 |
# File 'lib/datet.rb', line 911 def years Thread.current[:datet_mode] = :years return self end |