timedelta

Django: Using F arguments in datetime.timedelta inside a query

强颜欢笑 提交于 2019-11-26 21:37:17
问题 Using Django model syntax, if I do this: ThatModel.objects.filter( last_datetime__lte=now + datetime.timedelta(seconds=F("interval"))) I get: TypeError: unsupported type for timedelta days component: ExpressionNode Is there a way to make this work with pure Django syntax (and not parsing all the results with Python)? 回答1: From django docs: Django provides F expressions to allow such comparisons. Instances of F() act as a reference to a model field within a query. These references can then be

Python timedelta in years

。_饼干妹妹 提交于 2019-11-26 21:33:45
I need to check if some number of years have been since some date. Currently I've got timedelta from datetime module and I don't know how to convert it to years. Rick Copeland You need more than a timedelta to tell how many years have passed; you also need to know the beginning (or ending) date. (It's a leap year thing.) Your best bet is to use the dateutil.relativedelta object , but that's a 3rd party module. If you want to know the datetime that was n years from some date (defaulting to right now), you can do the following:: from dateutil.relativedelta import relativedelta def yearsago(years

How to get the sum of timedelta in Python?

别说谁变了你拦得住时间么 提交于 2019-11-26 21:29:25
问题 Python: How to get the sum of timedelta? Eg. I just got a lot of timedelta object, and now I want the sum. That's it! 回答1: datetime combine method allows you to combine time with a delta datetime.combine(date.today(), time()) + timedelta(hours=2) timedelta can be combined using usual '+' operator >>> timedelta(hours=3) datetime.timedelta(0, 10800) >>> timedelta(hours=2) datetime.timedelta(0, 7200) >>> >>> timedelta(hours=3) + timedelta(hours=2) datetime.timedelta(0, 18000) >>> You can read

Python: Convert timedelta to int in a dataframe

早过忘川 提交于 2019-11-26 18:33:12
I would like to create a column in a pandas data frame that is an integer representation of the number of days in a timedelta column. Is it possible to use 'datetime.days' or do I need to do something more manual? timedelta column 7 days, 23:29:00 day integer column 7 Use the dt.days attribute. Supposing td is the name of your timedelta Series, access this attribute via: td.dt.days You can also get the seconds and microseconds attributes in the same way. You could do this, where td is your series of timedeltas. The division converts the nanosecond deltas into day deltas, and the conversion to

Calculating the difference between two Java date instances

谁说胖子不能爱 提交于 2019-11-26 17:58:51
I'm using Java's java.util.Date class in Scala and want to compare a Date object and the current time. I know I can calculate the delta by using getTime(): (new java.util.Date()).getTime() - oldDate.getTime() However, this just leaves me with a long representing milliseconds. Is there any simpler, nicer way to get a time delta? notnoop The JDK Date API is horribly broken unfortunately. I recommend using Joda Time library . Joda Time has a concept of time Interval : Interval interval = new Interval(oldTime, new Instant()); EDIT: By the way, Joda has two concepts: Interval for representing an

Python format timedelta greater than 24 hours for display only containing hours?

我是研究僧i 提交于 2019-11-26 17:20:03
问题 How do I format timedelta greater than 24 hours for display only containing hours in Python? >>> import datetime >>> td = datetime.timedelta(hours=36, minutes=10, seconds=10) >>> str(td) '1 day, 12:10:10' # my expected result is: '36:10:10' I acheive it by: import datetime td = datetime.timedelta(hours=36, minutes=10, seconds=10) seconds = td.total_seconds() hours = seconds // 3600 minutes = (seconds % 3600) // 60 seconds = seconds % 60 str = '{}:{}:{}'.format(int(hours), int(minutes), int

Convert a timedelta to days, hours and minutes

不羁的心 提交于 2019-11-26 15:47:24
I've got a timedelta. I want the days, hours and minutes from that - either as a tuple or a dictionary... I'm not fussed. I must have done this a dozen times in a dozen languages over the years but Python usually has a simple answer to everything so I thought I'd ask here before busting out some nauseatingly simple (yet verbose) mathematics. Mr Fooz raises a good point. I'm dealing with "listings" (a bit like ebay listings) where each one has a duration. I'm trying to find the time left by doing when_added + duration - now Am I right in saying that wouldn't account for DST? If not, what's the

How to construct a timedelta object from a simple string

我只是一个虾纸丫 提交于 2019-11-26 15:10:54
I'm writing a function that needs a timedelta input to be passed in as a string. The user must enter something like "32m" or "2h32m", or even "4:13" or "5hr34m56s"... Is there a library or something that has this sort of thing already implemented? virhilo for the 4:13, and other standard formats(but if you don't know which one) use dateutil.parser.parse from python-dateutil For the first format(5hr34m56s), you should parse using regular expressions Here is re-based solution: import re from datetime import timedelta regex = re.compile(r'((?P<hours>\d+?)hr)?((?P<minutes>\d+?)m)?((?P<seconds>\d+?

How to add delta to python datetime.time?

寵の児 提交于 2019-11-26 06:44:25
问题 From: http://docs.python.org/py3k/library/datetime.html#timedelta-objects A timedelta object represents a duration, the difference between two dates or times. So why i get error with this: >>> from datetime import datetime, timedelta, time >>> datetime.now() + timedelta(hours=12) datetime.datetime(2012, 9, 17, 6, 24, 9, 635862) >>> datetime.now().date() + timedelta(hours=12) datetime.date(2012, 9, 16) >>> datetime.now().time() + timedelta(hours=12) Traceback (most recent call last): File \"

Convert a timedelta to days, hours and minutes

穿精又带淫゛_ 提交于 2019-11-26 05:57:22
问题 I\'ve got a timedelta. I want the days, hours and minutes from that - either as a tuple or a dictionary... I\'m not fussed. I must have done this a dozen times in a dozen languages over the years but Python usually has a simple answer to everything so I thought I\'d ask here before busting out some nauseatingly simple (yet verbose) mathematics. Mr Fooz raises a good point. I\'m dealing with \"listings\" (a bit like ebay listings) where each one has a duration. I\'m trying to find the time