Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the time functions commonly used in lua

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what the time functions commonly used in lua are, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

-- function: get the current server time, numtype

Function Player:get_cur_sys_time ()

Local last = self.time_span or 0

Return os.time ()-last

End

-- get the current date

Function CommonFunc_getNowDate ()

Local nd = os.date ("* t", g_player:get_cur_sys_time ())

Return {

Year = nd.year

Month = nd.month

Day = nd.day

Hour = nd.hour

Minute = nd.min

Second = nd.sec

}

End

-- convert the stme sent by the server to seconds

Function CommonFunc_ConvertDateToTime (stime)

Local timeDate = os.date ("* t", os.time ())

-- Log (timeDate)

TimeDate.year = stime.year

TimeDate.month = stime.month

TimeDate.day = stime.day

TimeDate.hour = stime.hour or 0

TimeDate.min = stime.minute or 0

TimeDate.sec = stime.second or 0

-- Log (timeDate)

Print ("os.time", os.time (timeDate))

Return os.time (timeDate)

End

-- get the number of days in a certain year and month

Function UIShareToCircle:getMonthDays (year, month)

Local time1 = CommonFunc_ConvertDateToTime ({year=year, month=month, day=1})

-- local time1 = STime:dateToTime ({year=year, month=month, day=1})

Local nextYear, nextMonth = year, month + 1

If nextMonth > 12 then

NextYear = year + 1

NextMonth = 1

End

Local time2 = CommonFunc_ConvertDateToTime ({year=nextYear, month=nextMonth, day=1})

-- local time2 = STime:dateToTime ({year=nextYear, month=nextMonth, day=1})

Return math.floor ((time2-time1) / (2403600))

End

-- get the total number of days from a few days to a few months in the same year

Function UIShareToCircle:calSpaceDaysByMonth (startDay, startMonth, startYear, endMonth, spaceDays)

For month = startMonth,endMonth,1 do

Local monthDays = self:getMonthDays (startYear, month)

If month = = startMonth then

SpaceDays = monthDays-startDay

Else

SpaceDays = spaceDays + monthDays

End

End

Return spaceDays

End

It has been several days since the last login.

Function UIShareToCircle:calInternalDays (startDate)

Local spaceDays = 0Mui-number of days between

Local startDate = startDate-- test data *

Local nowDate = CommonFunc_getNowDate ()-current year, month and day

-- Log ("62 *", nowDate,startDate)

-- compare years first

If nowDate.year > startDate.year then

For I = startDate.year,nowDate.year,1 do

If I = = startDate.year then

SpaceDays = self:calSpaceDaysByMonth (startDate.day, startDate.month, startDate.year, 12, spaceDays)

Elseif I = = nowDate.year then

SpaceDays = self:calSpaceDaysByMonth (nowDate.day, 1, nowDate.year, nowDate.month, spaceDays)

Else

For month = 1, 12, 12, 1 do

Local monthDays = self:getMonthDays (I, month)

SpaceDays = spaceDays + monthDays

End

End

End

Else-- comparison month

If nowDate.month > startDate.month then

For I = startDate.month,nowDate.month,1 do

Local monthDays = self:getMonthDays (nowDate.year, I)

If I = = startDate.month then

SpaceDays = monthDays-startDate.day

Elseif I = = nowDate.month then

SpaceDays = spaceDays + nowDate.day

Else

SpaceDays = spaceDays + monthDays

End

End

Else-- comparison Day

SpaceDays = nowDate.day-startDate.day

End

End

Return spaceDays

End

-- get the current time

Function CommonFunc_getCurTime ()

Local tbCurTime = os.date ("* t", g_Const_Time_Diff + os.time ())

-- turn the incident into something that conforms to the habits of the Chinese people

If tbCurTime.wday = = 1 then

TbCurTime.wday = 7

Else

TbCurTime.wday = tbCurTime.wday-1

End

Return tbCurTime

End

-- some operations of time *

-- judge whether a time is within two times

