# Get day and time in python and assign that value to BGE property

I am trying to make a realistic day/night/season simulator for a BGE project. the lighting and sky will be animated based on the time of day, and the ground will have 365 frames - animated between green grass, leaves, and snow, as the seasons progress. I know how to make everything run perfectly, except for one part.

I have an object, called Time of Day Control Empty. (the name could easily be changed if it is too long, or the spaces are a problem)

I need a script to, upon running, find how many days it is into the year, and assign that value to the property "Day"

Then, I want the script to constantly update the property "Minutes" with the value of how many minutes into the day it currently is based on the current time.

The property "Day" should only range between 0-365, and the property "Minutes" should range between 0.00-1440.99 - It would be nice if the seconds were represented in .00 - .99 based on how far it was into each minute, at the end of the float property "Minutes," but that isn't really necessary.

Supposing that the current time was 2:27:27 PM on December 25th, the properties should look just like this:

I'm sure this is possible, but I have no idea where to start. How can I go about making this script?

• oooo can't wait to see some good answers. – Scalia Dec 4 '15 at 19:55
• BTW your example time seems off.. If seconds are to go from .0 to .99, 2:27:27PM should be 867.45. Do you want seconds to be scaled? Or left as .0 to .59? – gandalf3 Dec 4 '15 at 21:21
• scaled. my example is incorrect.... (your answer answered what I wanted even though I asked the wrong thing!) – X-27 Dec 4 '15 at 21:46

You can get the current time in python with the time module (who would've thunk, right?)

time.localtime() gives you a struct containing everything you need:

import time
import bge

def clamp(n, nmin, nmax):
if n > nmax:
return nmax
elif n < nmin:
return nmin
else:
return n

cont = bge.logic.getCurrentController()
own = cont.owner

# this returns a struct containing the year, month, day etc.
now = time.localtime()

# combine the hour and minute to get the total number of minutes, plus seconds (clamped to avoid tangling with leap seconds) scaled to .00 to .99
own['minute'] = now.tm_min + now.tm_hour*60 + clamp(now.tm_sec, 0, 59)*.01667
# we don't even need to do anything to get the year day. Note that this can return 356 on leap days, so be sure you either clamp it here or handle such a situation properly later.
own['day'] = now.tm_yday


Just hook it up to an always sensor. I set the skip on the always sensor so it only gets executed once a second at 60 tics per second, to avoid using extra precious cpu time:

• This is exactly what I wanted! already have it working in my blend. Thanks! (Uved and Accepted) – X-27 Dec 4 '15 at 21:17