1-- libquvi-scripts v0.9.20131130
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.
10--
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.
15--
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/>.
19--
20
21local M = {}
22
23--[[
24Convert a string to a timestamp.
25Parameters:
26  s .. String to convert
27Returns:
28  Converted string.
29]]--
30function M.to_timestamp(s) -- Based on <http://is.gd/ee9ZTD>
31  local p = "%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+)"
32
33  local d,m,y,hh,mm,ss = s:match(p)
34  if not d then error('no match: date') end
35
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}
38
39  local m = MON[m]
40  local offset = os.time() - os.time(os.date("!*t"))
41
42  return os.time({day=d,month=m,year=y,
43                  hour=hh,min=mm,sec=ss}) + offset
44end
45
46--[[
47Convert seconds to a timecode string.
48Parameters:
49  s           .. Seconds
50  hours_only  .. true=if seconds >=24h then return hours only
51Returns:
52  Timecode string.
53]]--
54function 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
60end
61
62--[[
63Convert seconds to a timecode dictionary.
64Parameters:
65  s .. Seconds
66Returns:
67  A dictionary containing the hours (hh), the minutes (mm) and the
68  seconds (ss).
69]]--
70function M.to_timecode(s)
71  return {hh=(s/3600)%60, mm=(s/60)%60, ss=s%60}
72end
73
74--[[
75Convert a timecode string to a timecode dictionary.
76Parameters:
77  s .. Timecode string in format "%02d:%02d:%02d"
78Returns:
79  A dictionarary (see `to_timecode' above).
80]]--
81function 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)
87  }
88end
89
90--[[
91Convert a timecode string to seconds.
92Parameters:
93  s .. Timecode string in format "%02d:%02d:%02d"
94Returns:
95  Number of seconds.
96]]--
97function M.timecode_str_to_s(s)
98  local tc = M.from_timecode_str(s)
99  return tc.hh + tc.mm + tc.ss
100end
101
102-- Uncomment to test.
103--[[
104package.path = package.path .. ';../?.lua'
105
106local t = {
107  {tc='00:03:25', s=205},
108  {tc='12:03:25', s=43405},
109  {tc='25:03:25', s=90205}
110}
111
112local n = 0
113for 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)
119    }
120    print(table.concat(r,'\n'))
121    n = n+1
122  end
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)
128    }
129    print(table.concat(r,'\n'))
130    n = n+1
131  end
132end
133print((n == 0) and 'All tests OK' or ('\nerrors: '..n))
134]]--
135
136return M
137
138-- vim: set ts=2 sw=2 tw=72 expandtab:
139