Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32440 > unrolled thread

date and time comparison how to

Started bynoydb <jenn.duerr@gmail.com>
First post2012-10-29 16:13 -0700
Last post2012-11-02 07:22 -0400
Articles 17 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 16:13 -0700
    Re: date and time comparison how to Gary Herron <gherron@digipen.edu> - 2012-10-29 17:04 -0700
    Re: date and time comparison how to MRAB <python@mrabarnett.plus.com> - 2012-10-30 00:50 +0000
      Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 18:47 -0700
        Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 19:02 -0700
          Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 19:13 -0700
            Re: date and time comparison how to Dave Angel <d@davea.name> - 2012-10-29 23:11 -0400
              Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 21:20 -0700
                Re: date and time comparison how to Chris Angelico <rosuav@gmail.com> - 2012-10-30 17:12 +1100
                Re: date and time comparison how to Dave Angel <d@davea.name> - 2012-10-30 05:35 -0400
              Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 21:20 -0700
            Re: date and time comparison how to MRAB <python@mrabarnett.plus.com> - 2012-10-30 03:30 +0000
          Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 19:13 -0700
        Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 19:02 -0700
      Re: date and time comparison how to noydb <jenn.duerr@gmail.com> - 2012-10-29 18:47 -0700
    RE: date and time comparison how to "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-31 21:04 +0000
    Re: date and time comparison how to Adam  Tauno Williams <awilliam@whitemice.org> - 2012-11-02 07:22 -0400

#32440 — date and time comparison how to

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 16:13 -0700
Subjectdate and time comparison how to
Message-ID<50e576ea-2ebe-4340-954e-6c23b5885680@googlegroups.com>
All,

I need help with a date and time comparison.

Say a user enters a date-n-time and a file on disk.  I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process.

How best to do?  I have looked at the datetime module, tried a few things, no luck.

Is os.stat a part of it?  Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time.  ??

Any help would be appreciated!

[toc] | [next] | [standalone]


#32447

FromGary Herron <gherron@digipen.edu>
Date2012-10-29 17:04 -0700
Message-ID<mailman.3060.1351555829.27098.python-list@python.org>
In reply to#32440
On 10/29/2012 04:13 PM, noydb wrote:
> All,
>
> I need help with a date and time comparison.
>
> Say a user enters a date-n-time and a file on disk.  I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process.
>
> How best to do?  I have looked at the datetime module, tried a few things, no luck.
>
> Is os.stat a part of it?  Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time.  ??
>
> Any help would be appreciated!

Use the datetime module (distributed with Python) to compare date/times.

You can turn a filesystem time into a datetime with something like the 
following:
                 import datetime, os, stat
                 mtime = os.lstat(filename)[stat.ST_MTIME]   // the 
files modification time
                 dt = datetime.datetime.fromtimestamp(mtime)


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

[toc] | [prev] | [next] | [standalone]


#32449

FromMRAB <python@mrabarnett.plus.com>
Date2012-10-30 00:50 +0000
Message-ID<mailman.3061.1351558251.27098.python-list@python.org>
In reply to#32440
On 2012-10-30 00:04, Gary Herron wrote:
> On 10/29/2012 04:13 PM, noydb wrote:
>> All,
>>
>> I need help with a date and time comparison.
>>
>> Say a user enters a date-n-time and a file on disk.  I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process.
>>
>> How best to do?  I have looked at the datetime module, tried a few things, no luck.
>>
>> Is os.stat a part of it?  Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time.  ??
>>
>> Any help would be appreciated!
>
> Use the datetime module (distributed with Python) to compare date/times.
>
> You can turn a filesystem time into a datetime with something like the
> following:
>                   import datetime, os, stat
>                   mtime = os.lstat(filename)[stat.ST_MTIME]   // the
> files modification time
>                   dt = datetime.datetime.fromtimestamp(mtime)
>
>
Instead of os.lstat(filename)[stat.ST_MTIME] you could use
os.path.getmtime(filename).

[toc] | [prev] | [next] | [standalone]


#32452

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 18:47 -0700
Message-ID<6101355b-ae00-4482-96ee-ed60f5cb465f@googlegroups.com>
In reply to#32449
Thanks, I did find this...

pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))

>> pdf_timestamp
>> '102909133000'

... but now how to do the comparison?  Cannot just do a raw string comparison, gotta declare it a date

[toc] | [prev] | [next] | [standalone]


#32455

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 19:02 -0700
Message-ID<85ea1841-6849-4970-b08b-5c7bbb7bba14@googlegroups.com>
In reply to#32452
if I do time.time() I get 1351562187.757, do it again I get 1351562212.2650001 --- so I can compare those, the latter is later then the former.  Good.  SO how do I turn pdf_timeStamp (a string) above into time in this (as from time.time()) format?  Am I on the right track -- is that the way to do a time comparison?

[toc] | [prev] | [next] | [standalone]


#32457

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 19:13 -0700
Message-ID<6f437b0f-1fe5-49c6-98e3-7be615d04858@googlegroups.com>
In reply to#32455
I guess I get there eventually!  
This seems to work

    pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
    intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
    pdfFile_compareTime = time.mktime(intermediateTime)

(and I'll do the same to the user entered date-n-time and then compare)


Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?

[toc] | [prev] | [next] | [standalone]


#32460

FromDave Angel <d@davea.name>
Date2012-10-29 23:11 -0400
Message-ID<mailman.3068.1351566714.27098.python-list@python.org>
In reply to#32457
On 10/29/2012 10:13 PM, noydb wrote:
> I guess I get there eventually!  
> This seems to work
>
>     pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
>     intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
>     pdfFile_compareTime = time.mktime(intermediateTime)
>
> (and I'll do the same to the user entered date-n-time and then compare)
>
>
> Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?

Please read the rest of the thread in particular the message 3 hours ago
from Gary Herron

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

Now you can compare two datetimes simply by
   if dt1 < dt2:

Or you can subtract them, and examine the difference.

What's the need for all that string conversion stuff?



-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#32462

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 21:20 -0700
Message-ID<d6043deb-74d8-4c3a-9661-228ab7f17291@googlegroups.com>
In reply to#32460
On Monday, October 29, 2012 11:11:55 PM UTC-4, Dave Angel wrote:
> On 10/29/2012 10:13 PM, noydb wrote:
> 
> > I guess I get there eventually!  
> 
> > This seems to work
> 
> >
> 
> >     pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
> 
> >     intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
> 
> >     pdfFile_compareTime = time.mktime(intermediateTime)
> 
> >
> 
> > (and I'll do the same to the user entered date-n-time and then compare)
> 
> >
> 
> >
> 
> > Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?
> 
> 
> 
> Please read the rest of the thread in particular the message 3 hours ago
> 
> from Gary Herron
> 
> 
> 
>                 import datetime, os, stat
> 
>                 mtime = os.lstat(filename)[stat.ST_MTIME]   // the files
> 
> modification time
> 
>                 dt = datetime.datetime.fromtimestamp(mtime)
> 
> 
> 
> Now you can compare two datetimes simply by
> 
>    if dt1 < dt2:
> 
> 
> 
> Or you can subtract them, and examine the difference.
> 
> 
> 
> What's the need for all that string conversion stuff?
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

okay, I see.
But for the user supplied date... I'm not sure of the format just yet... testing with a string for now (actual date-date might be possible, tbd later), so like '10292012213000' (oct 29, 2012 9:30pm).  How would you get that input into a format to compare with dt above?

[toc] | [prev] | [next] | [standalone]


#32467

FromChris Angelico <rosuav@gmail.com>
Date2012-10-30 17:12 +1100
Message-ID<mailman.3074.1351577570.27098.python-list@python.org>
In reply to#32462
On Tue, Oct 30, 2012 at 3:20 PM, noydb <jenn.duerr@gmail.com> wrote:
> But for the user supplied date... I'm not sure of the format just yet... testing with a string for now (actual date-date might be possible, tbd later), so like '10292012213000' (oct 29, 2012 9:30pm).  How would you get that input into a format to compare with dt above?

Instead of formatting your other date to match the input, turn the
input into something you can easily manipulate - preferably, Unix time
(seconds since 1970, sometimes referred to as a "time_t"). Once both
your timestamps are integer (or float) seconds since a known epoch,
they can be compared directly, as numbers.

You're already pretty much there with strptime. Poke around with that
and play with your format string and you should have it. But you may
need to tweak it to match your actual input.

ChrisA

[toc] | [prev] | [next] | [standalone]


#32478

FromDave Angel <d@davea.name>
Date2012-10-30 05:35 -0400
Message-ID<mailman.3084.1351589734.27098.python-list@python.org>
In reply to#32462
On 10/30/2012 12:20 AM, noydb wrote:
> On Monday, October 29, 2012 11:11:55 PM UTC-4, Dave Angel wrote:
>> On 10/29/2012 10:13 PM, noydb wrote:
>>
>>> I guess I get there eventually!  
>>
>>> <snip>
>>
>>
> 
> okay, I see.
> But for the user supplied date... I'm not sure of the format just yet... testing with a string for now (actual date-date might be possible, tbd later), so like '10292012213000' (oct 29, 2012 9:30pm).  How would you get that input into a format to compare with dt above?
> 

See http://docs.python.org/2/library/datetime.html

There are a number of constructors for datetime.  For example,

now = datetime.datetime.now()

spec = datetime.strptime(datstring, format)

spec = datetime.datetime(year, month, day[, hour[, minute[, second[,
microsecond)

In each case, you have an optional tz for timezone.  Or if possible, use
utc versions of these functions to get "greenwich" times.  tz is one of
the biggest pains, and the quirks vary between operating systems and
filesystems.  If possible in your environment, use   utcnow,
utcfromtimestamp, etc.


-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#32463

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 21:20 -0700
Message-ID<mailman.3070.1351570848.27098.python-list@python.org>
In reply to#32460
On Monday, October 29, 2012 11:11:55 PM UTC-4, Dave Angel wrote:
> On 10/29/2012 10:13 PM, noydb wrote:
> 
> > I guess I get there eventually!  
> 
> > This seems to work
> 
> >
> 
> >     pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
> 
> >     intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
> 
> >     pdfFile_compareTime = time.mktime(intermediateTime)
> 
> >
> 
> > (and I'll do the same to the user entered date-n-time and then compare)
> 
> >
> 
> >
> 
> > Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?
> 
> 
> 
> Please read the rest of the thread in particular the message 3 hours ago
> 
> from Gary Herron
> 
> 
> 
>                 import datetime, os, stat
> 
>                 mtime = os.lstat(filename)[stat.ST_MTIME]   // the files
> 
> modification time
> 
>                 dt = datetime.datetime.fromtimestamp(mtime)
> 
> 
> 
> Now you can compare two datetimes simply by
> 
>    if dt1 < dt2:
> 
> 
> 
> Or you can subtract them, and examine the difference.
> 
> 
> 
> What's the need for all that string conversion stuff?
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

okay, I see.
But for the user supplied date... I'm not sure of the format just yet... testing with a string for now (actual date-date might be possible, tbd later), so like '10292012213000' (oct 29, 2012 9:30pm).  How would you get that input into a format to compare with dt above?

[toc] | [prev] | [next] | [standalone]


#32461

FromMRAB <python@mrabarnett.plus.com>
Date2012-10-30 03:30 +0000
Message-ID<mailman.3069.1351567835.27098.python-list@python.org>
In reply to#32457
On 2012-10-30 03:11, Dave Angel wrote:
> On 10/29/2012 10:13 PM, noydb wrote:
>> I guess I get there eventually!
>> This seems to work
>>
>>     pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
>>     intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
>>     pdfFile_compareTime = time.mktime(intermediateTime)
>>
>> (and I'll do the same to the user entered date-n-time and then compare)
>>
>>
>> Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?
>
> Please read the rest of the thread in particular the message 3 hours ago
> from Gary Herron
>
>                  import datetime, os, stat
>                  mtime = os.lstat(filename)[stat.ST_MTIME]   // the files
> modification time
>                  dt = datetime.datetime.fromtimestamp(mtime)
>
> Now you can compare two datetimes simply by
>     if dt1 < dt2:
>
> Or you can subtract them, and examine the difference.
>
> What's the need for all that string conversion stuff?
>
+1

Incidentally, the best order for dates is year (4 digits - remember
Y2K? :-)) then month then day.

[toc] | [prev] | [next] | [standalone]


#32458

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 19:13 -0700
Message-ID<mailman.3066.1351563245.27098.python-list@python.org>
In reply to#32455
I guess I get there eventually!  
This seems to work

    pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
    intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
    pdfFile_compareTime = time.mktime(intermediateTime)

(and I'll do the same to the user entered date-n-time and then compare)


Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?

[toc] | [prev] | [next] | [standalone]


#32456

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 19:02 -0700
Message-ID<mailman.3065.1351562588.27098.python-list@python.org>
In reply to#32452
if I do time.time() I get 1351562187.757, do it again I get 1351562212.2650001 --- so I can compare those, the latter is later then the former.  Good.  SO how do I turn pdf_timeStamp (a string) above into time in this (as from time.time()) format?  Am I on the right track -- is that the way to do a time comparison?

[toc] | [prev] | [next] | [standalone]


#32453

Fromnoydb <jenn.duerr@gmail.com>
Date2012-10-29 18:47 -0700
Message-ID<mailman.3063.1351561643.27098.python-list@python.org>
In reply to#32449
Thanks, I did find this...

pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))

>> pdf_timestamp
>> '102909133000'

... but now how to do the comparison?  Cannot just do a raw string comparison, gotta declare it a date

[toc] | [prev] | [next] | [standalone]


#32533

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-10-31 21:04 +0000
Message-ID<mailman.3125.1351717511.27098.python-list@python.org>
In reply to#32440
Gary Herron wrote:
> On 10/29/2012 04:13 PM, noydb wrote:
> > All,
> >
> > I need help with a date and time comparison.
> >
> > Say a user enters a date-n-time and a file on disk.  I want to compare the date and time of the file to the
> entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process.
> >
> > How best to do?  I have looked at the datetime module, tried a few things, no luck.
> >
> > Is os.stat a part of it?  Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's
> correct date and time.  ??
> >
> > Any help would be appreciated!
> 
> Use the datetime module (distributed with Python) to compare date/times.
> 
> You can turn a filesystem time into a datetime with something like the
> following:
>                  import datetime, os, stat
>                  mtime = os.lstat(filename)[stat.ST_MTIME]   // the
> files modification time
>                  dt = datetime.datetime.fromtimestamp(mtime)
> 

You could also write that as:

datetime.datetime.fromtimestamp( os.path.getmtime( path ) )


Ramit P


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#32624

FromAdam Tauno Williams <awilliam@whitemice.org>
Date2012-11-02 07:22 -0400
Message-ID<mailman.3201.1351855379.27098.python-list@python.org>
In reply to#32440
On Mon, 2012-10-29 at 16:13 -0700, noydb wrote:
> All,
> I need help with a date and time comparison.
> Say a user enters a date-n-time and a file on disk.  I want to compare
> the date and time of the file to the entered date-n-time; if the file
> is newer than the entered date-n-time, add the file to a list to
> process.
> How best to do?  I have looked at the datetime module, tried a few
> things, no luck.
> Is os.stat a part of it?  Tried, not sure of the output, the
> st_mtime/st_ctime doesnt jive with the file's correct date and
> time.  ??
> Any help would be appreciated!

Date and time is much more complicated then people guess at first.

<http://taaviburns.ca/what_you_need_to_know_about_datetimes/datetime_transforms.html>
<http://www.whitemiceconsulting.com/2012/10/setting-course-for-utc.html>

What do you mean by "st_mtime/st_ctime doesnt jive with the file's
correct date"?  Is it off my some offset, or does it completely not
match?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web