os_core_demo.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. --[[
  2. @module os_core_demo
  3. @summary LuatOS os核心库功能演示模块,严格按照官方API文档展示所有接口
  4. @version 001.000.000
  5. @date 2025.10.23
  6. @author 王棚嶙
  7. @usage
  8. 本模块演示了LuatOS os核心库官方文档中的所有API功能,包括:
  9. 1. os.remove - 删除文件
  10. 2. os.rename - 文件重命名
  11. 3. os.date - 格式化时间日期
  12. 4. os.time - 获取时间戳
  13. 5. os.difftime - 计算时间差
  14. ]]
  15. local function os_core_demo()
  16. log.info("os_core", "===== 开始os核心库API演示 =====")
  17. -- 1. 演示os.date接口 - 格式化时间日期功能
  18. log.info("os.date", "===== 开始os.date接口演示 =====")
  19. -- 获取默认格式的本地时间字符串
  20. local default_time = os.date()
  21. log.info("os.date", "默认格式本地时间:", default_time)
  22. -- 获取指定格式的本地时间字符串
  23. local formatted_time = os.date("%Y-%m-%d %H:%M:%S")
  24. log.info("os.date", "自定义格式本地时间:", formatted_time)
  25. -- 获取UTC时间字符串(使用!前缀)
  26. local utc_time = os.date("!%Y-%m-%d %H:%M:%S")
  27. log.info("os.date", "UTC时间:", utc_time)
  28. -- 获取本地时间table(使用*t)
  29. local local_table = os.date("*t")
  30. log.info("os.date", "本地时间table:", "year=" .. local_table.year .. ", month=" .. local_table.month .. ", day=" .. local_table.day)
  31. -- 获取UTC时间table(使用!*t)
  32. local utc_table = os.date("!*t")
  33. log.info("os.date", "UTC时间table:", "year=" .. utc_table.year .. ", month=" .. utc_table.month .. ", day=" .. utc_table.day)
  34. -- 格式化指定时间戳
  35. local specific_timestamp = os.time({year=2024, month=12, day=25, hour=10, min=30, sec=0})
  36. local specific_time = os.date("%Y年%m月%d日 %H:%M:%S", specific_timestamp)
  37. log.info("os.date", "指定时间戳格式化:", specific_time)
  38. log.info("os.date", "===== os.date接口演示完成 =====")
  39. -- 2. 演示os.time接口 - 获取时间戳功能
  40. log.info("os.time", "===== 开始os.time接口演示 =====")
  41. -- 获取当前时间戳
  42. local current_timestamp = os.time()
  43. log.info("os.time", "当前时间戳:", current_timestamp, "秒")
  44. -- 从指定时间表获取时间戳
  45. local specific_time = {
  46. year = 2024,
  47. month = 12,
  48. day = 25,
  49. hour = 10,
  50. min = 30,
  51. sec = 0
  52. }
  53. local specific_timestamp = os.time(specific_time)
  54. log.info("os.time", "指定时间的时间戳:", specific_timestamp, "秒")
  55. -- 获取当前时间的详细时间信息
  56. local current_time_table = os.date("*t", current_timestamp)
  57. log.info("os.time", "当前时间详细信息:", "year=" .. current_time_table.year .. ", month=" .. current_time_table.month .. ", day=" .. current_time_table.day)
  58. log.info("os.time", "===== os.time接口演示完成 =====")
  59. -- 3. 演示os.difftime接口 - 计算时间差功能
  60. log.info("os.difftime", "===== 开始os.difftime接口演示 =====")
  61. -- 获取两个时间点的时间戳
  62. local time_a = os.time({year=2024, month=1, day=1, hour=0, min=0, sec=0})
  63. local time_b = os.time({year=2025, month=1, day=1, hour=0, min=0, sec=0})
  64. log.info("os.difftime", "时间点A (2024-01-01):", time_a, "秒")
  65. log.info("os.difftime", "时间点B (2025-01-01):", time_b, "秒")
  66. -- 计算时间差
  67. local difference = os.difftime(time_b, time_a)
  68. log.info("os.difftime", "时间差(B-A):", difference, "秒")
  69. -- 转换为天数显示
  70. local days = difference / (24 * 60 * 60)
  71. log.info("os.difftime", "时间差(B-A):", string.format("%.3f", days), "天")
  72. -- 计算负时间差
  73. local negative_diff = os.difftime(time_a, time_b)
  74. log.info("os.difftime", "时间差(A-B):", negative_diff, "秒")
  75. log.info("os.difftime", "===== os.difftime接口演示完成 =====")
  76. -- 4. 演示os.remove接口 - 文件删除功能
  77. log.info("os.remove", "===== 开始os.remove接口演示 =====")
  78. -- 首先创建一个测试文件用于删除
  79. local test_file = "/os_test_delete.txt"
  80. local file = io.open(test_file, "w")
  81. if file then
  82. file:write("这是用于测试os.remove的示例文件内容\n")
  83. file:close()
  84. log.info("os.remove", "创建测试文件成功:", test_file)
  85. else
  86. log.error("os.remove", "创建测试文件失败")
  87. -- 继续演示,尝试删除可能存在的文件
  88. end
  89. -- 验证文件存在
  90. if io.exists(test_file) then
  91. log.info("os.remove", "测试文件存在,准备删除")
  92. else
  93. log.warn("os.remove", "测试文件不存在,将尝试删除操作")
  94. end
  95. -- 使用os.remove删除文件
  96. local success, errmsg = os.remove(test_file)
  97. if success then
  98. log.info("os.remove", "文件删除成功")
  99. -- 验证文件已删除
  100. if not io.exists(test_file) then
  101. log.info("os.remove", "删除验证通过,文件已不存在")
  102. end
  103. else
  104. log.error("os.remove", "文件删除失败,错误信息:", errmsg or "未知错误")
  105. end
  106. log.info("os.remove", "===== os.remove接口演示完成 =====")
  107. -- 5. 演示os.rename接口 - 文件重命名功能
  108. log.info("os.rename", "===== 开始os.rename接口演示 =====")
  109. -- 首先创建源文件
  110. local old_file = "/os_test_old.txt"
  111. local new_file = "/os_test_new.txt"
  112. local file = io.open(old_file, "w")
  113. if file then
  114. file:write("这是用于测试os.rename的源文件内容\n")
  115. file:close()
  116. log.info("os.rename", "创建源文件成功:", old_file)
  117. else
  118. log.error("os.rename", "创建源文件失败")
  119. -- 继续演示,尝试重命名可能存在的文件
  120. end
  121. -- 验证源文件存在
  122. if io.exists(old_file) then
  123. log.info("os.rename", "源文件存在,准备重命名")
  124. else
  125. log.warn("os.rename", "源文件不存在,将尝试重命名操作")
  126. end
  127. -- 清理目标文件(如果存在)
  128. if io.exists(new_file) then
  129. os.remove(new_file)
  130. log.info("os.rename", "清理已存在的目标文件:", new_file)
  131. end
  132. -- 使用os.rename重命名文件
  133. local success, errmsg = os.rename(old_file, new_file)
  134. if success then
  135. log.info("os.rename", "文件重命名成功:", old_file, "->", new_file)
  136. -- 验证原文件不存在
  137. if not io.exists(old_file) then
  138. log.info("os.rename", "原文件已不存在,重命名验证通过")
  139. end
  140. -- 验证新文件存在
  141. if io.exists(new_file) then
  142. log.info("os.rename", "新文件存在验证通过:", new_file)
  143. end
  144. -- 清理测试文件
  145. os.remove(new_file)
  146. log.info("os.rename", "测试文件已清理")
  147. else
  148. log.error("os.rename", "文件重命名失败,错误信息:", errmsg or "未知错误")
  149. -- 尝试清理源文件
  150. if io.exists(old_file) then
  151. os.remove(old_file)
  152. end
  153. end
  154. log.info("os.rename", "===== os.rename接口演示完成 =====")
  155. log.info("os_core", "===== os核心库API演示全部完成 =====")
  156. end
  157. sys.taskInit(os_core_demo)