블로그는 나의 힘!
[ 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
[ Programing ]/Algorithm2022. 4. 21. 22:15

# 조건 : 
1. 기간 있음.
     StartDate : 2022.04.10.     EndDate : 2022.04.25.
     MySQL : tmStartDate = UNIX_TIMESTAMP(startDate)    tmEndDate = UNIX_TIMESTAMP(endDate)

2. 초기화로 정해진 주차가 있음.
     EX) 월요일  (0:월 ~ 6:일)

3. 초기화로 정해진 시간이 있음.
     EX) 10 (0~23)

4. 상수.
     TIME_MINUTE = 60
     TIME_HOUR = TIME_MINUTE * 60
     TIME_DAY = TIME_HOUR * 24      // 86400
     TIME_WEEK = TIME_DAY * 7

5. 그 외.
     1주는 7일
     하루는 86400 (초)
     요일은 0:월 ~ 6:일
     시간은 0 ~ 23
     UNIX_TIMESTAMP -> 1970.01.01 이후 값. 초로 계산된 시간.
                                2022.04.10. Hour(9) -> 1649548800



# 알고리즘 : 
     // [ 허수 Day 계산 ]
     // 정해진 주차(/7) 계산으로 초기화 하기 위해. 시작 주차 차/가산 허수Day 계산.
     ( StartDate_요일 < 초기화_요일 ) 이라면, 
          허수Day_초 = 86400 * ( 7 - (초기화_요일 - StartDate_요일) )
     ( StartDate_요일 > 초기화_요일 ) 이라면, 
          허수Day_초 = 86400 * ( 7 - (StartDate_요일 - 초기화_요일) )

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

     // [ 초기화 시간 체크 ]
     // 초기화 시간에 도달 하지 않았다면 주차 계산(/7) 초기화 안되도록 +1 Day(허수) 가산.
     (현재_요일 == 초기화_요일) 이고, 
     (현재_시간 < 초기화_시간) 이라면, 
          MadeStartDate_초 = MadeStartDate_초 + 86400 

     // [ 남아있는 Day 계산 ]
     남은Day_초 = 현재_일_초 - MadeStartDate_초
     남은Day = 남은Day_초 / TIME_DAY

     // [ 주차 계산 ]
    진행_주차 = ( 남은Day / 7 ) + 1

 

Posted by Mister_Q
[ Programing ]/Lua Scirpt2022. 4. 11. 14:47

[lua] Time Function
: 루아에서는 시스템 OS 시간 관련 정보를 취득할 수 있는 함수 3가지가 있다.


# 요약 

os.date() 시스템의 날짜와 시간을 알 수 있는 함수.
os.time() UTC+0, 1970년 1월 1일 기준으로 경과한 시간을 알 수 있는 함수.
os.clock() 프로그램이 첫 실행후 경과한 시간을 알 수 있는 함수.





-- 초기화 기준 시간 세팅.
local deadLineHour = 13    -- 오후 1시 기준으로 초기화 하겠다.
local deadLineSec = deadLineHour * 60 * 60

-- 현재 시간.
local defaultSec = os.time()

-- 현재 시스템의 날짜와 시간을 스트링 형태의 값을 출력.
local date = os.date()

-- 설정한 시간(timestamp 초)을 Date 타입으로 변경.
local nowDate = os.date( "*t", defaultSec - deadLineSec)     -- deadLine 미초과 시 다음 날짜 미갱신

-- 설정한 Date를 Time (timestamp(초 1Day = 60*60*24)) 타입으로 추출.
local nowTimeAt = os.time{
                               year = nowDate.year,
                               month = nowDate.month,
                               day = nowDate.day,
                               hour = nowDate.hour,
                               min = nowDate.min,
                               sec = nowDate.sec
                           }

print( nowTimeAt )
/* 출력 : 1704266112 */



-- 인수에 스트링 '*t' 를 입력해 줌으로 년, 월, 일, 시, 분, 초, 요일, 섬머 타임 여부 등을 

-- Key를 가지는 딕셔너리 형태의 테이블로 반환 받을수 있다.
local dateTable = os.date( '*t' )

for key, value in pair(dateTable) do
     print( key, value )
end
/* 출력 :
      year 2021
      month 4
      day 16
      hour 17
      min 35
      sec 13
      yday 106
      wday 6
      isdst true
*/

print( dateTable.year, dateTable.month, dateTable.day )
-- 출력 : 2021, 4, 16

print( dateTable.hour, dateTable.min, dateTable.sec )
-- 출력 : 17, 35, 13



/*
그 외 인수 : 

%a abbreviated weekday name (e.g., Wed)
%A full weekday name (e.g., Wednesday)
%b abbreviated month name (e.g., Sep)
%B full month name (e.g., September)
%c date and time (e.g., 09/16/98 23:48:10)
%d day of the month (16) [01-31]
%H hour, using a 24-hour clock (23) [00-23]
%I hour, using a 12-hour clock (11) [01-12]
%M minute (48) [00-59]
%m month (09) [01-12]
%p either "am" or "pm" (pm)
%S second (10) [00-61]
%w weekday (3) [0-6 = Sunday-Saturday]
%x date (e.g., 09/16/98)
%X time (e.g., 23:48:10)
%Y full year (1998)
%y two-digit year (98) [00-99]
%% the character `%´

*/

-- 원하는 날짜 데이터를 문자열과 같이 반환
print( os.date('오늘은 %a요일, %b월 %d일 입니다.') )
-- 출력 : '오늘은 토요일, 3월 26일 입니다.'

-- 년 월 일
date = os.date( '%x' )
print( date )
-- 출력 : 2022-03-26

-- 년 월 일 시 분 초
date = os.date( "%Y%m%d%H%M%S", os.time() )
print( date )
-- 출력 : 20220326143000

-- 시간
time = os.date( '%X' )
print( time )
-- 출력 : 17:29:31

-- 요일 (0:일, 1:월, 2:화, 3:수, 4:목, 5:금, 6:토)
weekday = os.date( '%w' )
print( weekday )
-- 출력 : 6




출처 : [lua] 날짜와 시간 함수 os.date() (tistory.com)

 

[lua] 날짜와 시간 함수 os.date()

[lua] Time Function 루아에서는 시스템OS 시간 관련 정보를 취득할수있는 함수가 크게 3가지가 있습니다. 시스템의 날짜와 시간을 알 수 있는 함수 os.date() UTC+0, 1970년 1월 1일 기준으로 경과한 시간을

plcman.tistory.com



 

Posted by Mister_Q