I’m interested in implementing a day-night cycle for my game, but I’m not exactly sure how to go about doing it. I’ve looked over python’s time module, and I can only find functions that return the time as “Sat Nov 29 12:45:02 2014” whereas I would rather have it just simply return “12:45 PM” or something similar. My plan was to try and do something like this:
if time == '5:00 PM':
render.colorScaleInterval(5, (0.8, 0.7, 0.7, 1))
I know of course that this doesn’t work, but shouldn’t there be something as simple as that? If anyone could help me out, that’d be great. Thanks.
Would it be possible to take the function that returns the time as “Sat Nov 29 12:45:02 2014”, and do something like the following:
print time.thefunction().split(" ")?
that wouldn’t have the AM or PM, though.
I think that you may find what you want in time.localtime: it produces a struct containing the current local time (which can be accessed as though it were a tuple or via its named attributes (such as “tm_hour”)–details should be available here)).
For example, the following code:
timeVal = time.localtime()
print timeVal.tm_hour, timeVal.tm_min, timeVal.tm_sec
should print out the current, local time as hours, minutes and seconds as of the function call.
That works great, Thaumaturge, but I wanted to try something like this:
if timeVal.tm_hour == 22 and timeVal.tm_min == 30:
print timeVal.tm_hour, ':', timeVal.tm_min
I tested this code and for whatever reason it does not update. However, it would work when I run my .py file exactly at 22:30. Maybe there’s something I’m misunderstanding about tasks?
You have to call time.localtime() every time you call UpdateTime or it will keep the original values. Also, consider using the datetime module since it gives you more flexibility with your display format. For example:
from datetime import datetime
dt = datetime.now()
if dt.hour == 22 and dt.minute == 30: