7

Given a any unix timestamp (i.e. 1306396801) which translates to 26.05.2011 08:00:01, how can I determine if this is within a given timeframe (i.e. 08:00:00 and 16:00:00)?

This needs to work for any day. I just want to know if this timestamp is within the given time-interval, on any future (or past) day, the date is unimportant. I don't care if it is on the 25th or 26th, as long as it is between 08:00 and 16:00.

I am on the lookout for a java solution, but any pseudo code that works will be ok, I'll just convert it.

My attempts so far has been converting it to a java Calendar, and reading out the hour/min/sec values and comparing those, but that just opened up a big can of worms. If the time interval I want it between is 16.30, I can't just check for tsHour > frameStartHour && tsMin > frameStartMin as this will discard any timestamps that got a minute part > 30.

Thank you for looking at this :)

To clarify. I am only using and referring to UTC time, my timestamp is in UTC, and the range I want it within is in UTC.

NightDog
  • 135
  • 1
  • 7

5 Answers5

6

I think I understand what you want. You want to test for any day, if it's between 8am and 4pm UTC. Take the timestamp mod 24*3600. This will give you the number of seconds elapsed in the day. Then you just compare that it's between 8*3600 and 16*3600. If you need to deal with timezones, things get more complicated.

Joshua Martell
  • 7,074
  • 2
  • 30
  • 37
2

Given your timestamp (in seconds) and the desired time zone, Jodatime gives you the hour which leads you to a simple integer range check.

new org.joda.time.DateTime(timestamp*1000L, zone).getHourOfDay()

With java.util.* its more difficult.

Wolfgang
  • 2,367
  • 23
  • 29
  • Is this can of worms (if/elseif until my eyes bleed) the only/best solution? – NightDog May 28 '11 at 18:18
  • Well, it's just (hour>=8) && (hour<16). But if you want to consider minutes, too, than you will like to convert the DateTime to a LocalTime and use the isAfter() and isBefore() methods. Feel yourself lucky that your time span is not over midnight. I had this task once and it gave me a headache! ;-) – Wolfgang May 28 '11 at 18:25
  • Well, I ended up doing it this way, sadly. Maybe jodatime got some more consistent Calendar.after() when setting some of the fields to zero, but I did not try that. Brute force all the way... :P – NightDog May 28 '11 at 22:34
  • And after 2 hours of doing it this way, I came back and found the mod solution! – NightDog May 29 '11 at 01:12
  • This hardly seems like the type of task that demands a 3rd party library. – arkon Aug 07 '15 at 02:38
2

If I understood you correctly, you only need to normalize your dates to some common value. Create three instances of Calendar - one with your time, but day, month, and year set to zero, and two with start and end of your timeframe, other fields also zeroed. Then you can use Calendar.after() and Calendar.before() to see if the date is within the range.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
  • I did some testing on this, and with Calendar I got inconsistent results. Even if I sat everything to 0 it gave me nothing I could use. I ended up checking every value. – NightDog May 28 '11 at 22:35
1

Your unix timestamp is an absolute time. Your time frame is relative. You need some kind of time zone information in order to solve this problem. I just answered some of this for PostgreSQL a few minutes ago. Hopefully that article is of use.

Community
  • 1
  • 1
Sean
  • 9,888
  • 4
  • 40
  • 43
  • OK, I think I get the part about relative time, but both my timestamp and the hour/min/sec range I want it within are UTC. – NightDog May 28 '11 at 18:13
0

Convert the beginning of your range to a unix timestamp, and the end of your range to a unix tmestamp, then it's a simple integer check.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Yes and no. If I want to do this only for 1 day, then that is a piece of cake. But that is not what I want, I want to get "true" if this timestamp got a hour and minute part between a set interval. – NightDog May 28 '11 at 18:09