1

How do you convert a str to timedelta?

I am trying to compare 2 different times from two different sources. Very similar to this Compare two dates with datetime.timedelta

But that answer is super unclear

error:

not supported between instances of 'datetime.timedelta' and 'str'

When it reads this line of code:

elif elapsed_time > maxtime_heatac:

Even though I set them the same way.

start_time = central.localize(d)

and

currentt = current.localize(d)

How do I convert a str to timedelta so I can do my comparison?

                                if plant_warning_flag == 0:
                                        d = datetime.fromtimestamp(0)
                                        central = pytz.timezone('US/Central')
                                        start_time = central.localize(d)
                                        #start_time = dt.datetime.now("{dt.datetime.now(tz=dt.timezone.utc):%H:%M:%S}")
                                        plant_warning_flag = 1
                                        #print("start_time ", start_time)
                                current = pytz.timezone('US/Central')
                                currentt = current.localize(d)
                                elapsed_time = currentt - start_time
                                print("current ", current, "start_time ", start_time)
                                print("\nelapsed_time: ", elapsed_time, "maxtime_heatac: ", maxtime_heatac)
                                elif elapsed_time > maxtime_heatac:
                                        payload = {'on1':'4000'}
                                        session = requests.Session()
                                        session.post(acserver_url,headers=headers,data=payload)

The output of the format is ideal for me

elapsed_time:  0:00:00 maxtime_heatac:  00:00:30

So I don't want to reformat it, just compare.

Attempt update:

I tried

elapsed_time = datetime.strptime(elapsedtmp_time, '%H-%M-%S')

But I get this error

TypeError: strptime() argument 1 must be str, not datetime.timedelta

and I cannot get past that one.

brad
  • 870
  • 2
  • 13
  • 38
  • This article may be helpful.[how to convert string to datetime.timedelta()?](https://stackoverflow.com/q/8365380/9014308) – kunif Jan 23 '21 at 15:37
  • @kunif Thank you, but this is as far as I can get. TypeError: strptime() argument 1 must be str, not datetime.timedelta – brad Jan 23 '21 at 15:49
  • using -> elapsed_time = datetime.strptime(elapsedtmp_time, '%H-%M-%S') – brad Jan 23 '21 at 15:50
  • Isn't the target to be processed wrong? Is `maxtime_heatac` a string? If the error occurs with `elif elapsed_time> maxtime_heatac:`, it's probably a good idea to convert `maxtime_heatac` to `timedelta`. – kunif Jan 23 '21 at 16:08
  • @kunif You are failing to read the url you sent me. " 10 You wouldn't convert date_select to a timedelta, instead, you need a datetime object, which can be added to a timedelta to produce an updated datetime object:" – brad Jan 23 '21 at 16:40
  • I tried to convert the string to delta, many times and many ways. They all fail. According to the URL you sent me, it is impossible to convert from object to delta – brad Jan 23 '21 at 16:41
  • This article is in Japanese, but [this answer](https://ja.stackoverflow.com/a/56444/26370) may be helpful. – kunif Jan 23 '21 at 16:52
  • @kunif according to the first URL you sent me, you cannot convert to delta. I applied the suggestion on the URL you sent and now my counter fails. I never count higher than 00:00:00 – brad Jan 23 '21 at 17:43
  • newmthactime = datetime.strptime('2021-01-01 '+maxtime_heatac, '%Y-%m-%d %H:%M:%S') - datetime(2021,1,1) – brad Jan 23 '21 at 17:43
  • Why is this url still removed? It obviously is nothing like the other one. – brad Jan 23 '21 at 18:13
  • When you say "_Why is this url still removed_?" are you asking why your question is still closed as a duplicate? If so, you should try to express yourself better, because it took me a while to understand that you were protesting against closure. I agree that the original duplicate target wasn't good (because the title and body of that question don't match). I've added another duplicate target that actually answers what you're asking, please check its answers. – Andras Deak -- Слава Україні Jan 23 '21 at 21:52
  • @brad , assuming that the value of `maxtime_heatac` is `00:00:30` in the `string`, both of the two answers in the second URL I introduced can be converted to `00:00:30` in `timedelta`. – kunif Jan 23 '21 at 23:07
  • @kunif I am here https://bpa.st/52B5W – brad Jan 24 '21 at 05:05
  • As you can see in the source link provided and in the question itself, `str(elapsed_time)` is `0:00:15` and `maxtime_heatac` is `00:00:30`, and the number of digits in the hour part is different. Therefore, in the comparison as a character string, `0: > 00` in the second digit is always `True`. It can be difficult to specify the format when converting `timedelta` to a `string`, so it's a good idea to store `maxtime_heatac` as `timedelta` instead of a `string` and compare it directly to the non-string `elapsed_time`. – kunif Jan 24 '21 at 05:48
  • @kunif How do I get "elapsed_time = new_current_time - new_start_time" in 00:00:00 format and not 0:00:00? I am having zero luck with deltas. It would be easier to add the extra 0. Deltas mess up my formatting and cannot compare it with 00:03:00 which is a requirement – brad Jan 24 '21 at 06:22
  • As I said again, there is no problem if both timedeltas are compared. If you really want to compare as a string, try defining and using a dedicated conversion function as in this article. [Formatting timedelta objects \[duplicate\]](https://stackoverflow.com/q/8906926/9014308) – kunif Jan 24 '21 at 06:40
  • @kunif yes there is. I am not comparing 2 deltas, I am comparing the elapsed time which is NOT a clock time, it is a timer. I will be comparing it to another none time. 00:03:00. Delta does nothing for me. – brad Jan 24 '21 at 07:11
  • You should understand what you are dealing with. `start_time` and `currentt` are `datetime` objects, and the result of subtraction of the `datetime` object, `elapsed_time`, is a `timedelta` object. The correct program cannot be made from the wrong knowledge. – kunif Jan 24 '21 at 12:34

0 Answers0