quvi/time: Impl. from_timecode_str, timecode_str_to_s
[libquvi-scripts.git] / share / common / quvi / time.lua
blob0581a8db62b1c441b7228456af5f59b40f5d439b
1 -- libquvi-scripts
2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 local M = {}
23 --[[
24 Convert a string to a timestamp.
25 Parameters:
26 s .. String to convert
27 Returns:
28 Converted string.
29 ]]--
30 function M.to_timestamp(s) -- Based on <http://is.gd/ee9ZTD>
31 local p = "%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+)"
33 local d,m,y,hh,mm,ss = s:match(p)
34 if not d then error('no match: date') end
36 local MON = {Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8,
37 Sep=9, Oct=10, Nov=11, Dec=12}
39 local m = MON[m]
40 local offset = os.time() - os.time(os.date("!*t"))
42 return os.time({day=d,month=m,year=y,
43 hour=hh,min=mm,sec=ss}) + offset
44 end
46 --[[
47 Convert seconds to a timecode string.
48 Parameters:
49 s .. Seconds
50 hours_only .. true=if seconds >=24h then return hours only
51 Returns:
52 Timecode string.
53 ]]--
54 function M.to_timecode_str(s, hours_only)
55 if hours_only and s >= 86400 then -- 24h
56 return string.format('%d hours', (s/3600)%60)
57 else
58 return string.format('%02d:%02d:%02d', (s/3600)%60, (s/60)%60, s%60)
59 end
60 end
62 --[[
63 Convert seconds to a timecode dictionary.
64 Parameters:
65 s .. Seconds
66 Returns:
67 A dictionary containing the hours (hh), the minutes (mm) and the
68 seconds (ss).
69 ]]--
70 function M.to_timecode(s)
71 return {hh=(s/3600)%60, mm=(s/60)%60, ss=s%60}
72 end
74 --[[
75 Convert a timecode string to a timecode dictionary.
76 Parameters:
77 s .. Timecode string in format "%02d:%02d:%02d"
78 Returns:
79 A dictionarary (see `to_timecode' above).
80 ]]--
81 function M.from_timecode_str(s)
82 local hh,mm,ss = s:match('(%d+)%:(%d+)%:(%d+)')
83 return {
84 hh = tonumber(hh or 0)*3600,
85 mm = tonumber(mm or 0)*60,
86 ss = tonumber(ss or 0)
88 end
90 --[[
91 Convert a timecode string to seconds.
92 Parameters:
93 s .. Timecode string in format "%02d:%02d:%02d"
94 Returns:
95 Number of seconds.
96 ]]--
97 function M.timecode_str_to_s(s)
98 local tc = M.from_timecode_str(s)
99 return tc.hh + tc.mm + tc.ss
102 -- Uncomment to test.
103 --[[
104 package.path = package.path .. ';../?.lua'
106 local t = {
107 {tc='00:03:25', s=205},
108 {tc='12:03:25', s=43405},
109 {tc='25:03:25', s=90205}
112 local n = 0
113 for k,v in pairs(t) do
114 local s = M.timecode_str_to_s(v.tc)
115 if s ~= v.s then
116 local r = {
117 string.format('\n input: %s (#%d)', v.tc, n),
118 string.format('expected: %d, got %d', v.s, s)
120 print(table.concat(r,'\n'))
121 n = n+1
123 local tc = M.to_timecode_str(s)
124 if tc ~= v.tc then
125 local r = {
126 string.format('\n input: %s (#%d)', s, n),
127 string.format('expected: %d, got %d', v.tc, tc)
129 print(table.concat(r,'\n'))
130 n = n+1
133 print((n == 0) and 'All tests OK' or ('\nerrors: '..n))
134 ]]--
136 return M
138 -- vim: set ts=2 sw=2 tw=72 expandtab: