Human-readable datetime interval to datetime.timedelta in Python?
up vote
0
down vote
favorite
I find myself needing to specify a timespan in a python configuration file a lot.
Is there a way that I can specify a more human readable timeframe (similar to PostgreSQL's Interval syntax) in a python configuration file with stdlib? Or will this require a 3rd party lib?
Clarification I'm not looking for anything in the ConfigParser.ConfigParser
stdlib API specifically. I guess what I really need is a way to go from human readable date/time interval to datetime.timedelta
value.
python datetime intervals
add a comment |
up vote
0
down vote
favorite
I find myself needing to specify a timespan in a python configuration file a lot.
Is there a way that I can specify a more human readable timeframe (similar to PostgreSQL's Interval syntax) in a python configuration file with stdlib? Or will this require a 3rd party lib?
Clarification I'm not looking for anything in the ConfigParser.ConfigParser
stdlib API specifically. I guess what I really need is a way to go from human readable date/time interval to datetime.timedelta
value.
python datetime intervals
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I find myself needing to specify a timespan in a python configuration file a lot.
Is there a way that I can specify a more human readable timeframe (similar to PostgreSQL's Interval syntax) in a python configuration file with stdlib? Or will this require a 3rd party lib?
Clarification I'm not looking for anything in the ConfigParser.ConfigParser
stdlib API specifically. I guess what I really need is a way to go from human readable date/time interval to datetime.timedelta
value.
python datetime intervals
I find myself needing to specify a timespan in a python configuration file a lot.
Is there a way that I can specify a more human readable timeframe (similar to PostgreSQL's Interval syntax) in a python configuration file with stdlib? Or will this require a 3rd party lib?
Clarification I'm not looking for anything in the ConfigParser.ConfigParser
stdlib API specifically. I guess what I really need is a way to go from human readable date/time interval to datetime.timedelta
value.
python datetime intervals
python datetime intervals
edited Dec 19 '12 at 18:45
asked Dec 19 '12 at 18:35
bitcycle
3,3831252106
3,3831252106
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I don't think there is a standard library module for that. I wrote one that does that. You can install it, or adapt it to your needs.
The module is called pycopia.timespec
It converts strings such as "1day 3min" to seconds, as a float. It's easy to get a datetime.timedelta
from that.
add a comment |
up vote
0
down vote
I found a good answer to this in an somewhat related question. Turns out the humanfriendly
library does that fairly well:
In [1]: import humanfriendly
In [2]: humanfriendly.parse_timespan('1w')
Out[2]: 604800.0
That's in seconds. To get a timedelta
object, you can simply load that:
In [3]: from datetime import timedelta
In [4]: timedelta(seconds=humanfriendly.parse_timespan('1w'))
Out[4]: datetime.timedelta(7)
Since humanfriendly also supports converting the other way, you can also do full round trip, which would look like:
In [5]: humanfriendly.format_timespan(timedelta(seconds=humanfriendly.parse_timespan('1w')).total_seconds())
Out[5]: '1 week'
Note how format_timespan
does not access timedelta
objects, unfortunately: only an integer (seconds).
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I don't think there is a standard library module for that. I wrote one that does that. You can install it, or adapt it to your needs.
The module is called pycopia.timespec
It converts strings such as "1day 3min" to seconds, as a float. It's easy to get a datetime.timedelta
from that.
add a comment |
up vote
1
down vote
accepted
I don't think there is a standard library module for that. I wrote one that does that. You can install it, or adapt it to your needs.
The module is called pycopia.timespec
It converts strings such as "1day 3min" to seconds, as a float. It's easy to get a datetime.timedelta
from that.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I don't think there is a standard library module for that. I wrote one that does that. You can install it, or adapt it to your needs.
The module is called pycopia.timespec
It converts strings such as "1day 3min" to seconds, as a float. It's easy to get a datetime.timedelta
from that.
I don't think there is a standard library module for that. I wrote one that does that. You can install it, or adapt it to your needs.
The module is called pycopia.timespec
It converts strings such as "1day 3min" to seconds, as a float. It's easy to get a datetime.timedelta
from that.
answered Dec 19 '12 at 18:48
Keith
30.1k64260
30.1k64260
add a comment |
add a comment |
up vote
0
down vote
I found a good answer to this in an somewhat related question. Turns out the humanfriendly
library does that fairly well:
In [1]: import humanfriendly
In [2]: humanfriendly.parse_timespan('1w')
Out[2]: 604800.0
That's in seconds. To get a timedelta
object, you can simply load that:
In [3]: from datetime import timedelta
In [4]: timedelta(seconds=humanfriendly.parse_timespan('1w'))
Out[4]: datetime.timedelta(7)
Since humanfriendly also supports converting the other way, you can also do full round trip, which would look like:
In [5]: humanfriendly.format_timespan(timedelta(seconds=humanfriendly.parse_timespan('1w')).total_seconds())
Out[5]: '1 week'
Note how format_timespan
does not access timedelta
objects, unfortunately: only an integer (seconds).
add a comment |
up vote
0
down vote
I found a good answer to this in an somewhat related question. Turns out the humanfriendly
library does that fairly well:
In [1]: import humanfriendly
In [2]: humanfriendly.parse_timespan('1w')
Out[2]: 604800.0
That's in seconds. To get a timedelta
object, you can simply load that:
In [3]: from datetime import timedelta
In [4]: timedelta(seconds=humanfriendly.parse_timespan('1w'))
Out[4]: datetime.timedelta(7)
Since humanfriendly also supports converting the other way, you can also do full round trip, which would look like:
In [5]: humanfriendly.format_timespan(timedelta(seconds=humanfriendly.parse_timespan('1w')).total_seconds())
Out[5]: '1 week'
Note how format_timespan
does not access timedelta
objects, unfortunately: only an integer (seconds).
add a comment |
up vote
0
down vote
up vote
0
down vote
I found a good answer to this in an somewhat related question. Turns out the humanfriendly
library does that fairly well:
In [1]: import humanfriendly
In [2]: humanfriendly.parse_timespan('1w')
Out[2]: 604800.0
That's in seconds. To get a timedelta
object, you can simply load that:
In [3]: from datetime import timedelta
In [4]: timedelta(seconds=humanfriendly.parse_timespan('1w'))
Out[4]: datetime.timedelta(7)
Since humanfriendly also supports converting the other way, you can also do full round trip, which would look like:
In [5]: humanfriendly.format_timespan(timedelta(seconds=humanfriendly.parse_timespan('1w')).total_seconds())
Out[5]: '1 week'
Note how format_timespan
does not access timedelta
objects, unfortunately: only an integer (seconds).
I found a good answer to this in an somewhat related question. Turns out the humanfriendly
library does that fairly well:
In [1]: import humanfriendly
In [2]: humanfriendly.parse_timespan('1w')
Out[2]: 604800.0
That's in seconds. To get a timedelta
object, you can simply load that:
In [3]: from datetime import timedelta
In [4]: timedelta(seconds=humanfriendly.parse_timespan('1w'))
Out[4]: datetime.timedelta(7)
Since humanfriendly also supports converting the other way, you can also do full round trip, which would look like:
In [5]: humanfriendly.format_timespan(timedelta(seconds=humanfriendly.parse_timespan('1w')).total_seconds())
Out[5]: '1 week'
Note how format_timespan
does not access timedelta
objects, unfortunately: only an integer (seconds).
answered Nov 9 at 18:56
anarcat
2,0911828
2,0911828
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13958844%2fhuman-readable-datetime-interval-to-datetime-timedelta-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown