From: John W Higgins Date: 2011-07-03T06:13:21+09:00 Subject: Re: Renages --000e0ce02aaa39255904a71c9adb Content-Type: text/plain; charset=ISO-8859-1 Afternoon, On Sat, Jul 2, 2011 at 1:37 PM, Peter Hicks wrote: > All, > > I have a requirement to translate a time between 0000 and 2359 in to a > single character 'interval ID' based on the time. For example, if the > time is between 0000 and 0059, '0' should be returned, if it's between > 0100 and 0159, '1' should be return. The problem is that 0700-0729 > should return 'A', 0730-0759 should return 'B', i.e. it's not a simple > mapping. > > I have the following, but I can't help thinking there's a much more > efficient way of doing it - can anyone advise, as I'm a little stuck! > > xlate = [ [ '0000', '0059', '0' ], [ '0100', '0159', '1' ], > [ '0200', '0259', '2' ], [ '0300', '0359', '3' ], > [ '0400', '0459', '4' ], [ '0500', '0559', '5' ], > [ '0600', '0659', '6' ], [ '0700', '0729', 'A' ], > [ '0730', '0759', 'B' ], [ '0800', '0829', 'C' ], > [ '0830', '0859', 'D' ], [ '0900', '0929', 'E' ], > [ '0930', '0959', 'F' ], [ '1000', '1029', 'G' ], > [ '1030', '1059', 'H' ], [ '1100', '1129', 'I' ], > [ '1130', '1159', 'J' ], [ '1200', '1229', 'K' ], > [ '1230', '1259', 'L' ], [ '1300', '1329', 'M' ], > [ '1330', '1359', 'N' ], [ '1400', '1429', 'O' ], > [ '1430', '1459', 'P' ], [ '1500', '1529', 'Q' ], > [ '1530', '1559', 'R' ], [ '1600', '1629', 'S' ], > [ '1630', '1659', 'T' ], [ '1700', '1729', 'U' ], > [ '1730', '1759', 'V' ], [ '1800', '1829', 'W' ], > [ '1830', '1859', 'X' ], [ '1900', '1959', 'Y' ], > [ '2000', '2059', 'Z' ], [ '2100', '2159', '7' ], > [ '2200', '2259', '8' ], [ '2300', '2359', '9' ] ] > > xlate.each do |range| > return range[2] if time >= range[0] && time <= range[1] > end > > How about this #Setup a string with all 48 options (splitting into half hours) results = "00112233445566ABCDEFGHIJKLMNOPQRSTUVWXYYZZ778899" #Convert our time into which of the 48 half hour slots it fits time = '1734' slot = (time[0..1].to_i * 60 + time[2..3].to_i) / 30 #> 35 #Return the result for the slot results[slot] #> V John --000e0ce02aaa39255904a71c9adb--