File: TimeCode.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (102 lines) | stat: -rw-r--r-- 2,812 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * TimeCode.cpp
 * The TimeCode class
 * Copyright (C) 2011 Simon Newton
 */

#include <iomanip>
#include <iostream>
#include <string>
#include "ola/Logging.h"

#include "ola/timecode/TimeCode.h"
#include "ola/timecode/TimeCodeEnums.h"

namespace ola {
namespace timecode {

using std::setw;
using std::setfill;
using std::string;


TimeCode::TimeCode(const TimeCode &other)
    : m_type(other.m_type),
      m_hours(other.m_hours),
      m_minutes(other.m_minutes),
      m_seconds(other.m_seconds),
      m_frames(other.m_frames) {
}

TimeCode& TimeCode::operator=(const TimeCode &other) {
  m_type = other.m_type;
  m_hours = other.m_hours;
  m_minutes = other.m_minutes;
  m_seconds = other.m_seconds;
  m_frames = other.m_frames;
  return *this;
}

/**
 * Returns true if this timecode value is consistent, given the time
 */
bool TimeCode::IsValid() const {
  if (m_hours > MAX_HOURS ||
      m_minutes > MAX_MINUTES ||
      m_seconds > MAX_SECONDS)
    return false;

  switch (m_type) {
    case TIMECODE_FILM:
      return m_frames < 24;
    case TIMECODE_EBU:
      return m_frames < 25;
    case TIMECODE_DF:
      return m_frames < 30;
    case TIMECODE_SMPTE:
      return m_frames < 30;
  }
  return false;
}

string TimeCode::AsString() const {
  std::ostringstream str;
  str << setw(2) << setfill('0') << static_cast<int>(m_hours) << ":"
    << setw(2) << setfill('0') << static_cast<int>(m_minutes) << ":"
    << setw(2) << setfill('0') << static_cast<int>(m_seconds) << ":"
    << setw(2) << setfill('0') << static_cast<int>(m_frames);
  return str.str();
}

std::ostream& operator<<(std::ostream &out, const TimeCode &t) {
  return out << t.AsString();
}

bool TimeCode::operator==(const TimeCode &other) const {
  return (m_type == other.m_type &&
          m_hours == other.m_hours &&
          m_minutes == other.m_minutes &&
          m_seconds == other.m_seconds &&
          m_frames == other.m_frames);
}


bool TimeCode::operator!=(const TimeCode &other) const {
  return !(*this == other);
}
}  // namespace timecode
}  // namespace ola