1 /* === S Y N F I 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-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
33 #include <ETL/stringf>
46 extern "C" { int _isnan(double x); }
52 #define isnan __isnanf
59 /* === U S I N G =========================================================== */
63 using namespace synfig;
65 #define tolower ::tolower
67 /* === M A C R O S ========================================================= */
69 /* === G L O B A L S ======================================================= */
71 /* === M E T H O D S ======================================================= */
73 Time::Time(const String &str_, float fps):
77 std::transform(str.begin(),str.end(),str.begin(),&tolower);
79 // Start/Begin Of Time
80 if(str=="SOT" || str=="BOT")
97 // Now try to read it in the letter-abreviated format
98 while(pos<str.size() && sscanf(String(str,pos).c_str(),"%f%n",&amount,&read))
101 if(pos>=str.size() || read==0)
103 // Throw up a warning if there are no units
104 // and the amount isn't zero. There is no need
105 // to warn about units if the value is zero
106 // it is the only case where units are irrelevant.
108 synfig::warning("Time(): No unit provided in time code, assuming SECONDS (\"%s\")",str.c_str());
131 synfig::warning("Time(): Individual frames referenced, but frame rate is unknown");
134 // try to read it in as a traditional time format
136 int hour,minute,second;
138 if(fps && sscanf(str.c_str(),"%d:%d:%d.%f",&hour,&minute,&second,&frame)==4)
140 value_=frame/fps+(hour*3600+minute*60+second);
144 if(sscanf(str.c_str(),"%d:%d:%d",&hour,&minute,&second)==3)
146 value_=hour*3600+minute*60+second;
150 synfig::warning("Time(): Bad time format");
155 synfig::warning("Time(): Unexpected character '%c' when parsing time string \"%s\"",str[pos],str.c_str());
164 Time::get_string(float fps, Time::Format format)const
169 return "SOT"; // Start Of Time
171 return "EOT"; // End Of Time
175 if(ceil(time.value_)-time.value_<epsilon_())
176 time.value_=ceil(time.value_);
180 hour=time/3600;time-=hour*3600;
181 minute=time/60;time-=minute*60;
183 if(format<=FORMAT_VIDEO)
186 second=time;time-=second;
191 frame=round_to_int(time*fps);
193 return strprintf("%02d:%02d:%02d.%02d",hour,minute,second,frame);
196 return strprintf("%02d:%02d:%02d",hour,minute,second);
201 if(format<=FORMAT_FULL || hour)
202 ret+=strprintf(format<=FORMAT_NOSPACES?"%dh":"%dh ",hour);
204 if(format<=FORMAT_FULL || hour || minute)
205 ret+=strprintf(format<=FORMAT_NOSPACES?"%dm":"%dm ",minute);
211 second=time;time-=second;
213 if(format<=FORMAT_FULL || second)
214 ret+=strprintf(format<=FORMAT_NOSPACES?"%ds":"%ds ",(int)second);
216 if(abs(frame-floor(frame)>=epsilon_()))
217 ret+=strprintf("%0.3ff",frame);
219 ret+=strprintf("%0.0ff",frame);
225 if(abs(second-floor(second))>=epsilon_())
226 ret+=strprintf("%0.8fs",second);
228 ret+=strprintf("%0.0fs",second);
235 Time::round(float fps)const
239 value_type time(*this);
243 if(abs(time-floor(time))<0.5)
244 return floor(time)/fps;
246 return ceil(time)/fps;
251 Time::is_valid()const
253 return !isnan(value_);