Add timecode methods to Time
[dcpomatic.git] / src / lib / dcpomatic_time.h
index 05b4e1a5d7d1d2061fe26327068632e03c79c78b..4afc9ab40623e09a226965ce8cdfb7e7a77d7784 100644 (file)
@@ -21,6 +21,9 @@
 #define DCPOMATIC_TIME_H
 
 #include <cmath>
+#include <ostream>
+#include <sstream>
+#include <iomanip>
 #include <stdint.h>
 #include "frame_rate_change.h"
 
@@ -55,8 +58,40 @@ public:
                return rint (_t * r / HZ);
        }
 
-       operator bool () const {
-               return _t != 0;
+       template <typename T>
+       void split (T r, int& h, int& m, int& s, int& f) const
+       {
+               /* Do this calculation with frames so that we can round
+                  to a frame boundary at the start rather than the end.
+               */
+               int64_t ff = frames (r);
+               
+               h = ff / (3600 * r);
+               ff -= h * 3600 * r;
+               m = ff / (60 * r);
+               ff -= m * 60 * r;
+               s = ff / r;
+               ff -= s * r;
+
+               f = static_cast<int> (ff);
+       }
+
+       template <typename T>
+       std::string timecode (T r) const {
+               int h;
+               int m;
+               int s;
+               int f;
+               split (r, h, m, s, f);
+
+               std::ostringstream o;
+               o.width (2);
+               o.fill ('0');
+               o << std::setw(2) << std::setfill('0') << h << ":"
+                 << std::setw(2) << std::setfill('0') << m << ":"
+                 << std::setw(2) << std::setfill('0') << s << ":"
+                 << std::setw(2) << std::setfill('0') << f;
+               return o.str ();
        }
 
 protected:
@@ -109,6 +144,10 @@ public:
                return *this;
        }
 
+       ContentTime operator- () const {
+               return ContentTime (-_t);
+       }
+
        ContentTime operator- (ContentTime const & o) const {
                return ContentTime (_t - o._t);
        }
@@ -118,14 +157,50 @@ public:
                return *this;
        }
 
+       /** Round up to the nearest sampling interval
+        *  at some sampling rate.
+        *  @param r Sampling rate.
+        */
+       ContentTime round_up (float r) {
+               int64_t const n = rint (HZ / r);
+               int64_t const a = _t + n - 1;
+               return ContentTime (a - (a % n));
+       }
+
        static ContentTime from_seconds (double s) {
                return ContentTime (s * HZ);
        }
 
        template <class T>
        static ContentTime from_frames (int64_t f, T r) {
+               assert (r > 0);
                return ContentTime (f * HZ / r);
        }
+
+       static ContentTime max () {
+               return ContentTime (INT64_MAX);
+       }
+};
+
+std::ostream& operator<< (std::ostream& s, ContentTime t);
+
+class ContentTimePeriod
+{
+public:
+       ContentTimePeriod () {}
+       ContentTimePeriod (ContentTime f, ContentTime t)
+               : from (f)
+               , to (t)
+       {}
+
+       ContentTime from;
+       ContentTime to;
+
+       ContentTimePeriod operator+ (ContentTime const & o) const {
+               return ContentTimePeriod (from + o, to + o);
+       }
+
+       bool overlaps (ContentTimePeriod const & o) const;
 };
 
 class DCPTime : public Time
@@ -168,6 +243,10 @@ public:
                return *this;
        }
 
+       DCPTime operator- () const {
+               return DCPTime (-_t);
+       }
+
        DCPTime operator- (DCPTime const & o) const {
                return DCPTime (_t - o._t);
        }
@@ -181,8 +260,8 @@ public:
         *  at some sampling rate.
         *  @param r Sampling rate.
         */
-       DCPTime round_up (int r) {
-               int64_t const n = HZ / r;
+       DCPTime round_up (float r) {
+               int64_t const n = rint (HZ / r);
                int64_t const a = _t + n - 1;
                return DCPTime (a - (a % n));
        }
@@ -197,6 +276,7 @@ public:
 
        template <class T>
        static DCPTime from_frames (int64_t f, T r) {
+               assert (r > 0);
                return DCPTime (f * HZ / r);
        }
 
@@ -210,5 +290,6 @@ public:
 };
 
 DCPTime min (DCPTime a, DCPTime b);
+std::ostream& operator<< (std::ostream& s, DCPTime t);
 
 #endif