블로그는 나의 힘!
[ Programing ]/Lua Scirpt2022. 4. 21. 22:55

     TIME_MINUTE = 60
     TIME_HOUR = TIME_MINUTE * 60
     TIME_DAY = TIME_HOUR * 24
     TIME_WEEK = TIME_DAY * 7

     -- 현재 시간
     g_CurrentTime = os.time()

     local DAY_SEC = TIME_MINUTE * TIME_HOUR * TIME_DAY     -- 86400
     local EVERY_WEEK = 1     -- 0 : 일 ~ 6 : 토
     local EVERY_HOUR = 0     -- 0 ~ 23 시
     local tmStartDate = 1649548800     -- 2022.04.10. Hour(9)
     local tmEndDate = 1649548800 + ( 86400 * DAY_SEC )     -- 2022.04.25. Hour(9)



function GetTermDays( _dateSec, _defaultSec )
     local nowDate = os.date( "*t", _defaultSec )
     local baseDate = os.date( "*t", _dateSec )
     local nowDateAt = os.time( { year = nowDate.year, month = nowDate.month, day = nowDate.day } )
     local baseDateAt = os.time( { year = baseDate.year, month = baseDate.month, day = baseDate.day } )

     return ( math.floor( ( math.abs( nowDateAt - baseDateAt ) / TIME_DAY ) ) )
end 


function GetCurrentWeek()
     -- 기간 체크
     if g_CurrentTime < tmStartDate and g_CurrentTime > tmEndDate then
          return 0
     end

     local customDate = DAY_SEC

     -- 정해진 주차(/7) 계산으로 초기화 하기 위해. 시작 주차 차/가산 허수Day 계산.
     local startWeekDay = tonumber( os.date( "%w", tmStartDate ) )
     if startWeekDay < EVERY_WEEK then
          local tempWeek = EVERY_WEEK - startWeekDay
          customDate = customDate * ( 7 - tempWeek )
     else
          local tempWeek = startWeekDay - EVERY_WEEK
          customDate = customDate * tempWeek
     end

     -- [ 초기화 주차 계산 - 보정 ]
     -- EX) StartDate 요일이 일요일. 초기화 월요일 라면, 
     -- -> 주차 계산(/7) 위해 : 일요일 -6 Day(허수) = 월요일로 설정.
     local madeStartDate = tmStartDate - customDate

     -- [ 초기화 시간 체크- 보정 ]
     local currentWeekDay = tonumber( os.date( "%w", g_CurrentTime ) )
     if currentWeekDay == everyWeek then
          local currentHour = tonumber( os.date( "%H", g_CurrentTime ) )
          if currentHour < EVERY_HOUR then
               -- 초기화 시간에 도달 하지 않았다면 주차 계산(/7) 초기화 안되도록 +1 Day(허수) 가산.
               madeStartDate = madeStartDate + DAY_SEC
          end
     end

     local termDays = GetTermDays( madeStartDate, g_CurrentTime )
     local weekIndex = math.floor( tonumber( ( termDays / 7 ) + 1 ) )     -- 실수 -> 정수로. 소수는 버림.

     return weekIndex
end




참고 : Mi_Q Kingdom :: 매주 이벤트(퀘스트) 주차 초기화 계산. (tistory.com)

 

매주 이벤트(퀘스트) 주차 초기화 계산.

# 조건 : 1. 기간 있음.  StartDate : 2022.04.10.     EndDate : 2022.04.25.  MySQL : tmStartDate = UNIX_TIMESTAMP(startDate)    tmEndDate = UNIX_TIMESTAMP(endDate) 2. 초기화로 정해진 ..

goguri.tistory.com



 

Posted by Mister_Q