1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
10 ** Copyright (c) 2008 Gerco Ballintijn
11 ** Copyright (c) 2008 Carlos López
13 ** This package is free software; you can redistribute it and/or
14 ** modify it under the terms of the GNU General Public License as
15 ** published by the Free Software Foundation; either version 2 of
16 ** the License, or (at your option) any later version.
18 ** This package is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ** General Public License for more details.
24 /* ========================================================================= */
26 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
49 extern "C" { int _isnan(double x); }
54 // For some reason isnan() isn't working on macosx any more.
55 // This is a quick fix.
56 #if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
60 inline bool isnan(double x) { return x != x; }
61 inline bool isnan(float x) { return x != x; }
62 #define SYNFIG_ISNAN_FIX 1
68 /* === U S I N G =========================================================== */
72 using namespace synfig;
74 #define tolower ::tolower
76 /* === M A C R O S ========================================================= */
78 /* === G L O B A L S ======================================================= */
80 /* === M E T H O D S ======================================================= */
82 Time::Time(const String &str_, float fps):
86 std::transform(str.begin(),str.end(),str.begin(),&tolower);
88 // Start/Begin Of Time
89 if(str=="sot" || str=="bot")
106 // Now try to read it in the letter-abbreviated format
107 while(pos<str.size() && sscanf(String(str,pos).c_str(),"%f%n",&amount,&read))
110 if(pos>=str.size() || read==0)
112 // Throw up a warning if there are no units
113 // and the amount isn't zero. There is no need
114 // to warn about units if the value is zero
115 // it is the only case where units are irrelevant.
117 synfig::warning("Time(): No unit provided in time code, assuming SECONDS (\"%s\")",str.c_str());
140 synfig::warning("Time(): Individual frames referenced, but frame rate is unknown");
143 // try to read it in as a traditional time format
145 int hour,minute,second;
147 if(fps && sscanf(str.c_str(),"%d:%d:%d.%f",&hour,&minute,&second,&frame)==4)
149 value_=frame/fps+(hour*3600+minute*60+second);
153 if(sscanf(str.c_str(),"%d:%d:%d",&hour,&minute,&second)==3)
155 value_=hour*3600+minute*60+second;
159 synfig::warning("Time(): Bad time format");
164 synfig::warning("Time(): Unexpected character '%c' when parsing time string \"%s\"",str[pos],str.c_str());
173 Time::get_string(float fps, Time::Format format)const
178 return "SOT"; // Start Of Time
180 return "EOT"; // End Of Time
184 if(ceil(time.value_)-time.value_<epsilon_())
185 time.value_=ceil(time.value_);
188 if(!(format<=FORMAT_FRAMES))
190 hour=time/3600;time-=hour*3600;
191 minute=time/60;time-=minute*60;
193 // <= is redefined, so this means "is the FORMAT_VIDEO bit set in the format?"
194 if(format<=FORMAT_VIDEO)
197 second=time;time-=second;
202 frame=round_to_int(time*fps);
204 return strprintf("%02d:%02d:%02d.%02d",hour,minute,second,frame);
207 return strprintf("%02d:%02d:%02d",hour,minute,second);
210 if (format <= FORMAT_FRAMES)
213 return strprintf("%df", round_to_int(time * fps));
215 return strprintf("%ds", round_to_int(time * 1));
219 bool started = false;
221 if(format<=FORMAT_FULL || hour)
223 ret+=strprintf("%dh",hour);
227 if(format<=FORMAT_FULL || minute)
229 if (!(format<=FORMAT_NOSPACES) && started)
232 ret += strprintf("%dm", minute);
240 second=time;time-=second;
243 if(format<=FORMAT_FULL || second)
245 if (!(format<=FORMAT_NOSPACES) && started)
248 ret += strprintf("%ds", (int)second);
252 if(format<=FORMAT_FULL || abs(frame) > epsilon_() || !started)
254 if (!(format<=FORMAT_NOSPACES) && started)
257 if(abs(frame-floor(frame) >= epsilon_()))
258 ret += strprintf("%0.3ff", frame);
260 ret += strprintf("%0.0ff", frame);
267 if(format<=FORMAT_FULL || second || !started)
269 if (!(format<=FORMAT_NOSPACES) && started)
272 if(abs(second-floor(second))>=epsilon_())
274 String seconds(strprintf("%0.8f",second));
276 // skip trailing zeros
278 String::reverse_iterator i = seconds.rbegin();
279 for ( ; (*i) == '0'; i++)
282 // if we removed too many, go back one place, leaving one zero
283 if (*i < '0' || *i > '9') count--;
285 ret += seconds.substr(0, seconds.size()-count) + "s";
288 ret+=strprintf("%0.0fs",second);
296 Time::round(float fps)const
300 value_type time(*this);
304 if(abs(time-floor(time))<0.5)
305 return floor(time)/fps;
307 return ceil(time)/fps;
314 return get_string().c_str();
320 Time::is_valid()const
322 return !isnan(value_);