From: Ryan Davis Date: 2011-07-03T06:12:27+09:00 Subject: Re: Renages On Jul 2, 2011, at 13:37 , Peter Hicks wrote: > 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. It is very much a simple mapping, it just isn't a 1:1 mapping anymore. All you need to do is figure out which hour and half hour of the day it is, and return the code for that. Given a time t, `t.min / 30` gives you 0 or 1 based on which half hour you're in. t.hour gives you 0 to 23 based on which hour you're in. So: t.hour * 2 + (t.hour / 30) Gives you a number from 0 to 47. From there you can either look it up from a pre-populated array or hash or whatever... xlate = "00112233445566ABCDEFGHIJKLMNOPQRSTUVWXYZ778899".split(//) t = Time.now p [t.hour, t.min] # => [14, 10] p xlate[t.hour * 2 + (t.hour / 30)] # => "O"