fgms 0.11.8
The
FlightGear MultiPlayer Server
project
fg_util.cxx
Go to the documentation of this file.
1 #include <fg_util.hxx>
2 #include <stdio.h>
3 
4 //////////////////////////////////////////////////////////////////////
5 /**
6  * @brief convert a unix timestamp to a string
7  * representation of a date
8  */
9 string
11 (
12  time_t date
13 )
14 {
15  struct tm *tmr;
16  char buf[64];
17  tmr = localtime(&date);
18  sprintf (buf, "%02d.%02d.%04d %02d:%02d:%02d ",
19  tmr->tm_mday,
20  tmr->tm_mon+1,
21  tmr->tm_year+1900,
22  tmr->tm_hour,
23  tmr->tm_min,
24  tmr->tm_sec);
25  return (string)buf;
26 } // timestamp_to_datestr()
27 //////////////////////////////////////////////////////////////////////
28 
29 //////////////////////////////////////////////////////////////////////
30 /**
31  * @brief calculate the difference of a timestamp to now and convert
32  * the difference to a string representation of the form "3 days 2 hours"
33  */
34 string
36 (
37  time_t Date
38 )
39 {
40  time_t Diff = time (0) - Date;
41  return diff_to_days (Diff);
42 } // timestamp_to_days
43 //////////////////////////////////////////////////////////////////////
44 
45 //////////////////////////////////////////////////////////////////////
46 /**
47  * @brief convert a time duration expressed in seconds to a string
48  * representation of the form "3 days 2 hours"
49  */
50 string
52 (
53  time_t Date
54 )
55 {
56  time_t Diff = Date;
57  unsigned int temp = 0;
58  string Result;
59 
60  if (Diff > 31536000) // years
61  {
62  temp = Diff / 31536000;
63  Result += NumToStr (temp, 0) + " years";
64  return Result;
65  }
66  if (Diff > 86400) // days
67  {
68  temp = Diff / 86400;
69  Result += NumToStr (temp, 0) + " days";
70  return Result;
71  }
72  if (Diff > 3600) // hours
73  {
74  temp = Diff / 3600;
75  Result += NumToStr (temp, 0) + " hours";
76  return Result;
77  }
78  if (Diff > 60) // minutes
79  {
80  temp = Diff / 60;
81  Result += NumToStr (temp, 0) + " minutes";
82  return Result;
83  }
84  temp = Diff;
85  Result += NumToStr (temp, 0) + " seconds";
86  return Result;
87 
88 } // diff_to_days
89 //////////////////////////////////////////////////////////////////////
90 
91 //////////////////////////////////////////////////////////////////////
92 /**
93  * Convert a byte counter to a string representation of the form
94  * "3.7 Gib" (3.7 Gigibit).
95  * The units conform to IEC,
96  * see http://physics.nist.gov/cuu/Units/binary.html
97  */
98 string
100 (
101  double bytes
102 )
103 {
104  double ret_val;
105  string ret_str;
106 
107  if (bytes > 1099511627776.)
108  {
109  ret_val = (bytes / 1099511627776.);
110  ret_str = NumToStr (ret_val) + " TiB";
111  return ret_str;
112  }
113  else if (bytes > 1073741824.)
114  {
115  ret_val = (bytes / 1073741824.);
116  ret_str = NumToStr (ret_val) + " GiB";
117  return ret_str;
118  }
119  else if (bytes > 1048576)
120  {
121  ret_val = (bytes / 1048576);
122  ret_str = NumToStr (ret_val) + " MiB";
123  return ret_str;
124  }
125  else if (bytes > 1024)
126  {
127  ret_val = (bytes / 1024);
128  ret_str = NumToStr (ret_val) + " KiB";
129  return ret_str;
130  }
131  ret_str = NumToStr (bytes) + " b";
132  return ret_str;
133 } // byte_counter()
134 //////////////////////////////////////////////////////////////////////
135 
136 
string diff_to_days(time_t Date)
convert a time duration expressed in seconds to a string representation of the form "3 days 2 hours" ...
Definition: fg_util.cxx:52
string NumToStr(T n_Number, int n_Precision=2, int n_Base=10)
Convert a number to string.
Definition: fg_util.hxx:193
string timestamp_to_days(time_t Date)
calculate the difference of a timestamp to now and convert the difference to a string representation ...
Definition: fg_util.cxx:36
string byte_counter(double bytes)
Definition: fg_util.cxx:100
string timestamp_to_datestr(time_t date)
convert a unix timestamp to a string representation of a date
Definition: fg_util.cxx:11