1 /* === S I N F G =========================================================== */
3 ** \brief Template File
5 ** $Id: time.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
32 #include <ETL/stringf>
45 extern "C" { int _isnan(double x); }
58 /* === U S I N G =========================================================== */
62 using namespace sinfg;
64 #define tolower ::tolower
66 /* === M A C R O S ========================================================= */
68 /* === G L O B A L S ======================================================= */
70 /* === M E T H O D S ======================================================= */
72 Time::Time(const String &str_, float fps):
76 std::transform(str.begin(),str.end(),str.begin(),&tolower);
78 // Start/Begin Of Time
79 if(str=="SOT" || str=="BOT")
96 // Now try to read it in the letter-abreviated format
97 while(pos<str.size() && sscanf(String(str,pos).c_str(),"%f%n",&amount,&read))
100 if(pos>=str.size() || read==0)
102 // Throw up a warning if there are no units
103 // and the amount isn't zero. There is no need
104 // to warn about units if the value is zero
105 // it is the only case where units are irrelevant.
107 sinfg::warning("Time(): No unit provided in time code, assuming SECONDS (\"%s\")",str.c_str());
130 sinfg::warning("Time(): Individual frames referenced, but frame rate is unknown");
133 // try to read it in as a traditional time format
135 int hour,minute,second;
137 if(fps && sscanf(str.c_str(),"%d:%d:%d.%f",&hour,&minute,&second,&frame)==4)
139 value_=frame/fps+(hour*3600+minute*60+second);
143 if(sscanf(str.c_str(),"%d:%d:%d",&hour,&minute,&second)==3)
145 value_=hour*3600+minute*60+second;
149 sinfg::warning("Time(): Bad time format");
154 sinfg::warning("Time(): Unexpected character '%c' when parsing time string \"%s\"",str[pos],str.c_str());
163 Time::get_string(float fps, Time::Format format)const
168 return "SOT"; // Start Of Time
170 return "EOT"; // End Of Time
174 if(ceil(time.value_)-time.value_<epsilon_())
175 time.value_=ceil(time.value_);
179 hour=time/3600;time-=hour*3600;
180 minute=time/60;time-=minute*60;
182 if(format<=FORMAT_VIDEO)
185 second=time;time-=second;
190 frame=round_to_int(time*fps);
192 return strprintf("%02d:%02d:%02d.%02d",hour,minute,second,frame);
195 return strprintf("%02d:%02d:%02d",hour,minute,second);
200 if(format<=FORMAT_FULL || hour)
201 ret+=strprintf(format<=FORMAT_NOSPACES?"%dh":"%dh ",hour);
203 if(format<=FORMAT_FULL || hour || minute)
204 ret+=strprintf(format<=FORMAT_NOSPACES?"%dm":"%dm ",minute);
210 second=time;time-=second;
212 if(format<=FORMAT_FULL || second)
213 ret+=strprintf(format<=FORMAT_NOSPACES?"%ds":"%ds ",(int)second);
215 if(abs(frame-floor(frame)>=epsilon_()))
216 ret+=strprintf("%0.3ff",frame);
218 ret+=strprintf("%0.0ff",frame);
224 if(abs(second-floor(second))>=epsilon_())
225 ret+=strprintf("%0.8fs",second);
227 ret+=strprintf("%0.0fs",second);
234 Time::round(float fps)const
238 value_type time(*this);
242 if(abs(time-floor(time))<0.5)
243 return floor(time)/fps;
245 return ceil(time)/fps;
250 Time::is_valid()const
252 return !isnan(value_);