LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

how to convert time from second to hh:mm:ss using CVI

Solved!
Go to solution

how to convert time from second to hh:mm:ss using CVI?

 

Can anyone advice?

0 Kudos
Message 1 of 5
(5,175 Views)

Please be more specific: do you want to convert

  • A time in some standard format (e.g. number of seconds since midnight, January 1, 1900)
  • A interval in seconds in the corresponding h:m:s format (e.g. 63930 sec => 17:45:30)

In the first case functions in Time Related Functions class in the User Interface Library can be of help, specifically FormatDateTimeString

In the second case you'll have to develop your own function. I have a very simple one I can share if you're interested to.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 5
(5,171 Views)

hi

 

thanks

 

ya this is what i need

 

"A interval in seconds in the corresponding h:m:s format (e.g. 63930 sec => 17:45:30)" can share this ? 

0 Kudos
Message 3 of 5
(5,166 Views)
Solution
Accepted by topic author chris_chua

Here it is. As I told you, it is very simple:

 

//----------------------------------------------------------------------
// Function secToHMSstring ()
//----------------------------------------------------------------------
/// HIFN secToHMSstring ()
/// HIFN The function takes an amount of seconds and returns a string with
/// HIFN the corresponding value in H:M:S format
/// HIPAR sec/Value in seconds to convert
/// HIPAR verbose/If True use "hms" separators; if not, use ":" separator
/// HIPAR string/The output string. It is responsibility of the programmer
/// HIPAR string/that the string is large enough to keep the resulting text
/// OUT string
void secToHMSstring (int sec, int verbose, char *string)

{
	int     hh = 0, mm = 0, ss = 0;

	if (sec >= 3600) {
		hh = sec / 3600;
		sec -= hh * 3600;
	}
	if (sec >= 60)
		mm = sec / 60;
	ss = sec - mm * 60;

	strcpy (string, "");
	if (verbose) {
		if (hh > 0) sprintf (string, "%s%dh", string, hh);
		if (mm > 0) { if (strlen (string)) strcat (string, " "); sprintf (string, "%s%dm", string, mm); }
		if (ss > 0) { if (strlen (string)) strcat (string, " "); sprintf (string, "%s%ds", string, ss); }
	}
	else {
		if (hh > 0) sprintf (string, "%s%d:", string, hh);
		if (mm > 0) {
			if (strlen (string)) sprintf (string, "%s%02d:", string, mm); else sprintf (string, "%d:", mm);
		}
		else if (strlen (string))
			strcat (string, "00:");
		if (strlen (string)) sprintf (string, "%s%02d",  string, ss); else sprintf (string, "%d", ss);
	}

	return;
}

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 5
(5,163 Views)

Thanks alots.. 🙂 

0 Kudos
Message 5 of 5
(5,130 Views)