![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 13
Rep Power: 0
![]() |
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, yesterdayAside: 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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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). |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2006
Posts: 13
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
print today.strftime("%e %B %Y"), yesterday.strftime("%e %B %Y") |
|
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
-T. |
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
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, yesterdayThen just change -5 to how many hours you are from GMT. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
I was wondering if there was an easier way to do the following:
from time import ctime, mktime, time
def ctime_to_epoch(ctime):
x1 = ctime.split(' ')
x2 = x1[3].split(':')
localtime = (int(x1[-1]),
{"Jan":1,"Feb":2,"Mar":3,"Apr":4,
"May":5,"Jun":6,"Jul":7,"Aug":8,
"Sep":9,"Oct":10,"Nov":11,"Dec":12}[x1[1]],
int(x1[2]),int(x2[0]),int(x2[1]),int(x2[2]),0,0,0)
return mktime(localtime)
ctime = ctime()
time = time()
print ctime_to_epoch(ctime)
print timeConverting a ctime literal back to seconds since epoch. I need to take forum post times and determine which posts are new by converting post times to epoch and comparing it to the epoch of his/her last visit. |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
import time
def timestr_to_epoch(timestr):
localtime = time.strptime(timestr, "%a %b %d %H:%M:%S %Y")
return time.mktime(localtime)
print timestr_to_epoch(time.ctime())
print time.time() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|