I am trying to simulate time change in my program. For example, if I start (today) from 0 26th Oct 2016 (my var 'param' starts from 0), then one second in the real world will be one hour in my program. So every 1 second passed my 'param += 1'. Then I can simulate the time change in hours.
Now I want to do some stats in the every beginning of every month. in this case, the 1st stats I want to do is at 0am on the 1st of Nov 2016. My question is how can I find this point (the beginning of every month) by the value of 'param'
today = date.today()
if t0 == '0':
time_now = today.strftime("%Y-%m-%d %H")
time_now = (today + relativedelta(hours=int(param))).strftime("%Y-%m-%d %H")
There are 3600 seconds in an hour. Therefore, your game time is always the elapsed 'realtime' (timedelta) multiplied by 3600.
import datetime def time_shift(t1, t2): '''returns the game-time timedelta based on two 'real' datetime objects''' real_elapsed = t2 - t1 game_elapsed = real_elapsed * 3600 return game_elapsed
>>> t1 = datetime.datetime(year=2016, month=10, day=26) >>> delta = datetime.timedelta(seconds=30) #lets say 30 seconds realtime passes #that should be 1 day 6 hours game-time. >>> t2 = t1 + delta >>> time_shift(t1, t2) datetime.timedelta(1, 21600) #1 day, 21600 seconds IE 1 day and 6 hours #You can translate that to a specific date for the game, too >>> game_date = t1 + time_shift(t1, t2) >>> game_date datetime.datetime(2016, 10, 27, 6, 0)
If you want to get the time until the first day of the next month. We can do that as follows. I'll assume we already have the current date/time in-game as
cur_month, cur_year = current_datetime.month, current_datetime.year try: next_month = datetime.datetime(year=cur_year, month=cur_month+1, day=1) except ValueError: #If it was december next_month = datetime.datetime(year=cur_year+1, month=1, day=1) until_next_month = next_month - current_datetime
Instead of a try/except you could also simply test
if cur_month != 12