Python 3.6 DateTime Strptime Returns error while Python 3.7 works well










0















I just created a data type for my date data, which returns a datetime.datetime object



Here is the code:



import datetime


class Date:
def __new__(cls, dateTime, *args, **kwargs):
return datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%f%z")


So everytime I give this class an ISO-8601 it should return the datetime object from the string...



Python 3.7 Example:



Date("2018-12-09T08:56:12.189Z") 
# Returns => datetime.datetime(2018, 12, 9, 8, 56, 12, 189000, tzinfo=datetime.timezone.utc)


This works damn well, but when I use it on Python 3.6 or Python 3.5:



# Python 3.5 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

# Python 3.6 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'


It's so weird, What causes the problem? How can I fix it?










share|improve this question



















  • 2





    The one you say is Python 3.6 is using modules from Python 3.5. That's a problem.

    – user2357112
    Nov 14 '18 at 0:07











  • @user2357112 I didn't have Python 3.6 installed on my desktop, but it was installed on the server, So I used my python3.5, but I also checked it on the server with python3.6 and it had the same problem

    – DarkSuniuM
    Nov 14 '18 at 0:10











  • @user2357112 Added Python 3.6 Traceback as well

    – DarkSuniuM
    Nov 14 '18 at 0:18















0















I just created a data type for my date data, which returns a datetime.datetime object



Here is the code:



import datetime


class Date:
def __new__(cls, dateTime, *args, **kwargs):
return datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%f%z")


So everytime I give this class an ISO-8601 it should return the datetime object from the string...



Python 3.7 Example:



Date("2018-12-09T08:56:12.189Z") 
# Returns => datetime.datetime(2018, 12, 9, 8, 56, 12, 189000, tzinfo=datetime.timezone.utc)


This works damn well, but when I use it on Python 3.6 or Python 3.5:



# Python 3.5 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

# Python 3.6 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'


It's so weird, What causes the problem? How can I fix it?










share|improve this question



















  • 2





    The one you say is Python 3.6 is using modules from Python 3.5. That's a problem.

    – user2357112
    Nov 14 '18 at 0:07











  • @user2357112 I didn't have Python 3.6 installed on my desktop, but it was installed on the server, So I used my python3.5, but I also checked it on the server with python3.6 and it had the same problem

    – DarkSuniuM
    Nov 14 '18 at 0:10











  • @user2357112 Added Python 3.6 Traceback as well

    – DarkSuniuM
    Nov 14 '18 at 0:18













0












0








0








I just created a data type for my date data, which returns a datetime.datetime object



Here is the code:



import datetime


class Date:
def __new__(cls, dateTime, *args, **kwargs):
return datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%f%z")


So everytime I give this class an ISO-8601 it should return the datetime object from the string...



Python 3.7 Example:



Date("2018-12-09T08:56:12.189Z") 
# Returns => datetime.datetime(2018, 12, 9, 8, 56, 12, 189000, tzinfo=datetime.timezone.utc)


This works damn well, but when I use it on Python 3.6 or Python 3.5:



# Python 3.5 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

# Python 3.6 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'


It's so weird, What causes the problem? How can I fix it?










share|improve this question
















I just created a data type for my date data, which returns a datetime.datetime object



Here is the code:



import datetime


class Date:
def __new__(cls, dateTime, *args, **kwargs):
return datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%f%z")


So everytime I give this class an ISO-8601 it should return the datetime object from the string...



Python 3.7 Example:



Date("2018-12-09T08:56:12.189Z") 
# Returns => datetime.datetime(2018, 12, 9, 8, 56, 12, 189000, tzinfo=datetime.timezone.utc)


This works damn well, but when I use it on Python 3.6 or Python 3.5:



# Python 3.5 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

# Python 3.6 Traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2018-12-09T08:56:12.189Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'


It's so weird, What causes the problem? How can I fix it?







python datetime python-3.6 python-3.7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 0:18







DarkSuniuM

















asked Nov 14 '18 at 0:02









DarkSuniuMDarkSuniuM

8021119




8021119







  • 2





    The one you say is Python 3.6 is using modules from Python 3.5. That's a problem.

    – user2357112
    Nov 14 '18 at 0:07











  • @user2357112 I didn't have Python 3.6 installed on my desktop, but it was installed on the server, So I used my python3.5, but I also checked it on the server with python3.6 and it had the same problem

    – DarkSuniuM
    Nov 14 '18 at 0:10











  • @user2357112 Added Python 3.6 Traceback as well

    – DarkSuniuM
    Nov 14 '18 at 0:18












  • 2





    The one you say is Python 3.6 is using modules from Python 3.5. That's a problem.

    – user2357112
    Nov 14 '18 at 0:07











  • @user2357112 I didn't have Python 3.6 installed on my desktop, but it was installed on the server, So I used my python3.5, but I also checked it on the server with python3.6 and it had the same problem

    – DarkSuniuM
    Nov 14 '18 at 0:10











  • @user2357112 Added Python 3.6 Traceback as well

    – DarkSuniuM
    Nov 14 '18 at 0:18







2




2





The one you say is Python 3.6 is using modules from Python 3.5. That's a problem.

– user2357112
Nov 14 '18 at 0:07





The one you say is Python 3.6 is using modules from Python 3.5. That's a problem.

– user2357112
Nov 14 '18 at 0:07













@user2357112 I didn't have Python 3.6 installed on my desktop, but it was installed on the server, So I used my python3.5, but I also checked it on the server with python3.6 and it had the same problem

– DarkSuniuM
Nov 14 '18 at 0:10





@user2357112 I didn't have Python 3.6 installed on my desktop, but it was installed on the server, So I used my python3.5, but I also checked it on the server with python3.6 and it had the same problem

– DarkSuniuM
Nov 14 '18 at 0:10













@user2357112 Added Python 3.6 Traceback as well

– DarkSuniuM
Nov 14 '18 at 0:18





@user2357112 Added Python 3.6 Traceback as well

– DarkSuniuM
Nov 14 '18 at 0:18












1 Answer
1






active

oldest

votes


















0














Ok, after 2 days, I checked the Python 3.7 changelog, and I found out support for Z as a UTC offset was added in Python 3.7. See this issue on the Python issue tracker, which is primarily about adding support for colons, but also mentions Z support further down the page. Also see the datetime docs, which say




Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.




On my class, I had to change the time format to this:



datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%fZ")


I changed the %z at the end to Z, hardcoding the offset.






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%2f53291250%2fpython-3-6-datetime-strptime-returns-error-while-python-3-7-works-well%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














    Ok, after 2 days, I checked the Python 3.7 changelog, and I found out support for Z as a UTC offset was added in Python 3.7. See this issue on the Python issue tracker, which is primarily about adding support for colons, but also mentions Z support further down the page. Also see the datetime docs, which say




    Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.




    On my class, I had to change the time format to this:



    datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%fZ")


    I changed the %z at the end to Z, hardcoding the offset.






    share|improve this answer





























      0














      Ok, after 2 days, I checked the Python 3.7 changelog, and I found out support for Z as a UTC offset was added in Python 3.7. See this issue on the Python issue tracker, which is primarily about adding support for colons, but also mentions Z support further down the page. Also see the datetime docs, which say




      Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.




      On my class, I had to change the time format to this:



      datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%fZ")


      I changed the %z at the end to Z, hardcoding the offset.






      share|improve this answer



























        0












        0








        0







        Ok, after 2 days, I checked the Python 3.7 changelog, and I found out support for Z as a UTC offset was added in Python 3.7. See this issue on the Python issue tracker, which is primarily about adding support for colons, but also mentions Z support further down the page. Also see the datetime docs, which say




        Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.




        On my class, I had to change the time format to this:



        datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%fZ")


        I changed the %z at the end to Z, hardcoding the offset.






        share|improve this answer















        Ok, after 2 days, I checked the Python 3.7 changelog, and I found out support for Z as a UTC offset was added in Python 3.7. See this issue on the Python issue tracker, which is primarily about adding support for colons, but also mentions Z support further down the page. Also see the datetime docs, which say




        Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.




        On my class, I had to change the time format to this:



        datetime.datetime.strptime(dateTime, "%Y-%m-%dT%H:%M:%S.%fZ")


        I changed the %z at the end to Z, hardcoding the offset.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 0:20









        user2357112

        155k12167260




        155k12167260










        answered Nov 14 '18 at 0:09









        DarkSuniuMDarkSuniuM

        8021119




        8021119





























            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%2f53291250%2fpython-3-6-datetime-strptime-returns-error-while-python-3-7-works-well%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