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/environment/FormatTime.hpp>
00010 
00011 #include <sstream>
00012 #include <iomanip>
00013 #include <cmath>
00014 #include <iostream>
00015 
00016 namespace stk {
00017 
00018 std::string
00019 formatTime(
00020   double        time,
00021   TimeFormat    time_format)
00022 {
00023   std::stringstream oss;
00024 
00025   if (time < 0.0) {
00026     time = -time;
00027     oss << "-";
00028   }
00029   
00030   if ((time_format & TIMEFORMAT_STYLE_MASK) == TIMEFORMAT_SECONDS) {
00031     if (time_format & TIMEFORMAT_MILLIS)
00032       oss << std::fixed << std::setprecision(3) << time;
00033     else
00034       oss << std::fixed << std::setprecision(0) << time;
00035   }
00036   else if ((time_format & TIMEFORMAT_STYLE_MASK) == TIMEFORMAT_HMS) {
00037     int int_time = int(time);
00038 
00039     if (time >= 3600.0)
00040       oss << (int_time)/3600 << ':'
00041              << std::setw(2) << std::setfill('0') << (int_time/60)%60 << ':'
00042              << std::setw(2) << std::setfill('0') << int_time%60;
00043 
00044     else if (time >= 60.0)
00045       oss << ((int) (time)/60)%60 << ':'
00046              << std::setw(2) << std::setfill('0') << int_time%60;
00047 
00048 
00049     else
00050       oss << ((int) time)%60;
00051 
00052     if (time_format & TIMEFORMAT_MILLIS) {
00053       int milliseconds = int(std::fmod(time, 1.0)*1000.0 + 0.5);
00054 
00055       oss << '.' << std::setw(3) << std::setfill('0') << milliseconds;
00056     }
00057   }
00058   else
00059     oss << time;
00060 
00061   return oss.str();
00062 }
00063 
00064 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines