FIX: quvi/time: to_timecode_str: Return formatted string
[libquvi-scripts.git] / share / common / quvi / time.lua
blobae0e35f267abe0d169ed94ff1cfa7f0824f9b234
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 return M
76 -- vim: set ts=2 sw=2 tw=72 expandtab: