Python daylight savings time












36














How do I check if daylight saving time is in effect?










share|improve this question





























    36














    How do I check if daylight saving time is in effect?










    share|improve this question



























      36












      36








      36


      5





      How do I check if daylight saving time is in effect?










      share|improve this question















      How do I check if daylight saving time is in effect?







      python time dst






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 27 '10 at 20:50









      Lance Roberts

      17.3k2798126




      17.3k2798126










      asked May 21 '10 at 9:45









      Pawel FurmaniakPawel Furmaniak

      2,70032232




      2,70032232
























          2 Answers
          2






          active

          oldest

          votes


















          56














          You can use time.localtime and look at the tm_isdst flag in the return value.



          >>> import time
          >>> time.localtime()
          (2010, 5, 21, 21, 48, 51, 4, 141, 0)
          >>> _.tm_isdst
          0


          Using time.localtime(), you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.






          share|improve this answer





















          • It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
            – brian buck
            May 21 '10 at 15:33






          • 8




            @brian buck: That's different though. For a given time zone, time.daylight is constant because a daylight zone either exists or it doesn't. On the other hand, the tm_isdst flag reflects whether the given time is within the DST start and end dates.
            – Greg Hewgill
            May 21 '10 at 19:28










          • This confusion over time.daylight appears to be a bigger issue: bugs.python.org/issue7229.
            – Zachary Young
            Dec 6 '11 at 22:19












          • it should be _.tm_isdst > 0 (-1 value is possible).
            – jfs
            Feb 22 '14 at 8:05






          • 6




            Ahhh! I hate these sort of answers for Python questions as it is a command line example that does not illuminate (to me) how to put this into a function. (My naive effort to try time.localtime().tm_isdst does not work.) So I will plumb further and augment the answer.
            – Jiminion
            Apr 8 '16 at 15:36



















          0














          Expanding @Greg Hewgill's answer above, plus coping with local timezone (with help of pip install tzlocal), you get:



          import time
          from datetime import datetime, timedelta
          from tzlocal import get_localzone

          def to_local(dt):
          """From any timezone to local datetime - also cope with DST"""
          localtime = time.localtime()
          if localtime.tm_isdst:
          utctime = time.gmtime()
          hours_delta = timedelta(hours=(localtime.tm_hour - utctime.tm_hour))
          dt = dt - hours_delta

          return dt.replace(tzinfo=get_localzone())





          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',
            autoActivateHeartbeat: false,
            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%2f2881025%2fpython-daylight-savings-time%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









            56














            You can use time.localtime and look at the tm_isdst flag in the return value.



            >>> import time
            >>> time.localtime()
            (2010, 5, 21, 21, 48, 51, 4, 141, 0)
            >>> _.tm_isdst
            0


            Using time.localtime(), you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.






            share|improve this answer





















            • It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
              – brian buck
              May 21 '10 at 15:33






            • 8




              @brian buck: That's different though. For a given time zone, time.daylight is constant because a daylight zone either exists or it doesn't. On the other hand, the tm_isdst flag reflects whether the given time is within the DST start and end dates.
              – Greg Hewgill
              May 21 '10 at 19:28










            • This confusion over time.daylight appears to be a bigger issue: bugs.python.org/issue7229.
              – Zachary Young
              Dec 6 '11 at 22:19












            • it should be _.tm_isdst > 0 (-1 value is possible).
              – jfs
              Feb 22 '14 at 8:05






            • 6




              Ahhh! I hate these sort of answers for Python questions as it is a command line example that does not illuminate (to me) how to put this into a function. (My naive effort to try time.localtime().tm_isdst does not work.) So I will plumb further and augment the answer.
              – Jiminion
              Apr 8 '16 at 15:36
















            56














            You can use time.localtime and look at the tm_isdst flag in the return value.



            >>> import time
            >>> time.localtime()
            (2010, 5, 21, 21, 48, 51, 4, 141, 0)
            >>> _.tm_isdst
            0


            Using time.localtime(), you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.






            share|improve this answer





















            • It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
              – brian buck
              May 21 '10 at 15:33






            • 8




              @brian buck: That's different though. For a given time zone, time.daylight is constant because a daylight zone either exists or it doesn't. On the other hand, the tm_isdst flag reflects whether the given time is within the DST start and end dates.
              – Greg Hewgill
              May 21 '10 at 19:28










            • This confusion over time.daylight appears to be a bigger issue: bugs.python.org/issue7229.
              – Zachary Young
              Dec 6 '11 at 22:19












            • it should be _.tm_isdst > 0 (-1 value is possible).
              – jfs
              Feb 22 '14 at 8:05






            • 6




              Ahhh! I hate these sort of answers for Python questions as it is a command line example that does not illuminate (to me) how to put this into a function. (My naive effort to try time.localtime().tm_isdst does not work.) So I will plumb further and augment the answer.
              – Jiminion
              Apr 8 '16 at 15:36














            56












            56








            56






            You can use time.localtime and look at the tm_isdst flag in the return value.



            >>> import time
            >>> time.localtime()
            (2010, 5, 21, 21, 48, 51, 4, 141, 0)
            >>> _.tm_isdst
            0


            Using time.localtime(), you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.






            share|improve this answer












            You can use time.localtime and look at the tm_isdst flag in the return value.



            >>> import time
            >>> time.localtime()
            (2010, 5, 21, 21, 48, 51, 4, 141, 0)
            >>> _.tm_isdst
            0


            Using time.localtime(), you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 21 '10 at 9:48









            Greg HewgillGreg Hewgill

            665k14310091164




            665k14310091164












            • It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
              – brian buck
              May 21 '10 at 15:33






            • 8




              @brian buck: That's different though. For a given time zone, time.daylight is constant because a daylight zone either exists or it doesn't. On the other hand, the tm_isdst flag reflects whether the given time is within the DST start and end dates.
              – Greg Hewgill
              May 21 '10 at 19:28










            • This confusion over time.daylight appears to be a bigger issue: bugs.python.org/issue7229.
              – Zachary Young
              Dec 6 '11 at 22:19












            • it should be _.tm_isdst > 0 (-1 value is possible).
              – jfs
              Feb 22 '14 at 8:05






            • 6




              Ahhh! I hate these sort of answers for Python questions as it is a command line example that does not illuminate (to me) how to put this into a function. (My naive effort to try time.localtime().tm_isdst does not work.) So I will plumb further and augment the answer.
              – Jiminion
              Apr 8 '16 at 15:36


















            • It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
              – brian buck
              May 21 '10 at 15:33






            • 8




              @brian buck: That's different though. For a given time zone, time.daylight is constant because a daylight zone either exists or it doesn't. On the other hand, the tm_isdst flag reflects whether the given time is within the DST start and end dates.
              – Greg Hewgill
              May 21 '10 at 19:28










            • This confusion over time.daylight appears to be a bigger issue: bugs.python.org/issue7229.
              – Zachary Young
              Dec 6 '11 at 22:19












            • it should be _.tm_isdst > 0 (-1 value is possible).
              – jfs
              Feb 22 '14 at 8:05






            • 6




              Ahhh! I hate these sort of answers for Python questions as it is a command line example that does not illuminate (to me) how to put this into a function. (My naive effort to try time.localtime().tm_isdst does not work.) So I will plumb further and augment the answer.
              – Jiminion
              Apr 8 '16 at 15:36
















            It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
            – brian buck
            May 21 '10 at 15:33




            It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
            – brian buck
            May 21 '10 at 15:33




            8




            8




            @brian buck: That's different though. For a given time zone, time.daylight is constant because a daylight zone either exists or it doesn't. On the other hand, the tm_isdst flag reflects whether the given time is within the DST start and end dates.
            – Greg Hewgill
            May 21 '10 at 19:28




            @brian buck: That's different though. For a given time zone, time.daylight is constant because a daylight zone either exists or it doesn't. On the other hand, the tm_isdst flag reflects whether the given time is within the DST start and end dates.
            – Greg Hewgill
            May 21 '10 at 19:28












            This confusion over time.daylight appears to be a bigger issue: bugs.python.org/issue7229.
            – Zachary Young
            Dec 6 '11 at 22:19






            This confusion over time.daylight appears to be a bigger issue: bugs.python.org/issue7229.
            – Zachary Young
            Dec 6 '11 at 22:19














            it should be _.tm_isdst > 0 (-1 value is possible).
            – jfs
            Feb 22 '14 at 8:05




            it should be _.tm_isdst > 0 (-1 value is possible).
            – jfs
            Feb 22 '14 at 8:05




            6




            6




            Ahhh! I hate these sort of answers for Python questions as it is a command line example that does not illuminate (to me) how to put this into a function. (My naive effort to try time.localtime().tm_isdst does not work.) So I will plumb further and augment the answer.
            – Jiminion
            Apr 8 '16 at 15:36




            Ahhh! I hate these sort of answers for Python questions as it is a command line example that does not illuminate (to me) how to put this into a function. (My naive effort to try time.localtime().tm_isdst does not work.) So I will plumb further and augment the answer.
            – Jiminion
            Apr 8 '16 at 15:36













            0














            Expanding @Greg Hewgill's answer above, plus coping with local timezone (with help of pip install tzlocal), you get:



            import time
            from datetime import datetime, timedelta
            from tzlocal import get_localzone

            def to_local(dt):
            """From any timezone to local datetime - also cope with DST"""
            localtime = time.localtime()
            if localtime.tm_isdst:
            utctime = time.gmtime()
            hours_delta = timedelta(hours=(localtime.tm_hour - utctime.tm_hour))
            dt = dt - hours_delta

            return dt.replace(tzinfo=get_localzone())





            share|improve this answer


























              0














              Expanding @Greg Hewgill's answer above, plus coping with local timezone (with help of pip install tzlocal), you get:



              import time
              from datetime import datetime, timedelta
              from tzlocal import get_localzone

              def to_local(dt):
              """From any timezone to local datetime - also cope with DST"""
              localtime = time.localtime()
              if localtime.tm_isdst:
              utctime = time.gmtime()
              hours_delta = timedelta(hours=(localtime.tm_hour - utctime.tm_hour))
              dt = dt - hours_delta

              return dt.replace(tzinfo=get_localzone())





              share|improve this answer
























                0












                0








                0






                Expanding @Greg Hewgill's answer above, plus coping with local timezone (with help of pip install tzlocal), you get:



                import time
                from datetime import datetime, timedelta
                from tzlocal import get_localzone

                def to_local(dt):
                """From any timezone to local datetime - also cope with DST"""
                localtime = time.localtime()
                if localtime.tm_isdst:
                utctime = time.gmtime()
                hours_delta = timedelta(hours=(localtime.tm_hour - utctime.tm_hour))
                dt = dt - hours_delta

                return dt.replace(tzinfo=get_localzone())





                share|improve this answer












                Expanding @Greg Hewgill's answer above, plus coping with local timezone (with help of pip install tzlocal), you get:



                import time
                from datetime import datetime, timedelta
                from tzlocal import get_localzone

                def to_local(dt):
                """From any timezone to local datetime - also cope with DST"""
                localtime = time.localtime()
                if localtime.tm_isdst:
                utctime = time.gmtime()
                hours_delta = timedelta(hours=(localtime.tm_hour - utctime.tm_hour))
                dt = dt - hours_delta

                return dt.replace(tzinfo=get_localzone())






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 11 '18 at 15:20









                Jose AlbanJose Alban

                3,5592316




                3,5592316






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2881025%2fpython-daylight-savings-time%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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...