clock.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. --@author cedlemo
  2. local os = os
  3. local string = string
  4. local awful = require("awful")
  5. local textbox = require("wibox.widget.textbox")
  6. local text_box = require("blingbling.text_box")
  7. local days_of_week_in_kanji={
  8. "日曜日", "月曜日","火曜日","水曜日","木曜日","金曜日","土曜日"
  9. }
  10. local days_of_week_in_kana={
  11. "にちようび","げつようび","かようび","すいようび","もくようび","きんようび",
  12. "どようび"
  13. }
  14. local kanji_numbers={
  15. "一","ニ","三","四","五","六","七","八","九","十"
  16. }
  17. local kanas_numbers={
  18. "いち","に","さん","し","ご","ろく","しち","はち","く","じゅう"
  19. }
  20. local days_of_month_in_kanas={
  21. "ついたち","ふつか","みっか","よっか","いつか","むいか","なのか","ようか",
  22. "ここのか","とおか","ジュウイチニチ","ジュウニニチ","ジュウサンニチ",
  23. "じゅうよっか", "ジュウゴニチ","ジュウロクニチ","ジュウシチニチ",
  24. "ジュウハチニチ","ジュウクニチ","はつか","ニジュウイチニチ","ニジュウニニチ",
  25. "ニジュウサンニチ","にじゅうよっか", "ニジュウゴニチ","ニジュウロクニチ",
  26. "ニジュウシチニチ","ニジュウハチニチ","ニジュウクニチ","サンジュウニチ",
  27. "サンジュウイチニチ"
  28. }
  29. ---A clock module
  30. --@module blingbling.clock
  31. local function get_day_of_month_in_kanji(n)
  32. if n<=10 then
  33. return kanji_numbers[n] .. "日"
  34. elseif n<20 then
  35. return kanji_numbers[10]..(kanji_numbers[n-10] or "") .. "日"
  36. elseif n<30 then
  37. return kanji_numbers[2]..kanji_numbers[10]..(kanji_numbers[n-20] or "").. "日"
  38. elseif n<=31 then
  39. return kanji_numbers[3]..kanji_numbers[10]..(kanji_numbers[n-30] or "").. "日"
  40. end
  41. end
  42. local function get_month_in_kanji(n)
  43. if n<=10 then
  44. return kanji_numbers[n].."月"
  45. elseif n<=12 then
  46. return kanji_numbers[10]..(kanji_numbers[n-10] or "").."月"
  47. end
  48. end
  49. romajis_days_of_month={}
  50. local function get_current_day_of_week_in_kanji()
  51. return days_of_week_in_kanji[tonumber(os.date("%w") + 1)]
  52. end
  53. local function get_current_day_of_week_in_kanas()
  54. return days_of_week_in_kana[tonumber(os.date("%w") + 1)]
  55. end
  56. local function get_current_day_of_month_in_kanji()
  57. return get_day_of_month_in_kanji(tonumber(os.date("%d")))
  58. end
  59. local function get_current_day_of_month_in_kanas()
  60. return days_of_month_in_kanas[tonumber(os.date("%d"))]
  61. end
  62. local function get_current_month_in_kanji()
  63. return get_month_in_kanji(tonumber(os.date("%m")))
  64. end
  65. local function get_current_hour()
  66. return os.date("%H")
  67. end
  68. local function get_current_minutes()
  69. return os.date("%M")
  70. end
  71. local function get_current_time_in_japanese( str)
  72. --if type(string) ~= "string" then
  73. -- return nil
  74. --end
  75. local result = str or "%m、%d、%w、%H時%M分"
  76. result = string.gsub(result,"%%w",get_current_day_of_week_in_kanji())
  77. result = string.gsub(result,"%%d",get_current_day_of_month_in_kanji())
  78. result = string.gsub(result,"%%m",get_current_month_in_kanji())
  79. result = os.date(result)
  80. return result
  81. end
  82. local function japanese_clock(str, args)
  83. local clock = text_box(args)
  84. clock:set_text(get_current_time_in_japanese( str ))
  85. clocktimer = timer({ timeout = 1 })
  86. clocktimer:connect_signal("timeout", function() clock:set_text(get_current_time_in_japanese( str )) end)
  87. clocktimer:start()
  88. --clock_tooltip= awful.tooltip({
  89. -- objects = { clock },
  90. -- timer_function= function()
  91. -- return os.date("%B, %d, %A, %H:%M")
  92. -- end,
  93. -- })
  94. return clock
  95. end
  96. ---A clock that displays the date and the time in kanjis. This clock have a popup that shows the current date in your langage.
  97. --@usage myclock = blingbling.japanese_clock() --then just add it in your wibox like a classical widget
  98. --@name japanese_clock
  99. --@class function
  100. --@return a clock widget
  101. return {
  102. japanese = japanese_clock,
  103. get_current_time_in_japanese = get_current_time_in_japanese,
  104. get_current_day_of_week_in_kanji = get_current_day_of_week_in_kanji,
  105. get_current_day_of_month_in_kanji = get_current_day_of_month_in_kanji,
  106. get_current_month_in_kanji = get_current_month_in_kanji,
  107. get_current_day_of_month_in_kanas = get_current_day_of_month_in_kanas,
  108. get_current_day_of_week_in_kanas = get_current_day_of_week_in_kanas
  109. }