Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How can I know if a date is prior to today? (http://www.velocityreviews.com/forums/t677160-how-can-i-know-if-a-date-is-prior-to-today.html)

Giampaolo Rodola' 03-25-2009 01:31 PM

How can I know if a date is prior to today?
 
Hi,
I have a date expressed in seconds.
I'd want to pretty print it as "%H:%M" if the time refers to today and
"%b%d" (month, day) if it's of yesterday or before.

I managed to do that with the code below but I don't like it too much.
Is there a better way to do that?
Thanks in advance.


import time

today_day = time.strftime("%d", time.localtime(time.time()))
mytime = time.localtime(time.time() - (60*60*30)) # dummy time prior
to today
if time.strftime("%d", mytime) == today_day:
print time.strftime("%H:%M", mytime)
else:
print time.strftime("%b%d", mytime)


--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil

Paul McGuire 03-25-2009 01:43 PM

Re: How can I know if a date is prior to today?
 
On Mar 25, 8:31*am, "Giampaolo Rodola'" <gne...@gmail.com> wrote:
> Hi,
> I have a date expressed in seconds.
> I'd want to pretty print it as "%H:%M" if the time refers to today and
> "%b%d" (month, day) if it's of yesterday or before.
>


Use datetime module.

import time
from datetime import datetime
now = time.time()
thisTimeYesterday = now - 24*3600

print datetime.fromtimestamp(now) < datetime.today()
print datetime.fromtimestamp(thisTimeYesterday) < datetime.today()


-- Paul

Tim Chase 03-25-2009 01:51 PM

Re: How can I know if a date is prior to today?
 
> I have a date expressed in seconds.
> I'd want to pretty print it as "%H:%M" if the time refers to today and
> "%b%d" (month, day) if it's of yesterday or before.
>
> I managed to do that with the code below but I don't like it too much.
> Is there a better way to do that?
> Thanks in advance.
>
>
> import time
>
> today_day = time.strftime("%d", time.localtime(time.time()))
> mytime = time.localtime(time.time() - (60*60*30)) # dummy time prior
> to today
> if time.strftime("%d", mytime) == today_day:
> print time.strftime("%H:%M", mytime)
> else:
> print time.strftime("%b%d", mytime)


Well, date/datetime objects are directly comparable:

import datetime
today_day = datetime.date.today()
other = datetime.datetime.fromtimestamp(your_timestamp)
if other.date() == today_day:
fmt = "%H:%M"
else:
fmt = "%b%d"
print other.strftime(fmt)

-tkc




Giampaolo Rodola' 03-25-2009 02:20 PM

Re: How can I know if a date is prior to today?
 
On 25 Mar, 14:51, Tim Chase <python.l...@tim.thechases.com> wrote:
> > I have a date expressed in seconds.
> > I'd want to pretty print it as "%H:%M" if the time refers to today and
> > "%b%d" (month, day) if it's of yesterday or before.

>
> > I managed to do that with the code below but I don't like it too much.
> > Is there a better way to do that?
> > Thanks in advance.

>
> > import time

>
> > today_day = time.strftime("%d", time.localtime(time.time()))
> > mytime = time.localtime(time.time() - (60*60*30)) *# dummy time prior
> > to today
> > if time.strftime("%d", mytime) == today_day:
> > * * print time.strftime("%H:%M", mytime)
> > else:
> > * * print time.strftime("%b%d", mytime)

>
> Well, date/datetime objects are directly comparable:
>
> * *import datetime
> * *today_day = datetime.date.today()
> * *other = datetime.datetime.fromtimestamp(your_timestamp)
> * *if other.date() == today_day:
> * * *fmt = "%H:%M"
> * *else:
> * * *fmt = "%b%d"
> * *print other.strftime(fmt)
>
> -tkc


Thanks, it did the job.

--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil

John Machin 03-25-2009 02:43 PM

Re: How can I know if a date is prior to today?
 
On Mar 26, 12:51*am, Tim Chase <python.l...@tim.thechases.com> wrote:
> > I have a date expressed in seconds.
> > I'd want to pretty print it as "%H:%M" if the time refers to today and
> > "%b%d" (month, day) if it's of yesterday or before.

>
> > I managed to do that with the code below but I don't like it too much.
> > Is there a better way to do that?
> > Thanks in advance.

>
> > import time

>
> > today_day = time.strftime("%d", time.localtime(time.time()))
> > mytime = time.localtime(time.time() - (60*60*30)) *# dummy time prior
> > to today
> > if time.strftime("%d", mytime) == today_day:
> > * * print time.strftime("%H:%M", mytime)
> > else:
> > * * print time.strftime("%b%d", mytime)

>
> Well, date/datetime objects are directly comparable:
>
> * *import datetime
> * *today_day = datetime.date.today()
> * *other = datetime.datetime.fromtimestamp(your_timestamp)
> * *if other.date() == today_day:
> * * *fmt = "%H:%M"
> * *else:
> * * *fmt = "%b%d"
> * *print other.strftime(fmt)
>
> -tkc


time tuples are directly comparable, too:

import time
today_day = time.localtime(time.time())[:3]
other = time.localtime(your_timestamp)
if other[:3] == today_day:
etc
etc

Cheers,
John


All times are GMT. The time now is 08:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.