Sierra Toolkit Version of the Day
FormatTime.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include <stk_util/util/FormatTime.hpp>
00010 
00011 #include <sstream>
00012 #include <iomanip>
00013 #include <cmath>
00014 
00015 namespace stk {
00016 
00017 std::string
00018 formatTime(
00019   double        time,
00020   TimeFormat    time_format)
00021 {
00022   std::stringstream oss;
00023 
00024   if (time < 0.0) {
00025     time = -time;
00026     oss << "-";
00027   }
00028   
00029   if ((time_format & TIMEFORMAT_STYLE_MASK) == TIMEFORMAT_SECONDS) {
00030     if (time_format & TIMEFORMAT_MILLIS)
00031       oss << std::fixed << std::setprecision(3) << time;
00032     else
00033       oss << std::fixed << std::setprecision(0) << time;
00034   }
00035   else if ((time_format & TIMEFORMAT_STYLE_MASK) == TIMEFORMAT_HMS) {
00036     int int_time = int(time);
00037 
00038     if (time >= 3600.0)
00039       oss << (int_time)/3600 << ':'
00040              << std::setw(2) << std::setfill('0') << (int_time/60)%60 << ':'
00041              << std::setw(2) << std::setfill('0') << int_time%60;
00042 
00043     else if (time >= 60.0)
00044       oss << ((int) (time)/60)%60 << ':'
00045              << std::setw(2) << std::setfill('0') << int_time%60;
00046 
00047 
00048     else
00049       oss << ((int) time)%60;
00050 
00051     if (time_format & TIMEFORMAT_MILLIS) {
00052       int milliseconds = int(std::fmod(time, 1.0)*1000.0 + 0.5);
00053 
00054       oss << '.' << std::setw(3) << std::setfill('0') << milliseconds;
00055     }
00056   }
00057   else
00058     oss << time;
00059 
00060   return oss.str();
00061 }
00062 
00063 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends