473,671 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

str as param for timedelta

Hello Pythonistas

I'm new to Python, I hope you understand my question.
I would like to pass a parameter "gap" to a function. gap should be
used there to create a timedelta Object.

from datetime import *

def f(gap):
print (datetime.now() + datetime.timede lta(gap))

f('hours = -8')

I read in the manual that datetime does accept ints, longs, or floats,
which may be positive or negative. Same as the error message:
"unsupporte d type for timedelta days component: str"

I found out, that I can pass an Object

def f1(deltaObj):
print (datetime.now() + deltaObj)

f1(timedelta(ho urs = -8))

I would prefer the first version, as it is easier to use the string for
representation and so on.

Is there any way to pass a string to the function and use it there for
the timedelta?

or you might say I missed some basic concepts...

.... in any case, thank you very much!

Phil

Jan 27 '06 #1
1 6861
Philipp wrote:
from datetime import *

def f(gap):
print (datetime.now() + datetime.timede lta(gap))

f('hours = -8')


When you need a flexible input format, consider keyword arguments. In
this case, I would pass along the inputs to timedelta:

def from_now(**kwar gs):
return datetime.now() + timedelta(**kwa rgs)

from_now(hours= 8) returns datetime.now() + timedelta(hours =8), and the
function can now accept any of timedelta's arguments. For more on what
**kwargs means, see the tutorial:

http://docs.python.org/tut/node6.htm...00000000000000

Jan 27 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2014
by: Wil Schultz | last post by:
Hey all, Sorry, I am trying my best to learn python and seem to be able to grab somethings while others seem to escape me... I am trying to write a timedelta meter, i.e. "it has been U years, V months, W day's, X hours, Y minutes, Z seconds since SomePoint" Here's what I have so far, I can print the current time/date but am having issues figuring out how to timedelat it...
0
1209
by: josh | last post by:
Why can't timedelta arithmetic be done on time objects? (e.g. datetime.time(5)-datetime.timedelta(microseconds=3) Nonzero "days" of the timedelta could either be ignored, or trigger an exception.
0
1313
by: josh | last post by:
datetime.time should support timedelta arithmetic, patch attached. Times greater than 24 hours should not raise an exception, but always wrap around. Any other behavior is too surprising. (People expect to be able to call you up at 11pm, and say "meet me in two hours" (if you're a night person)). Clock time is cyclical. Though it's convenient to assign hours numbers, an unqualified hour 23.5 is not naturally "greater than" hour 0.5....
1
3977
by: Florian Lindner | last post by:
Hello, I want to know if a certain duration is over. I try it like this with timedelta objects: d = datetime.timedelta(minutes = 2) t = time.gmtime() print (t + d < time.gmtime()) gives:
5
28441
by: Lad | last post by:
Hello, what is the best /easest way how to get number of hours and minutes from a timedelta object? Let's say we have aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000) bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000) so c=bb-aa will be datetime.timedelta(5, 6339, 437000)
6
30598
by: Carl J. Van Arsdall | last post by:
Basically I used the datetime module and timedelta objects to calculate a difference between two times. Now I'm trying to figure out how I make that time delta a string HH:MM:SS to show elapsed time. I've spent tons of time looking at the module's documentation but I'm not seeing how those methods will help me. Can anyone help me with this? Here's what I'm trying to do, assum that resultsDict is a dictionary of objects that have...
2
13988
by: kath | last post by:
Hi, the following shows the contents of "datebook.xls" Date 8/9/2006 8/9/2006 8/9/2006 8/9/2006 8/9/2006
2
4302
by: skip | last post by:
Running from Subversion, I see confusing (to me) behavior related to division of datetime.timedelta objects by integers: % python Python 2.6a0 (trunk:57277:57280M, Aug 28 2007, 17:44:49) on darwin Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'int'
1
1886
by: admoore | last post by:
Maybe I'm missing something obvious, but is there a QT object to represent time intervals, a la datetime.timedelta? I'm working on a utility that displays database query results from a postgres database (using psycopg2) in a QTableView. For columns created using "age(some_date_column)", I get a datetime.timedelta object from psycopg2. What I'd like is to be able to format this column in a friendly way (say "7 years, 2 months, 4...
0
8478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8397
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2813
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.