-- GuildBossOpenTime = {

-- ["start_timer"] = {2014 Jing 1 Ji 1}}

-- ["end_timer"] = {{20999912531}}

-- ["day"] = {1pm 2pm 3pm 4pm 5}

-- ["hour"] = {0pm 24}

--}

Function CommmonFunc_compTimer (tbData, tbCurTime)

TbCurTime = tbCurTime or CommonFunc_getCurTime ()

Local yFlag = CommonFunc_compDataBetData (tbData ["start_timer"] [1], tbData ["end_timer"] [1]

{tbCurTime ["year"], tbCurTime ["month"], tbCurTime ["day"]})

-- if the weekly time is satisfied

Local wFlag = tbData ["day"]: has (tbCurTime ["wday"])

-- if the hour is satisfied

Local hFlag = CommonFunc_compDataBetHS (tbData ["hour"] [1], tbData ["hour"] [2], tbCurTime ["hour"])

Return yFlag and wFlag and hFlag

End

Function CommonFunc_compDataBetData (tbData1,tbData2,detData)

If detData [1]

< tbData1[1] or detData[1] >

TbData2 [1] then

Return false

End

If detData [1] = = tbData1 [1] or detData [1] = = tbData2 [1] then

If detData [2]

< tbData1[2] or detData[2] >

TbData2 [2] then

Return false

End

If detData [2] = = tbData2 [2] or detData [2] = = tbData2 [2] then

If detData [3]

< tbData1[3] or detData[3] >

TbData2 [3] then

Return false

End

End

End

Return true

End

-- judge whether a time is within two times

Function CommonFunc_compDataBetHS (tbData1,tbData2,detData)

If detData > = tbData1 and detData

< tbData2 then return true end return false end -- 获取活动剩余的天数 function UIActivity:getLeftTime(activity_tplt) local leftDays = 0-- 剩余的天数 local nowTb = CommonFunc_getCurTime()--当前的年月日 local startTb = activity_tplt.start_time--活动开始的时间 local endTb = activity_tplt.end_time--活动结束的时间 --先判断当前的时间在不在活动时间内 local tbDate = List({["start_timer"] = List({ {startTb.year,startTb.month,startTb.day,startTb.hour} }), ["end_timer"]= List({ {endTb.year,endTb.month,endTb.day,endTb.hour} }), ["day"]= List({1,2,3,4,5,6,7}), ["hour"]= List({0,24}) }) local inActivityFlag = CommmonFunc_compTimer(tbDate) if inActivityFlag then --先比较年 if endTb.year >

NowTb.year then

For I = nowTb.year,endTb.year,1 do

If I = = nowTb.year then

For month = nowTb.month,12,1 do

Local monthDays = self:getMonthDays (nowTb.year, month)

If month = = nowTb.month then

LeftDays = monthDays-nowTb.day

Else

LeftDays = leftDays + monthDays

End

End

Elseif I = = endTb.year then

For month = 1 do endTb.monthjue

Local monthDays = self:getMonthDays (I, month)

If month = = endTb.month then

LeftDays = leftDays + endTb.day

Else

LeftDays = leftDays + monthDays

End

End

Else

For month = 1, 12, 12, 1 do

Local monthDays = self:getMonthDays (I, month)

LeftDays = leftDays + monthDays

End

End

End

Else-- comparison month

If endTb.month > nowTb.month then

For I = nowTb.month,endTb.month,1 do

Local monthDays = self:getMonthDays (endTb.year, I)

If I = = nowTb.month then

LeftDays = monthDays-nowTb.day

Elseif I = = endTb.month then

LeftDays = leftDays + endTb.day

Else

LeftDays = leftDays + monthDays

End

End

Else-- comparison Day

LeftDays = endTb.day-nowTb.day

End

End

End

Return string.format (StringRes ["activity_copy_1"], leftDays)

End

Thank you for reading this article carefully. I hope the article "what are the common time functions in lua" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report