[med-svn] [SCM] aghermann branch, master, updated. 9c95ea59282c4fc6ef7eb192072500f9d0659fc3

Andrei Zavada johnhommer at gmail.com
Tue Jan 8 00:24:51 UTC 2013


The following commit has been merged in the master branch:
commit 51249108e0bee116897006c59f4a74920f5895a9
Author: Andrei Zavada <johnhommer at gmail.com>
Date:   Sun Jan 6 03:00:52 2013 +0200

    new functions agh::str::dhms(double seconds)

diff --git a/src/common/libcommon.cc b/src/common/libcommon.cc
index b7a1bed..4cc8d26 100644
--- a/src/common/libcommon.cc
+++ b/src/common/libcommon.cc
@@ -11,6 +11,7 @@
  */
 
 
+#include <cstdio>
 #include <cstring>
 #include <string>
 #include <list>
@@ -142,6 +143,71 @@ tilda2homedir( const string& v)
 
 
 
+string
+agh::str::
+dhms( double seconds, int dd)
+{
+	bool	positive = seconds >= 0.;
+	if ( not positive )
+		seconds = -seconds;
+
+	int	s = (int)seconds % 60,
+		m = (int)seconds/60 % 60,
+		h = (int)seconds/60/60 % (60*60),
+		d = (int)seconds/60/60/24 % (60*60*24);
+	double	f = seconds - floor(seconds);
+
+	DEF_UNIQUE_CHARP (f_);
+	if ( dd == 0 )
+		f_ = strdup( "");
+	else
+		ASPRINTF( &f_, ".%0*d", dd, (int)(f*pow(10, dd)));
+
+	DEF_UNIQUE_CHARP (_);
+
+	if ( d > 0 )
+		ASPRINTF( &_, "%dd %dh %dm %d%ss", d, h, m, s, f_);
+	else if ( h > 0 )
+		ASPRINTF( &_, "%dh %dm %d%ss", h, m, s, f_);
+	else if ( m > 0 )
+		ASPRINTF( &_, "%dm %d%ss", m, s, f_);
+	else
+		ASPRINTF( &_, "%d%ss", s, f_);
+
+	string ret (_);
+	return ret;
+}
+
+string
+agh::str::
+dhms_colon( double seconds, int dd)
+{
+	bool	positive = seconds >= 0.;
+	if ( not positive )
+		seconds = -seconds;
+
+	int	s = (int)seconds % 60,
+		m = (int)seconds/60 % 60,
+		h = (int)seconds/60/60 % (60*60),
+		d = (int)seconds/60/60/24 % (60*60*24);
+	double	f = seconds - floor(seconds);
+
+	DEF_UNIQUE_CHARP (f_);
+	if ( dd == 0 )
+		f_ = strdup( "");
+	else
+		ASPRINTF( &f_, ".%0*d", dd, (int)(f*pow(10, dd)));
+
+	DEF_UNIQUE_CHARP (_);
+
+	ASPRINTF( &_, "%dd %02d:%02d:%02d%ss", d, h, m, s, f_);
+
+	string ret (_);
+	return ret;
+}
+
+
+
 
 
 
diff --git a/src/common/string.hh b/src/common/string.hh
index a5d58e1..7b604ae 100644
--- a/src/common/string.hh
+++ b/src/common/string.hh
@@ -56,6 +56,9 @@ string  homedir2tilda( const string& v);
 string& tilda2homedir( string& inplace);
 string  tilda2homedir( const string& v);
 
+string dhms( double seconds, int decimal_digits = 0) __attribute__ ((pure));
+string dhms_colon( double seconds, int decimal_digits = 0) __attribute__ ((pure));
+
 }
 }
 

-- 
Sleep experiment manager



More information about the debian-med-commit mailing list