date-time comparison, aware vs naive

Discussion in 'Python' started by noydb, Dec 10, 2012.

  1. noydb

    noydb Guest

    I want to compare a user entered date-and-time against the date-and-time ofa pdf file. I posted on this (how to get a file's date-time) before, was advised to do it like:

    import datetime, os, stat
    mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification time
    dt = datetime.datetime.fromtimestamp(mtime)

    I am having problems with the comparison, that line is failing. I think I may have figured out the issue -- I think it is a matter of the file's timebeing 'aware' and the user-input date-time being 'naive'. The user-input date-time has a parameter type of date (calender and time tool supplied to enter), but is it 'aware'? The comparison is not working so I think that it is not aware. I can successfully compare two pdf file times against one another. So, is there a way to cast that user-input value (prints as 2/10/2012 3:19:57 PM) as an 'aware' date-time? How? And can anyone confirm that my findings are probably correct?

    Thanks for any help.
    noydb, Dec 10, 2012
    #1
    1. Advertising

  2. noydb

    John Gordon Guest

    In <> noydb <> writes:

    > I want to compare a user entered date-and-time against the date-and-time of
    > a pdf file. I posted on this (how to get a file's date-time) before, was
    > advised to do it like:


    > import datetime, os, stat
    > mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification time
    > dt = datetime.datetime.fromtimestamp(mtime)


    > I am having problems with the comparison, that line is failing.


    What line? You haven't posted any comparison line of code here.

    Please post the actual code you're using, instead of telling us about it.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Dec 10, 2012
    #2
    1. Advertising

  3. noydb

    noydb Guest

    noydb, Dec 10, 2012
    #3
  4. On Mon, 10 Dec 2012 11:57:37 -0800, noydb wrote:

    > I want to compare a user entered date-and-time against the date-and-time
    > of a pdf file. I posted on this (how to get a file's date-time) before,
    > was advised to do it like:
    >
    > import datetime, os, stat
    > mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification
    > time


    What language are you writing? Using // for comments is not Python.


    > dt = datetime.datetime.fromtimestamp(mtime)
    >
    > I am having problems with the comparison, that line is failing.


    You haven't shown us the comparison line. Would you like us to guess what
    it does?

    My guess is that you are doing this:

    if mtime is dtime: ...

    Am I close?

    If not, please forgive me, my crystal ball is often faulty.


    > I think
    > I may have figured out the issue -- I think it is a matter of the file's
    > time being 'aware' and the user-input date-time being 'naive'.


    "Aware" of what?



    --
    Steven
    Steven D'Aprano, Dec 10, 2012
    #4
  5. noydb

    noydb Guest

    On Monday, December 10, 2012 3:52:55 PM UTC-5, Steven D'Aprano wrote:
    > On Mon, 10 Dec 2012 11:57:37 -0800, noydb wrote:
    >
    >
    >
    > > I want to compare a user entered date-and-time against the date-and-time

    >
    > > of a pdf file. I posted on this (how to get a file's date-time) before,

    >
    > > was advised to do it like:

    >
    > >

    >
    > > import datetime, os, stat

    >
    > > mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification

    >
    > > time

    >
    >
    >
    > What language are you writing? Using // for comments is not Python.
    >
    >
    >
    >
    >
    > > dt = datetime.datetime.fromtimestamp(mtime)

    >
    > >

    >
    > > I am having problems with the comparison, that line is failing.

    >
    >
    >
    > You haven't shown us the comparison line. Would you like us to guess what
    >
    > it does?
    >
    >
    >
    > My guess is that you are doing this:
    >
    >
    >
    > if mtime is dtime: ...
    >
    >
    >
    > Am I close?
    >
    >
    >
    > If not, please forgive me, my crystal ball is often faulty.
    >
    >
    >
    >
    >
    > > I think

    >
    > > I may have figured out the issue -- I think it is a matter of the file's

    >
    > > time being 'aware' and the user-input date-time being 'naive'.

    >
    >
    >
    > "Aware" of what?
    >
    >
    >
    >
    >
    >
    >
    > --
    >
    > Steven


    Forgive me, I was just copying the code from the original reply to my orignal question.

    Forgive me for not posting the comparison line, it goes something like
    if one_time > another_time:

    Forgive me - the 'aware' time vs 'naive' time refers to documentation I found for the datetime module, see second sentence down http://docs.python.org/2/library/datetime.html
    noydb, Dec 10, 2012
    #5
  6. noydb

    Dave Angel Guest

    On 12/10/2012 03:52 PM, Steven D'Aprano wrote:
    > On Mon, 10 Dec 2012 11:57:37 -0800, noydb wrote:
    >
    >> I want to compare a user entered date-and-time against the date-and-time
    >> of a pdf file. I posted on this (how to get a file's date-time) before,
    >> was advised to do it like:
    >>
    >> import datetime, os, stat
    >> mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification
    >> time

    > What language are you writing? Using // for comments is not Python.
    >
    >
    >> dt = datetime.datetime.fromtimestamp(mtime)
    >>
    >> I am having problems with the comparison, that line is failing.

    > You haven't shown us the comparison line. Would you like us to guess what
    > it does?
    >
    > My guess is that you are doing this:
    >
    > if mtime is dtime: ...
    >
    > Am I close?
    >
    > If not, please forgive me, my crystal ball is often faulty.
    >
    >
    >> I think
    >> I may have figured out the issue -- I think it is a matter of the file's
    >> time being 'aware' and the user-input date-time being 'naive'.

    > "Aware" of what?
    >
    >

    http://docs.python.org/2/library/datetime
    """ An object of type *time* or *datetime* may be naive or *aware"

    aware refers to time-zone and daylight savings time, such political
    ephemerals. Two times can only be changed if one knows they're both in
    the same one, or if one knows precisely what each is.
    *
    naive assumes the former, while aware trusts the latter.


    To the OP: please specify your python version, your OS, and show your
    source. Also show the complete error traceback. And while you're at it,
    it might be useful to know the type of drive the file is on, since
    Windows uses local times on FAT32 partitions, and gmt on NTFS partitions.

    I suspect you're on Windows, so I can't help you with this nonsense. But I can at least help you ask a clear question.

    --

    DaveA
    Dave Angel, Dec 10, 2012
    #6
  7. noydb

    noydb Guest


    >
    > >

    >
    > >

    >
    > http://docs.python.org/2/library/datetime
    >
    > """ An object of type *time* or *datetime* may be naive or *aware"
    >
    >
    >
    > aware refers to time-zone and daylight savings time, such political
    >
    > ephemerals. Two times can only be changed if one knows they're both in
    >
    > the same one, or if one knows precisely what each is.
    >
    > *
    >
    > naive assumes the former, while aware trusts the latter.
    >
    >
    >
    >
    >
    > To the OP: please specify your python version, your OS, and show your
    >
    > source. Also show the complete error traceback. And while you're at it,
    >
    > it might be useful to know the type of drive the file is on, since
    >
    > Windows uses local times on FAT32 partitions, and gmt on NTFS partitions.
    >
    >
    >
    > I suspect you're on Windows, so I can't help you with this nonsense. But I can at least help you ask a clear question.
    >
    >
    >
    > --
    >
    >
    >
    > DaveA


    Fair enough, thanks
    noydb, Dec 10, 2012
    #7
  8. noydb

    noydb Guest


    >
    > >

    >
    > >

    >
    > http://docs.python.org/2/library/datetime
    >
    > """ An object of type *time* or *datetime* may be naive or *aware"
    >
    >
    >
    > aware refers to time-zone and daylight savings time, such political
    >
    > ephemerals. Two times can only be changed if one knows they're both in
    >
    > the same one, or if one knows precisely what each is.
    >
    > *
    >
    > naive assumes the former, while aware trusts the latter.
    >
    >
    >
    >
    >
    > To the OP: please specify your python version, your OS, and show your
    >
    > source. Also show the complete error traceback. And while you're at it,
    >
    > it might be useful to know the type of drive the file is on, since
    >
    > Windows uses local times on FAT32 partitions, and gmt on NTFS partitions.
    >
    >
    >
    > I suspect you're on Windows, so I can't help you with this nonsense. But I can at least help you ask a clear question.
    >
    >
    >
    > --
    >
    >
    >
    > DaveA


    Fair enough, thanks
    noydb, Dec 10, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Berg
    Replies:
    0
    Views:
    785
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,244
    Michael Borgwardt
    May 30, 2004
  3. Keith Cochrane
    Replies:
    2
    Views:
    610
    Keith Cochrane
    Aug 6, 2006
  4. Roy Smith
    Replies:
    4
    Views:
    263
    Roy Smith
    Jan 27, 2013
  5. Roy Smith
    Replies:
    8
    Views:
    182
Loading...

Share This Page