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.










share|improve this question



























    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.










    share|improve this question

























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 19 '12 at 18:45

























      asked Dec 19 '12 at 18:35









      bitcycle

      3,3831252106




      3,3831252106






















          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.






          share|improve this answer



























            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).






            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              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

























              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.






              share|improve this answer
























                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.






                share|improve this answer






















                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 '12 at 18:48









                  Keith

                  30.1k64260




                  30.1k64260






















                      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).






                      share|improve this answer
























                        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).






                        share|improve this answer






















                          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).






                          share|improve this answer












                          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).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 9 at 18:56









                          anarcat

                          2,0911828




                          2,0911828



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Syphilis

                              Darth Vader #20