Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2006, 5:30 PM   #1
Steveire
Newbie
 
Join Date: Jan 2006
Posts: 13
Rep Power: 0 Steveire is on a distinguished road
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.
Steveire is offline   Reply With Quote
Old Jan 26th, 2006, 5:48 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
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 is offline   Reply With Quote
Old Jan 26th, 2006, 6:36 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
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
Sane is offline   Reply With Quote
Old Jan 27th, 2006, 12:53 AM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 27th, 2006, 1:19 PM   #5
Steveire
Newbie
 
Join Date: Jan 2006
Posts: 13
Rep Power: 0 Steveire is on a distinguished road
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.
Steveire is offline   Reply With Quote
Old Jan 27th, 2006, 1:27 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Jan 27th, 2006, 4:54 PM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Jan 28th, 2006, 4:16 AM   #8
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
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.
hydroxide is offline   Reply With Quote
Old Jan 28th, 2006, 10:58 AM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 28th, 2006, 1:07 PM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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".
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:00 AM.

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