Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > date and time comparison how to

Reply
Thread Tools

date and time comparison how to

 
 
MRAB
Guest
Posts: n/a
 
      10-30-2012
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.pat h.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.
 
Reply With Quote
 
 
 
 
noydb
Guest
Posts: n/a
 
      10-30-2012
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.pat h.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?
 
Reply With Quote
 
 
 
 
noydb
Guest
Posts: n/a
 
      10-30-2012
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.pat h.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?
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      10-30-2012
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
 
Reply With Quote
 
Prasad, Ramit
Guest
Posts: n/a
 
      10-31-2012
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; ifthe 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 datetimemodule, 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 jivewith 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.
 
Reply With Quote
 
Adam Tauno Williams
Guest
Posts: n/a
 
      11-02-2012
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
date-time comparison, aware vs naive noydb Python 7 12-10-2012 09:20 PM
w3.org suggestion .. page, date, time and topic, date, time code (wish list). Keith Cochrane HTML 2 08-06-2006 06:57 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date & Time chooser for java 1.1 - using only the mouse to select time & date Chris Berg Java 0 10-27-2003 10:59 PM



Advertisments