Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   I can print 'today', but can I print 'yesterday'? (http://www.programmingforums.org/showthread.php?t=8128)

Steveire Jan 26th, 2006 5:30 PM

I can print 'today', but can I print 'yesterday'?
 
:

import time
today = time.strftime("%d %B %Y")
yesterday = str(int(time.strftime("%d"))-1) + time.strftime(" %B %Y")
print today, yesterday

Will work only if 'today' is not the first day of the month, otherwise, it will tell me that yesterday is the 0th of February for example. Is there a SMRT way to use the time module (or any other) to get 'yesterday'?

Aside:
Rather than bumping the other thread again: I got a working MediaWiki bot after finding I was using the wrong version of python with the pywikipediabot.

Sane Jan 26th, 2006 5:48 PM

I looked into it. I thought there might be a way to set the timezone back 24 hours. Doesn't look like there is.

Maybe you could change your computer clock with the win32api? But that might be very inconvenient.

I'd just make a short dictionary of how many days in each month, then a general function around that. Wouldn't be too hard (depending how experienced you are).

Sane Jan 26th, 2006 6:36 PM

Wee. I decided to try it out myself.

time.gmtime() will convert the seconds since epoch to a date. So if we take the current seconds since epoch time.time() and offset it back a day, voila!

:

import time

def time_zone( offset = 0 ): # DEFAULT = GMT
    new = time.gmtime( time.time() + offset*60*60 )
    return "Day: %s | Month: %s | Year: %s\n%s:%s:%s"%(new[2],new[1],new[0],new[3],new[4],new[5])

print time_zone( -5 )


I'm -5 from GMT, so if I then set it to -29, it gives me yesterday's date.

Hope this helps. =D

Dietrich Jan 27th, 2006 12:53 AM

The module datetime is the one you want to use:
:

import datetime
today = datetime.date.today()
oneday = datetime.timedelta(days=1)
yesterday = today - oneday
print today, yesterday


Steveire Jan 27th, 2006 1:19 PM

Thanks both, but it's not in the form I'd like, ie '31 January 2006', '1 Febraury 2006'. Is that as simple? I checked some of the documentation on ptyhon.org, but didn't immediately find anything.

Sane Jan 27th, 2006 1:27 PM

You could do that easily by yourself...

o_O I'll throw something up, just a moment.

EDIT:

:

import time

def yday(time_zone):  #HOURS FROM GMT!!
    new  = time.gmtime(time.time()+(time_zone-24)*3600)
    mon  = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    return "%s %s %s"%(new[2],mon[int(new[1])-1],new[0])

today = time.strftime("%d %B %Y")
yesterday = yday(-5)

print today, yesterday



Then just change -5 to how many hours you are from GMT.

Arevos Jan 27th, 2006 4:54 PM

Quote:

Originally Posted by Steveire
Thanks both, but it's not in the form I'd like, ie '31 January 2006', '1 Febraury 2006'. Is that as simple? I checked some of the documentation on ptyhon.org, but didn't immediately find anything.

Just as the time module has the strftime function, which converts a timestamp into a string based on a user-specified formatting template, so too do the objects in the datetime module:
:

import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
print today.strftime("%e %B %Y"), yesterday.strftime("%e %B %Y")

The formatting codes seem to tally with those used in the standard Unix "date" command (IIRC, PHP and SQL use the same codes as well). The time module doc page has a list of formatting codes, but this does not appear to be complete. At least under Linux, I find that "%e" represents the day of the month (as it does with the Unix date command), yet this is not listed in the Python documentation.

hydroxide Jan 28th, 2006 4:16 AM

Quote:

Originally Posted by Arevos
At least under Linux, I find that "%e" represents the day of the month

Not under Windows, alas =P

-T.

Dietrich Jan 28th, 2006 10:58 AM

With Windows use the same code you use for time:
:

import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
print today.strftime("%d %B %Y"), yesterday.strftime("%d %B %Y")  # 28 January 2006  27 January 2006
# or military date
print today.strftime("%d%b%Y"), yesterday.strftime("%d%b%Y")  # 28Jan2006  27Jan2006


Arevos Jan 28th, 2006 1:07 PM

Quote:

Originally Posted by Dietrich
With Windows use the same code you use for time

But %e is subtly different from %d. %d creates "01", whilst %e creates " 1".


All times are GMT -5. The time now is 4:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC