lua time zone

Time zone

The world is divided into 24 time zones (12 each in the East and West). It is stipulated that the UK is in the middle time zone (zero time zone), east 1-12 zone, west 1-12 zone, east positive and west negative. The east and west 12th districts each span 7.5 degrees of longitude, bounded by east and west longitudes of 180 degrees. Calculation method: the time moves eastward (+) and westward (-), the date moves eastward (-) and westward (+). In other words, one hour is added for each time zone passing eastward, and one hour is subtracted for each time zone passing westward. (The time difference between two adjacent time zones is 1 hour)
insert image description here

global time zone division

 
The time in East 8 District of my country is always 1 hour earlier than that in Thailand East 7 District, and 1 hour behind the time in East 9 District of Japan.
 
 
Time difference = time zone difference Time
  zone calculation example:
   Example 1: Given that the time in Tokyo (East Ninth District) is 12:00 on May 1, find the district time of Beijing (East Eighth District).
        (+9)(East Ninth District) – (+8)(East Eighth District) = 12:00 – x
        X = 12:00 – 1 = 11:00Example
   2: The known Beijing time is May 1 at 12:00 00, find the zone time of New York (West 5th District
        ) (+8) (East 8th District) – (-5) (West 5th District) = 12:00 – y
        Y = 12:00 – 13 = 23:00 (4 March 30)

 

Time zone calculation in lua

	local os_time = os.time() -- 返回时间戳,不受时区影响,单位秒
	local os_date = os.date("*t", os_time) -- 把时间戳转换为日期,受到时区的影响,
											--就是说会自动转换为当前时区的时间
	local os_date2 = os.date("*t", os_time) -- 把时间戳转换为日期,不受时区的影响,
											--因为它返回的是格林尼治(时区是0)的时间
	local timezone = os.difftime(os_time, os.time(os.date("!*t", os_time))) / 3600 -- 当前时区



	案例1:假设某个活动(暂称情缘活动)在某天开放,在那天11:00过后可以购买道具,怎么判断是否到了11:00?
			(活动的开启控制假设在其他地方,在这里假设活动已经开启了,不再处理活动开启相关。)
	解析:注意11:00是服务器上的11:00,如果我们在其他时区,那么当服务器上是11:00时我们的时间并不是
			11:00,不能通过os_date的hour来和11:00比较。我们的目标是把时间转换为服务器所在时区的时间。
			假设服务器在东8区,我们在东2区,我们的时间会比服务器晚6个小时。
			local currentTimeZone = 2
			local serverTimeZone = 8
			local os_time = os.time() -- 获取时间戳
			-- 如果直接把这个时间戳转换为我们时区的时间,结果会比服务器小6个小时,所以即使服务器上已经
			-- 是11:00,我们转换到本地后我们的时间是5:00,所以我们要补偿上6个小时
			local time_stamp_server = os_time + (serverTimeZone - currentTimeZone) * 60 * 60 -- 补偿
			local time_server = os.date("*t", time_stamp_server) -- 得到服务器上的时间
			if time_server.hour >= 11 then
				-- do something
			else
				-- do something
			end
	封装:
	function GetTimeZone()
		local os_time = os.time()
		return os.difftime(os_time, os.time(os.date("!*t", os_time))) / 3600
	end
	function GetServerTimeHour(serverTimeZone, timeStamp)
		local time_stamp_server = timeStamp + (serverTimeZone - GetTimeZone()) * 60 * 60
		-- 如果需要考虑夏令时的影响
		-- local isDst = os.date("*t").isdst
		-- time_stamp_server = time_stamp_server - (isDst and 1 or 0) * 60 * 60
		local time_server = os.date("*t", time_stamp_server)
		return time_server.hour
	end
	

 
Time zone introduction and calculation: https://www.zhuankeshuo.com/articles/4944287.html
Time calculation: https://blog.csdn.net/qq_33337811/article/details/69295317
os.date(): https:// www.cnblogs.com/gangtie/p/14837213.html
Daylight Saving Time: https://blog.csdn.net/yiluxiangbei000/article/details/126640670

Guess you like

Origin blog.csdn.net/baidu_38392815/article/details/129554711