0

I want to 'classify' timestamps to either A or B, where A and B are sets of timeframes. More specifically, I have a few dates in the following format '2014-11-23T12:00:00.000+04:00'. What I would like to do is to find whether each of them 'belongs' either to set [between 11 and 16 on Monday or 11 and 16 Tuesday] or to set [between 11 Saturday to 16 on Sunday] or to non of these sets. Any ideas how to implement this in Python?

EDIT:

The following code works fine for the provided date string, however, it is not complied with my date string as I use a slightly different format (i.e. '2014-11-23T12:00:00.000+04:00')

from datetime import datetime
new_time = datetime.strptime('2012-03-01T10:00:00Z','%Y-%m-%dT%H:%M:%SZ')
print(new_time)
user706838
  • 5,132
  • 14
  • 54
  • 78
  • 1
    You can use the [`datetime`](https://docs.python.org/2/library/datetime.html) module to convert your timestamps to `datetime` objects. Then you can do [greater than and less than](http://stackoverflow.com/questions/8142364/how-to-compare-two-dates) comparisons between timestamps, to see if it is in a certain range. – Cory Kramer Dec 11 '14 at 12:33
  • I have updated my question based on my findings. Can you please have a look? – user706838 Dec 11 '14 at 13:43
  • You may want to check out the [arrow](http://crsmithdev.com/arrow/) library for manipulating datetimes. – Wayne Werner Dec 11 '14 at 13:47
  • @WayneWerner seems nice. It can read the provided date string very easily. However, it doesn't seem to support string day formats such as 'Sat', 'Sun' and so on. Any ideas? – user706838 Dec 11 '14 at 15:48
  • Write a simple dictionary, e.g. `DAYS_OF_WEEK = {'Thurs': 3, ... }` – Wayne Werner Dec 11 '14 at 17:02

2 Answers2

0

Python has nice rich comparisons. Specifically, you can write:

if low < myval < high:
    print("{} is between {} and {}".format(myval, low, high))

And it works exactly as you expect.

If you run dir(new_time) you'll find a list of interesting looking properties, like weekday, and hour. So you could do something like

THURSDAY = 3

if new_time.weekday == THURSDAY and low_time < new_time.hour < high_time:
    print("{} is on Thursday between {} and {}".format(new_time,
                                                       low_time,
                                                       high_time))

You also might want to make sure that .minute/.second/.microsecond aren't > 0. Of course if it's a specific Monday or Friday you're looking for, you could easily create your times like so:

monday_at_1100 = datetime(2014, 12, 8, 11)
monday_at_1600 = datetime(2014, 12, 8, 16)

if monday_at_1100 < new_time < monday_at_1600:
    do_something()
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
0

Two general approaches that scale:

Reduce the precision of your data.

Map timetstamp 12:13:14 to 12:*****, then you can have a comprehensive set of timestamp in native Python set:

"12:*****" in set(["12:*****", "17:*****"])
True

Use binary tree or heap or skip list or similar hierarchical data structure

        (12:00, 17:59)
          /       \
(12:00, 12:59)  (17:00, 17:59)
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120