Sierra Toolkit  Version of the Day
FormatTime.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #include <stk_util/environment/FormatTime.hpp>
10 
11 #include <sstream>
12 #include <iomanip>
13 #include <cmath>
14 #include <iostream>
15 
16 namespace stk_classic {
17 
18 std::string
19 formatTime(
20  double time,
21  TimeFormat time_format)
22 {
23  std::stringstream oss;
24 
25  if (time < 0.0) {
26  time = -time;
27  oss << "-";
28  }
29 
30  if ((time_format & TIMEFORMAT_STYLE_MASK) == TIMEFORMAT_SECONDS) {
31  if (time_format & TIMEFORMAT_MILLIS)
32  oss << std::fixed << std::setprecision(3) << time;
33  else
34  oss << std::fixed << std::setprecision(0) << time;
35  }
36  else if ((time_format & TIMEFORMAT_STYLE_MASK) == TIMEFORMAT_HMS) {
37  int int_time = int(time);
38 
39  if (time >= 3600.0)
40  oss << (int_time)/3600 << ':'
41  << std::setw(2) << std::setfill('0') << (int_time/60)%60 << ':'
42  << std::setw(2) << std::setfill('0') << int_time%60;
43 
44  else if (time >= 60.0)
45  oss << ((int) (time)/60)%60 << ':'
46  << std::setw(2) << std::setfill('0') << int_time%60;
47 
48 
49  else
50  oss << ((int) time)%60;
51 
52  if (time_format & TIMEFORMAT_MILLIS) {
53  int milliseconds = int(std::fmod(time, 1.0)*1000.0 + 0.5);
54 
55  oss << '.' << std::setw(3) << std::setfill('0') << milliseconds;
56  }
57  }
58  else
59  oss << time;
60 
61  return oss.str();
62 }
63 
64 } // namespace stk_classic
_setfill setfill(char fill)
Function setfill sets the fill character as a manipulator.
Definition: WriterManip.hpp:96
_setprecision setprecision(int precision)
Function setprecision sets the numeric precision as a manipulator.
Definition: WriterManip.hpp:70
_setw setw(int width)
Function setw sets the width for the next field as a manipulator.
Definition: WriterManip.hpp:44
Sierra Toolkit.