2016-10-26 4 views
1

내 프로그램에서 시간 변경을 시뮬레이션하려고합니다. 예를 들어, 2016 년 10 월 26 일 (오늘부터) (내 var 'param'이 0에서 시작하는 경우) 실제 프로그램에서 1 초가 내 프로그램에서 1 시간이됩니다. 그래서 매 1 초마다 내 'param + = 1'을 통과했습니다. 그러면 시간 변화를 시뮬레이션 할 수 있습니다.파이썬 시뮬레이션 시간과 매월 말 찾기

이제 매월 의 모든 시작 부분에 몇 가지 통계를 작성하고 싶습니다.. 이 경우, 내가하고 싶은 1 통계는 내 질문에 내가 'PARAM'

today = date.today() 
    if t0 == '0': 
     time_now = today.strftime("%Y-%m-%d %H") 
    else: 
     time_now = (today + relativedelta(hours=int(param))).strftime("%Y-%m-%d %H") 
+0

월 값 '% m'이 (가) 이전 값에서 변경되면 새 달을 입력했습니다. –

답변

2
의 값에 의해이 시점 (매월 초)을 찾을 수있는 방법입니다 11월 2016 년의 1 일 자정에있다

한 시간에 3600 초가 있습니다. 따라서, 게임 시간은 항상 곱한 경과 '실시간'(timedelta)는 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) 

당신이 다음 달 1 일까지의 시간을 얻고 싶다면 . 우리는 다음과 같이 할 수 있습니다. 나는 우리가 이미/대신 시도의 current_datetime

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 

로 게임 현재 날짜/시간을 가정합니다 그럴 수 단순히 테스트 if cur_month != 12

또는

from dateutil.relativedelta import relativedelta 

def next_month(current_datetime): 
    return (current_datetime + relativedelta(months=1)).replace(day=1, hour=0, minute=0, second=0) 


def hours_until(dt1, dt2): 
    delta = dt2 - dt1 
    hours = (delta.hours * 24) + (delta.seconds/3600) 
    return hours 
+0

아마도 제 질문은 분명하지 않았습니다. 내가 정말로 알고 싶어하는 것은 지금과 지금과 월말과의 차이입니다. – wildcolor

+0

@wildcolor 아, 이제 알겠습니다. 미안해, 내 실수 야. 이 정보를 포함하도록 내 대답을 편집했습니다. 귀하의 답변에 대해 – sytech

+0

감사합니다. 나는 이미 내 자신의 대답을 이미 염두에두고 있었다. 그러나 당신의 대답은 또한 많은 도움이됩니다. 실제로 'until_next_month'를 'num_of_hours'로 어떻게 변경할 수 있는지 알려 주실 수 있습니까? – wildcolor

3

이 힘 제외 당신을위한 일 :

from datetime import datetime 
from dateutil.relativedelta import relativedelta 


def seconds_until_next_month(): 
    return (datetime(year=datetime.now().year, month=datetime.now().month, day=1) + 
      relativedelta(months=1) - datetime.now()).seconds 


def hours_until_next_month(): 
    return seconds_until_next_month()/60.0/60.0 

트릭은을 사용하는 것입니다.현재 월을 증가시킵니다. 이것은 연말에 적절히 포장됩니다.

+0

relativedelta는 좋은 접근 방법입니다. 원래 2 월에 문제가 생길 수 있다고 생각했는데 (IE'Jan30 + relativedelta (months = 1)') 3 월 대신에 2 월 29 일에 적절하다고 생각했습니다. – sytech

+0

@sytech 예. 그것이 다음 요일에 처음으로가는 이유입니다. 요일을 추가하는 대신에 무엇이든합니다. 그것은 모든 윤년 등을 처리합니다. –