common.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. ---模块功能:通用库函数、编码格式转换、时区时间转换
  2. -- @module common
  3. -- @author zhutianhua
  4. -- @license MIT
  5. -- @copyright OpenLuat.com
  6. -- @release 2017.02.20
  7. --定义模块,导入依赖库
  8. module(..., package.seeall)
  9. --加载常用的全局函数至本地
  10. local tinsert, ssub, sbyte, schar, sformat, slen = table.insert, string.sub, string.byte, string.char, string.format, string.len
  11. --- ascii字符串的unicode编码的16进制字符串 转化为 ascii字符串
  12. -- @string,inum:待转换字符串
  13. -- @return string,转换后的字符串
  14. -- @usage local str = common.ucs2toascii("0031003200330034")
  15. -- @usage str is "1234"
  16. function ucs2toascii(inum)
  17. local tonum = {}
  18. for i = 1, slen(inum), 4 do
  19. tinsert(tonum, tonumber(ssub(inum, i, i + 3), 16) % 256)
  20. end
  21. return schar(unpack(tonum))
  22. end
  23. --- ascii字符串 转化为 ascii字符串的unicode编码的16进制字符串(仅支持数字和+)
  24. -- @string,inum:待转换字符串
  25. -- @return string,转换后的字符串
  26. -- @usage local str = common.nstrToUcs2Hex("+1234")
  27. -- @usage str is "002B0031003200330034"
  28. function nstrToUcs2Hex(inum)
  29. local hexs = ""
  30. local elem = ""
  31. for i = 1, slen(inum) do
  32. elem = ssub(inum, i, i)
  33. if elem == "+" then
  34. hexs = hexs .. "002B"
  35. else
  36. hexs = hexs .. "003" .. elem
  37. end
  38. end
  39. return hexs
  40. end
  41. --- ASCII字符串 转化为 BCD编码格式字符串(仅支持数字和+)
  42. -- @string,num:待转换字符串
  43. -- @return string,转换后的字符串
  44. -- @usage local str = common.numtobcdnum("+8618126324567")
  45. -- @usage str is "91688121364265f7" (表示第1个字节是0x91,第2个字节为0x68,......)
  46. function numtobcdnum(num)
  47. local len, numfix, convnum = slen(num), "81", ""
  48. if ssub(num, 1, 1) == "+" then
  49. numfix = "91"
  50. len = len - 1
  51. num = ssub(num, 2, -1)
  52. end
  53. if len % 2 ~= 0 then --奇数位
  54. for i = 1, len / 2 do
  55. convnum = convnum .. ssub(num, i * 2, i * 2) .. ssub(num, i * 2 - 1, i * 2 - 1)
  56. end
  57. convnum = convnum .. "F" .. ssub(num, len, len)
  58. else --偶数位
  59. for i = 1, len / 2 do
  60. convnum = convnum .. ssub(num, i * 2, i * 2) .. ssub(num, i * 2 - 1, i * 2 - 1)
  61. end
  62. end
  63. return numfix .. convnum
  64. end
  65. --- BCD编码格式字符串 转化为 号码ASCII字符串(仅支持数字和+)
  66. -- @string,num:待转换字符串
  67. -- @return string,转换后的字符串
  68. -- @usage local str = common.bcdnumtonum("91688121364265f7") --表示第1个字节是0x91,第2个字节为0x68,......
  69. -- @usage str is "+8618126324567"
  70. function bcdnumtonum(num)
  71. local len, numfix, convnum = slen(num), "", ""
  72. if len % 2 ~= 0 then
  73. print("your bcdnum is err " .. num)
  74. return
  75. end
  76. if ssub(num, 1, 2) == "91" then
  77. numfix = "+"
  78. end
  79. len, num = len - 2, ssub(num, 3, -1)
  80. for i = 1, len / 2 do
  81. convnum = convnum .. ssub(num, i * 2, i * 2) .. ssub(num, i * 2 - 1, i * 2 - 1)
  82. end
  83. if ssub(convnum, len, len) == "f" or ssub(convnum, len, len) == "F" then
  84. convnum = ssub(convnum, 1, -2)
  85. end
  86. return numfix .. convnum
  87. end
  88. --- unicode小端编码 转化为 gb2312编码
  89. -- @param ucs2s,unicode小端编码数据
  90. -- @return param,gb2312编码数据
  91. -- @usage local gb = common.ucs2togb2312(ucs2s)
  92. function ucs2togb2312(ucs2s)
  93. local cd = iconv.open("gb2312", "ucs2")
  94. return cd:iconv(ucs2s)
  95. end
  96. --- gb2312编码 转化为 unicode小端编码
  97. -- @param gb2312s,gb2312编码数据
  98. -- @return param,unicode小端编码数据
  99. -- @usage local ucs = common.gb2312toucs2(gb2312s)
  100. function gb2312toucs2(gb2312s)
  101. local cd = iconv.open("ucs2", "gb2312")
  102. return cd:iconv(gb2312s)
  103. end
  104. --- unicode大端编码 转化为 gb2312编码
  105. -- @param ucs2s,unicode大端编码数据
  106. -- @return param ,gb2312编码数据
  107. -- @usage gb = common.ucs2betogb2312(ucs2s)
  108. function ucs2betogb2312(ucs2s)
  109. local cd = iconv.open("gb2312", "ucs2be")
  110. return cd:iconv(ucs2s)
  111. end
  112. --- gb2312编码 转化为 unicode大端编码
  113. -- @param gb2312s,gb2312编码数据
  114. -- @return param,unicode大端编码数据
  115. -- @usage local ucs = common.gb2312toucs2be(gb2312s)
  116. function gb2312toucs2be(gb2312s)
  117. local cd = iconv.open("ucs2be", "gb2312")
  118. return cd:iconv(gb2312s)
  119. end
  120. --- unicode小端编码 转化为 utf8编码
  121. -- @param ucs2s,unicode小端编码数据
  122. -- @return param ,utf8编码数据
  123. -- @usage u8 = common.ucs2toutf8(ucs2s)
  124. function ucs2toutf8(ucs2s)
  125. local cd = iconv.open("utf8", "ucs2")
  126. return cd:iconv(ucs2s)
  127. end
  128. --- utf8编码 转化为 unicode小端编码
  129. -- @param utf8s,utf8编码数据
  130. -- @return param,unicode小端编码数据
  131. -- @usage local ucs = common.utf8toucs2(utf8s)
  132. function utf8toucs2(utf8s)
  133. local cd = iconv.open("ucs2", "utf8")
  134. return cd:iconv(utf8s)
  135. end
  136. --- unicode大端编码 转化为 utf8编码
  137. -- @param ucs2s,unicode大端编码数据
  138. -- @return param ,utf8编码数据
  139. -- @usage u8 = common.ucs2betoutf8(ucs2s)
  140. function ucs2betoutf8(ucs2s)
  141. local cd = iconv.open("utf8", "ucs2be")
  142. return cd:iconv(ucs2s)
  143. end
  144. --- utf8编码 转化为 unicode大端编码
  145. -- @param utf8s,utf8编码数据
  146. -- @return param,unicode大端编码数据
  147. -- @usage local ucs = common.utf8toucs2be(utf8s)
  148. function utf8toucs2be(utf8s)
  149. local cd = iconv.open("ucs2be", "utf8")
  150. return cd:iconv(utf8s)
  151. end
  152. --- utf8编码 转化为 gb2312编码
  153. -- @param utf8s,utf8编码数据
  154. -- @return param,gb2312编码数据
  155. -- @usage local gb = common.utf8togb2312(utf8s)
  156. function utf8togb2312(utf8s)
  157. local cd = iconv.open("ucs2", "utf8")
  158. local ucs2s = cd:iconv(utf8s)
  159. cd = iconv.open("gb2312", "ucs2")
  160. return cd:iconv(ucs2s)
  161. end
  162. --- gb2312编码 转化为 utf8编码
  163. -- @param gb2312s,gb2312编码数据
  164. -- @return param ,utf8编码数据
  165. -- @usage local u8 = common.gb2312toutf8(gb2312s)
  166. function gb2312toutf8(gb2312s)
  167. local cd = iconv.open("ucs2", "gb2312")
  168. local ucs2s = cd:iconv(gb2312s)
  169. cd = iconv.open("utf8", "ucs2")
  170. return cd:iconv(ucs2s)
  171. end
  172. local function timeAddzone(y, m, d, hh, mm, ss, zone)
  173. if not y or not m or not d or not hh or not mm or not ss then
  174. return
  175. end
  176. hh = hh + zone
  177. if hh >= 24 then
  178. hh = hh - 24
  179. d = d + 1
  180. if m == 4 or m == 6 or m == 9 or m == 11 then
  181. if d > 30 then
  182. d = 1
  183. m = m + 1
  184. end
  185. elseif m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 then
  186. if d > 31 then
  187. d = 1
  188. m = m + 1
  189. end
  190. elseif m == 12 then
  191. if d > 31 then
  192. d = 1
  193. m = 1
  194. y = y + 1
  195. end
  196. elseif m == 2 then
  197. if (((y + 2000) % 400) == 0) or (((y + 2000) % 4 == 0) and ((y + 2000) % 100 ~= 0)) then
  198. if d > 29 then
  199. d = 1
  200. m = 3
  201. end
  202. else
  203. if d > 28 then
  204. d = 1
  205. m = 3
  206. end
  207. end
  208. end
  209. end
  210. local t = {}
  211. t.year, t.month, t.day, t.hour, t.min, t.sec = y, m, d, hh, mm, ss
  212. return t
  213. end
  214. local function timeRmozone(y, m, d, hh, mm, ss, zone)
  215. if not y or not m or not d or not hh or not mm or not ss then
  216. return
  217. end
  218. hh = hh + zone
  219. if hh < 0 then
  220. hh = hh + 24
  221. d = d - 1
  222. if m == 2 or m == 4 or m == 6 or m == 8 or m == 9 or m == 11 then
  223. if d < 1 then
  224. d = 31
  225. m = m - 1
  226. end
  227. elseif m == 5 or m == 7 or m == 10 or m == 12 then
  228. if d < 1 then
  229. d = 30
  230. m = m - 1
  231. end
  232. elseif m == 1 then
  233. if d < 1 then
  234. d = 31
  235. m = 12
  236. y = y - 1
  237. end
  238. elseif m == 3 then
  239. if (((y + 2000) % 400) == 0) or (((y + 2000) % 4 == 0) and ((y + 2000) % 100 ~= 0)) then
  240. if d < 1 then
  241. d = 29
  242. m = 2
  243. end
  244. else
  245. if d < 1 then
  246. d = 28
  247. m = 2
  248. end
  249. end
  250. end
  251. end
  252. local t = {}
  253. t.year, t.month, t.day, t.hour, t.min, t.sec = y, m, d, hh, mm, ss
  254. return t
  255. end
  256. --- 当前时区的时间转换为新时区的时间
  257. -- @param y,当前时区年份
  258. -- @param m,当前时区月份
  259. -- @param d,当前时区天
  260. -- @param hh,当前时区小时
  261. -- @param mm,当前时区分
  262. -- @param ss,当前时区秒
  263. -- @param pretimezone,当前时区
  264. -- @param nowtimezone,新时区
  265. -- @return table ,返回新时区对应的时间,table格式{year,month.day,hour,min,sec}
  266. function transftimezone(y, m, d, hh, mm, ss, pretimezone, nowtimezone)
  267. local t = {}
  268. local zone = nil
  269. zone = nowtimezone - pretimezone
  270. if zone >= 0 and zone < 23 then
  271. t = timeAddzone(y, m, d, hh, mm, ss, zone)
  272. elseif zone < 0 and zone >= -24 then
  273. t = timeRmozone(y, m, d, hh, mm, ss, zone)
  274. end
  275. return t
  276. end