Python timedelta issue with negative values

If you are using Python 2.7 or higher you can use timedelta.total_seconds() to get a float representation of the timedelta as a positive or negative number of seconds. >>> datetime.timedelta(-1, 86100).total_seconds() -300.0 You should be able to use this to calculate a number of minutes fairly easily. If you are not using Python 2.7 you … Read more

Adding seconds to a in PostgreSQL

The trick is to create a fixed interval and multiply it with the number of seconds in the column: SELECT start_time, expiration_time_seconds, start_time + expiration_time_seconds * interval ‘1 second’ FROM whatever ORDER BY start_time; start_time | expiration_time_seconds | end_time —————————-|————————-|—————————- 2014-08-05 08:23:32.428452 | 172800 | 2014-08-07 08:23:32.428452 2014-08-10 09:49:51.082456 | 3600 | 2014-08-10 10:49:51.082456 2014-08-13 … Read more

How do you convert a datetime/timestamp from one timezone to another timezone?

If you know your origin timezone and the new timezone that you want to convert it to, it turns out to be very straightforward: Make two pytz.timezone objects, one for the current timezone and one for the new timezone e.g. pytz.timezone(“US/Pacific”). You can find a list of all official timezones in pytz library: import pytz; … Read more

How to add delta to python datetime.time?

datetime.time objects do not support addition with datetime.timedeltas. There is one natural definition though, clock arithmetic. You could compute it like this: import datetime as dt now = dt.datetime.now() delta = dt.timedelta(hours = 12) t = now.time() print(t) # 12:39:11.039864 print((dt.datetime.combine(dt.date(1,1,1),t) + delta).time()) # 00:39:11.039864 dt.datetime.combine(…) lifts the datetime.time t to a datetime.datetime object, the … Read more

Comparing a time delta in python

You’ll have to create a new timedelta with the specified amount of time: d > timedelta(minutes=1) Or this slightly more complete script will help elaborate: import datetime from time import sleep start = datetime.datetime.now() sleep(3) stop = datetime.datetime.now() elapsed = stop – start if elapsed > datetime.timedelta(minutes=1): print “Slept for > 1 minute” if elapsed … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)