lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, May 08, 2009 at 06:22:29PM +0000, Diego - Red Baires wrote:
> Hi guys, i still cannot apply the concept that Kein recommends.

Hm, I may be missing something here, but what I think you want
to do (according to your first description) should not be so
complicated. As Kein suggested, use os.date('*t',time) to get
the hour of the day &c.


  function time2day (t)
      return math.floor( t / 86400 ) -- seconds per day
  end
  
  function date2day (y, m, d)
      local time = os.time{ year=y, month=m, day=d }
      return time2day( time )
  end
  
  test_interval = {
      first_day  = date2day( 2009, 05, 01 );
      last_day   = date2day( 2009, 05, 31 ); -- inclusive
      start_hour = 15;
      stop_hour  = 16; -- exclusive
  }
  
  function is_active( interval, time )
      time = time or os.time() -- default value for time
      
      local day = time2day( time )
      if (day < interval.first_day) or (interval.last_day < day) then
          return false
      end
      
      local hour = os.date( '*t', time ).hour
      if (hour < interval.start_hour) or (interval.stop_hour <= hour) then
          return false
      end
      
      return true
  end
  
  print( is_active( test_interval )) -- true in May 2009 from 3pm to 4pm




-- 
Tommy Pettersson <ptp@lysator.liu.se>