1

I am working with Carbon Black (cbapi) and in that, i have it iterating through processes on a computer and listing the start date of the process and a bunch of other details about the process. Issuing this command print eachProcName.start it prints the start date like this: 2015-06-08 12:05:48.835000

Now, my problem is that I want to create a filter so that it only displays between the dates that a user specifies. The user could enter the start and end date and the program would print all the processes between the starting and ending date. Something like if eachProcName.start > startDate and eachProcName.start < endDate

I have tried a number of thngs, such as strftime, and a few other methods of trying to convert the date and time the process was started to a string or a group of integers, but I've tried everything I know how to do, so I was wondering if someone could explain the easiest way to do this and where I went wrong. Thank you!

Alan
  • 53
  • 8
  • What format is the date? Try `print type(eachProcName.start)` to find out. – Mark Ransom Jun 09 '15 at 16:56
  • May we see what you tried? [ask] – boardrider Jun 09 '15 at 16:59
  • The python dateutil library is very powerful and might help: https://labix.org/python-dateutil it can parse/manage dates+times in all sorts of formats. See: http://stackoverflow.com/questions/9516025/parsing-datetime-in-python – Ricky Mutschlechner Jun 09 '15 at 17:02
  • Its a datetime.datetime – Alan Jun 09 '15 at 17:13
  • Have you tried to compare timestamps? – Miskov Jun 09 '15 at 17:24
  • @Miskov, No, I have not. Let me look into that. – Alan Jun 09 '15 at 17:26
  • You can use `timestamp = (dt - datetime(1970, 1, 1)).total_seconds()` where `dt` is your datetime.datetime – Miskov Jun 09 '15 at 17:28
  • the result should look like this: `start_time <= process.start_time < end_time`: it works if all variables are aware datetime objects or naive datetime objects that represent time in UTC (you could also work with float "seconds since the epoch" values but it is less convenient). – jfs Jun 09 '15 at 17:32
  • @Miskov: it works iff `dt` is a utc time. – jfs Jun 09 '15 at 17:33
  • @AR917: enumerate methods that you've tried. It is not clear that you understand the difference between a string and a datetime object. – jfs Jun 09 '15 at 17:34
  • @J.F. Sebastian: I have this [StackOverflow link](http://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python), so AR917 can pick a right one. EDIT: By You, Internet is so small :) – Miskov Jun 09 '15 at 17:35
  • 1
    @Miskov: the point is that start and end dates are provided by a user i.e., it is unlikely that they are already in UTC (most likely that users input dates in their local timezones) and therefore [How do I convert local time to UTC in Python?](http://stackoverflow.com/q/79797/4279) should be used first. – jfs Jun 09 '15 at 17:40
  • @J.F. Sebastian: You are absolutely right. – Miskov Jun 09 '15 at 17:41
  • @AR917: make sure you understand issues with local time described in: [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/a/26313848/4279) – jfs Jun 09 '15 at 17:50
  • @J.F.Sebastian, I figured it out using this code: `dstartDate = datetime.datetime.strptime(startDate, '%Y %m %d')` to make the string into a datetime, and it seemed to work. Thank you all very much for the help! – Alan Jun 09 '15 at 17:53
  • @AR917: do read the links that I've provided otherwise *your application may be wrong by a day* due to different timezone rules at different dates in various timezones. – jfs Jun 09 '15 at 18:09
  • I understand. I will definitely check them out. Thank you for the excellent resources! – Alan Jun 09 '15 at 18:15

0 Answers0