1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
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); }
51 // For some reason isnan() isn't working on macosx any more.
52 // This is a quick fix.
53 #if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
57 inline bool isnan(double x) { return x != x; }
58 inline bool isnan(float x) { return x != x; }
59 #define SYNFIG_ISNAN_FIX 1
65 /* === U S I N G =========================================================== */
69 using namespace synfig;
71 #define tolower ::tolower
73 /* === M A C R O S ========================================================= */
75 /* === G L O B A L S ======================================================= */
77 /* === M E T H O D S ======================================================= */
79 Time::Time(const String &str_, float fps):
83 std::transform(str.begin(),str.end(),str.begin(),&tolower);
85 // Start/Begin Of Time
86 if(str=="sot" || str=="bot")
103 // Now try to read it in the letter-abreviated format
104 while(pos<str.size() && sscanf(String(str,pos).c_str(),"%f%n",&amount,&read))
107 if(pos>=str.size() || read==0)
109 // Throw up a warning if there are no units
110 // and the amount isn't zero. There is no need
111 // to warn about units if the value is zero
112 // it is the only case where units are irrelevant.
114 synfig::warning("Time(): No unit provided in time code, assuming SECONDS (\"%s\")",str.c_str());
137 synfig::warning("Time(): Individual frames referenced, but frame rate is unknown");
140 // try to read it in as a traditional time format
142 int hour,minute,second;
144 if(fps && sscanf(str.c_str(),"%d:%d:%d.%f",&hour,&minute,&second,&frame)==4)
146 value_=frame/fps+(hour*3600+minute*60+second);
150 if(sscanf(str.c_str(),"%d:%d:%d",&hour,&minute,&second)==3)
152 value_=hour*3600+minute*60+second;
156 synfig::warning("Time(): Bad time format");
161 synfig::warning("Time(): Unexpected character '%c' when parsing time string \"%s\"",str[pos],str.c_str());
170 Time::get_string(float fps, Time::Format format)const
175 return "SOT"; // Start Of Time
177 return "EOT"; // End Of Time
181 if(ceil(time.value_)-time.value_<epsilon_())
182 time.value_=ceil(time.value_);
186 hour=time/3600;time-=hour*3600;
187 minute=time/60;time-=minute*60;
189 if(format<=FORMAT_VIDEO)
192 second=time;time-=second;
197 frame=round_to_int(time*fps);
199 return strprintf("%02d:%02d:%02d.%02d",hour,minute,second,frame);
202 return strprintf("%02d:%02d:%02d",hour,minute,second);
207 if(format<=FORMAT_FULL || hour)
208 ret+=strprintf(format<=FORMAT_NOSPACES?"%dh":"%dh ",hour);
210 if(format<=FORMAT_FULL || hour || minute)
211 ret+=strprintf(format<=FORMAT_NOSPACES?"%dm":"%dm ",minute);
217 second=time;time-=second;
219 if(format<=FORMAT_FULL || second)
220 ret+=strprintf(format<=FORMAT_NOSPACES?"%ds":"%ds ",(int)second);
222 if(abs(frame-floor(frame)>=epsilon_()))
223 ret+=strprintf("%0.3ff",frame);
225 ret+=strprintf("%0.0ff",frame);
231 if(abs(second-floor(second))>=epsilon_())
232 ret+=strprintf("%0.8fs",second);
234 ret+=strprintf("%0.0fs",second);
241 Time::round(float fps)const
245 value_type time(*this);
249 if(abs(time-floor(time))<0.5)
250 return floor(time)/fps;
252 return ceil(time)/fps;
257 Time::is_valid()const
259 return !isnan(value_);