2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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/>.
24 Convert a string to a timestamp.
26 s .. String to convert
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}
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
47 Convert seconds to a timecode string.
50 hours_only .. true=if seconds >=24h then return hours only
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)
58 return string.format('%02d:%02d:%02d', (s
/3600)%60, (s
/60)%60, s
%60)
63 Convert seconds to a timecode dictionary.
67 A dictionary containing the hours (hh), the minutes (mm) and the
70 function M
.to_timecode(s
)
71 return {hh
=(s
/3600)%60, mm
=(s
/60)%60, ss
=s
%60}
75 Convert a timecode string to a timecode dictionary.
77 s .. Timecode string in format "%02d:%02d:%02d"
79 A dictionarary (see `to_timecode' above).
81 function M
.from_timecode_str(s
)
82 local hh
,mm
,ss
= s
:match('(%d+)%:(%d+)%:(%d+)')
84 hh
= tonumber(hh
or 0)*3600,
85 mm
= tonumber(mm
or 0)*60,
86 ss
= tonumber(ss
or 0)
91 Convert a timecode string to seconds.
93 s .. Timecode string in format "%02d:%02d:%02d"
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.
104 package.path = package.path .. ';../?.lua'
107 {tc='00:03:25', s=205},
108 {tc='12:03:25', s=43405},
109 {tc='25:03:25', s=90205}
113 for k,v in pairs(t) do
114 local s = M.timecode_str_to_s(v.tc)
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'))
123 local tc = M.to_timecode_str(s)
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'))
133 print((n == 0) and 'All tests OK' or ('\nerrors: '..n))
138 -- vim: set ts=2 sw=2 tw=72 expandtab: