Convert CST to UTC












-3















How can I convert the CST to UTC by this code?



str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")) + "Z" 


This is the predefined and my UI is in CST and API want to be in UTC. So, how can I convert CST to UTC using same code?










share|improve this question





























    -3















    How can I convert the CST to UTC by this code?



    str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")) + "Z" 


    This is the predefined and my UI is in CST and API want to be in UTC. So, how can I convert CST to UTC using same code?










    share|improve this question



























      -3












      -3








      -3








      How can I convert the CST to UTC by this code?



      str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")) + "Z" 


      This is the predefined and my UI is in CST and API want to be in UTC. So, how can I convert CST to UTC using same code?










      share|improve this question
















      How can I convert the CST to UTC by this code?



      str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")) + "Z" 


      This is the predefined and my UI is in CST and API want to be in UTC. So, how can I convert CST to UTC using same code?







      python datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 18:30









      Brad Solomon

      13.4k73482




      13.4k73482










      asked Nov 13 '18 at 17:37









      Srikanth PonnapatiSrikanth Ponnapati

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          One comment upfront: if you are actually using datetime.datetime.utcnow(), you don't need to do any conversion. That is the UTC time, though it is technically a naive datetime because it does not have a .tzinfo attribute attached. (And you also don't need to call str.)





          Otherwise, assuming that your input time is actually local-based (does not use .utcnow()), you can use time.timezone to get a timedelta. Here is an example from my system, which is EST:



          >>> import datetime
          >>> import time

          >>> localtime = datetime.datetime.now()
          >>> localtime
          datetime.datetime(2018, 11, 13, 13, 22, 54, 109529)
          >>> datetime.datetime.utcnow()
          datetime.datetime(2018, 11, 13, 18, 23, 3, 373213)

          >>> convrt = localtime + datetime.timedelta(
          ... seconds=time.timezone)
          >>>
          >>> convrt
          datetime.datetime(2018, 11, 13, 18, 22, 54, 109529)
          >>> convrt.replace(tzinfo=datetime.timezone.utc).isoformat()
          '2018-11-13T18:22:54.109529+00:00'


          Notice that .isoformat() uses the numeric offset. To get the name, use %Z:



          >>> convrt.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%Z")
          '2018-11-13T18:22:54UTC'


          time.timezone is an offset of the local (non-DST) timezone, in seconds west of UTC. There's also time.altzone, which is offset of the local DST timezone (seconds west).






          share|improve this answer
























          • That's a good solution but I can't write my scripts in the python file. I've to use existed code. That is like I posted in the Question. I've to change only the code by adding the something like this: str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[-3]) + "Z"

            – Srikanth Ponnapati
            Nov 13 '18 at 20:49













          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%2f53286650%2fconvert-cst-to-utc%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          One comment upfront: if you are actually using datetime.datetime.utcnow(), you don't need to do any conversion. That is the UTC time, though it is technically a naive datetime because it does not have a .tzinfo attribute attached. (And you also don't need to call str.)





          Otherwise, assuming that your input time is actually local-based (does not use .utcnow()), you can use time.timezone to get a timedelta. Here is an example from my system, which is EST:



          >>> import datetime
          >>> import time

          >>> localtime = datetime.datetime.now()
          >>> localtime
          datetime.datetime(2018, 11, 13, 13, 22, 54, 109529)
          >>> datetime.datetime.utcnow()
          datetime.datetime(2018, 11, 13, 18, 23, 3, 373213)

          >>> convrt = localtime + datetime.timedelta(
          ... seconds=time.timezone)
          >>>
          >>> convrt
          datetime.datetime(2018, 11, 13, 18, 22, 54, 109529)
          >>> convrt.replace(tzinfo=datetime.timezone.utc).isoformat()
          '2018-11-13T18:22:54.109529+00:00'


          Notice that .isoformat() uses the numeric offset. To get the name, use %Z:



          >>> convrt.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%Z")
          '2018-11-13T18:22:54UTC'


          time.timezone is an offset of the local (non-DST) timezone, in seconds west of UTC. There's also time.altzone, which is offset of the local DST timezone (seconds west).






          share|improve this answer
























          • That's a good solution but I can't write my scripts in the python file. I've to use existed code. That is like I posted in the Question. I've to change only the code by adding the something like this: str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[-3]) + "Z"

            – Srikanth Ponnapati
            Nov 13 '18 at 20:49


















          0














          One comment upfront: if you are actually using datetime.datetime.utcnow(), you don't need to do any conversion. That is the UTC time, though it is technically a naive datetime because it does not have a .tzinfo attribute attached. (And you also don't need to call str.)





          Otherwise, assuming that your input time is actually local-based (does not use .utcnow()), you can use time.timezone to get a timedelta. Here is an example from my system, which is EST:



          >>> import datetime
          >>> import time

          >>> localtime = datetime.datetime.now()
          >>> localtime
          datetime.datetime(2018, 11, 13, 13, 22, 54, 109529)
          >>> datetime.datetime.utcnow()
          datetime.datetime(2018, 11, 13, 18, 23, 3, 373213)

          >>> convrt = localtime + datetime.timedelta(
          ... seconds=time.timezone)
          >>>
          >>> convrt
          datetime.datetime(2018, 11, 13, 18, 22, 54, 109529)
          >>> convrt.replace(tzinfo=datetime.timezone.utc).isoformat()
          '2018-11-13T18:22:54.109529+00:00'


          Notice that .isoformat() uses the numeric offset. To get the name, use %Z:



          >>> convrt.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%Z")
          '2018-11-13T18:22:54UTC'


          time.timezone is an offset of the local (non-DST) timezone, in seconds west of UTC. There's also time.altzone, which is offset of the local DST timezone (seconds west).






          share|improve this answer
























          • That's a good solution but I can't write my scripts in the python file. I've to use existed code. That is like I posted in the Question. I've to change only the code by adding the something like this: str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[-3]) + "Z"

            – Srikanth Ponnapati
            Nov 13 '18 at 20:49
















          0












          0








          0







          One comment upfront: if you are actually using datetime.datetime.utcnow(), you don't need to do any conversion. That is the UTC time, though it is technically a naive datetime because it does not have a .tzinfo attribute attached. (And you also don't need to call str.)





          Otherwise, assuming that your input time is actually local-based (does not use .utcnow()), you can use time.timezone to get a timedelta. Here is an example from my system, which is EST:



          >>> import datetime
          >>> import time

          >>> localtime = datetime.datetime.now()
          >>> localtime
          datetime.datetime(2018, 11, 13, 13, 22, 54, 109529)
          >>> datetime.datetime.utcnow()
          datetime.datetime(2018, 11, 13, 18, 23, 3, 373213)

          >>> convrt = localtime + datetime.timedelta(
          ... seconds=time.timezone)
          >>>
          >>> convrt
          datetime.datetime(2018, 11, 13, 18, 22, 54, 109529)
          >>> convrt.replace(tzinfo=datetime.timezone.utc).isoformat()
          '2018-11-13T18:22:54.109529+00:00'


          Notice that .isoformat() uses the numeric offset. To get the name, use %Z:



          >>> convrt.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%Z")
          '2018-11-13T18:22:54UTC'


          time.timezone is an offset of the local (non-DST) timezone, in seconds west of UTC. There's also time.altzone, which is offset of the local DST timezone (seconds west).






          share|improve this answer













          One comment upfront: if you are actually using datetime.datetime.utcnow(), you don't need to do any conversion. That is the UTC time, though it is technically a naive datetime because it does not have a .tzinfo attribute attached. (And you also don't need to call str.)





          Otherwise, assuming that your input time is actually local-based (does not use .utcnow()), you can use time.timezone to get a timedelta. Here is an example from my system, which is EST:



          >>> import datetime
          >>> import time

          >>> localtime = datetime.datetime.now()
          >>> localtime
          datetime.datetime(2018, 11, 13, 13, 22, 54, 109529)
          >>> datetime.datetime.utcnow()
          datetime.datetime(2018, 11, 13, 18, 23, 3, 373213)

          >>> convrt = localtime + datetime.timedelta(
          ... seconds=time.timezone)
          >>>
          >>> convrt
          datetime.datetime(2018, 11, 13, 18, 22, 54, 109529)
          >>> convrt.replace(tzinfo=datetime.timezone.utc).isoformat()
          '2018-11-13T18:22:54.109529+00:00'


          Notice that .isoformat() uses the numeric offset. To get the name, use %Z:



          >>> convrt.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%Z")
          '2018-11-13T18:22:54UTC'


          time.timezone is an offset of the local (non-DST) timezone, in seconds west of UTC. There's also time.altzone, which is offset of the local DST timezone (seconds west).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 18:27









          Brad SolomonBrad Solomon

          13.4k73482




          13.4k73482













          • That's a good solution but I can't write my scripts in the python file. I've to use existed code. That is like I posted in the Question. I've to change only the code by adding the something like this: str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[-3]) + "Z"

            – Srikanth Ponnapati
            Nov 13 '18 at 20:49





















          • That's a good solution but I can't write my scripts in the python file. I've to use existed code. That is like I posted in the Question. I've to change only the code by adding the something like this: str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[-3]) + "Z"

            – Srikanth Ponnapati
            Nov 13 '18 at 20:49



















          That's a good solution but I can't write my scripts in the python file. I've to use existed code. That is like I posted in the Question. I've to change only the code by adding the something like this: str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[-3]) + "Z"

          – Srikanth Ponnapati
          Nov 13 '18 at 20:49







          That's a good solution but I can't write my scripts in the python file. I've to use existed code. That is like I posted in the Question. I've to change only the code by adding the something like this: str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[-3]) + "Z"

          – Srikanth Ponnapati
          Nov 13 '18 at 20:49




















          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%2f53286650%2fconvert-cst-to-utc%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python