Raised This Month: $51 Target: $400
 12% 

Converting a single digit string (seconds) into (hours:minutes:seconds)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HappyTreeElf
New Member
Join Date: Jul 2020
Old 07-09-2020 , 22:23   Converting a single digit string (seconds) into (hours:minutes:seconds)
Reply With Quote #1

===
I messed the title up. It's clearly not a single digit string. It should read "number that's a string" I apologize
===

I've been struggling with this for hours now and I've officially ran out of ideas

I have a database query:

Code:
dbi_result(resultMaps, "time", qryTime, 31)
The "time" is an entry for "seconds". I believe this is returing a string and storing it in the qryTime variable.

Underneath that I have a line (cut it down for the purposes of this this thread) that sends it to the client in-game with the format:

Code:
"%s", qryTime
In game this works perfectly fine, but it only tracks it in seconds. I would like it to print hours:minutes:seconds in the game.

I tried to create a function for this. The aim of this function is to pass "qryTime" into the parameter so it looks like this

Code:
"%s", convertTime(qryTime)
Here is my function

Code:
public convertTime(runTimeInSeconds[32]) {	
	new convertedTime[32]
	new Float:finishTime = str_to_float(runTimeInSeconds[31])	
	new nHours = (floatround(Float:finishTime) / 3600) % 24
	new nMinutes = (floatround(Float:finishTime) / 60) % 60 
	new nSeconds = floatround(Float:finishTime) % 60	
	convertedTime[31] = ("%i:%i:%i", nHours, nMinutes, nSeconds)
	return convertedTime
}
I'm clearly doing something wrong here I can't even get my plug-in to compile now. It's saying "nMinutes" and "nHours" are not being used. Even if it does compile I have a suspicion it's not even going to work the way I want.

Last edited by HappyTreeElf; 07-09-2020 at 22:26.
HappyTreeElf is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-09-2020 , 23:07   Re: Converting a single digit string (seconds) into (hours:minutes:seconds)
Reply With Quote #2

It looks like you should look into studying how to use strings in this language because everything you've done with the string variables in that code is incorrect. One thing that isn't usually mentioned is that you should never return a string from a function in an AMX Mod X plugin. "output" string should be passed as an argument with an accompanying length input (strings are always passed by reference into functions). Take a look at other [working] plugins that use strings that are passed into and out of functions.

For the math part, you can use the following code to get hours, minutes, and seconds. Make sure that you you convert your string (which contains a number of seconds) with str_to_num() so iSeconds = str_to_num(szMyString).

Quote:
Originally Posted by fysiks View Post
Use the following code for calculating hours, minutes, and seconds:
Code:
iSeconds = 12345 // your time source in seconds
iHours = iSeconds / 3600
iMinutes = iSeconds % 3600 / 60
iSeconds = iSeconds % 60
A quick example of how I might do something like this (so you're not just randomly searching around though I still recommend learning the basics of strings):

Code:
new szSeconds[32], szSecondsFormatted[14]

FormatTimeFromSeconds(str_to_num(szSeconds), szSecondsFormatted, charsmax(szSecondsFormatted))

// szSecondsFormatted now contains the formatted time string in the "hh:mm:ss" format

stock FormatTimeFromSeconds(iSeconds, szSecondsFormatted[], len)
{
	iHours = iSeconds / 3600
	iMinutes = iSeconds % 3600 / 60
	iSeconds = iSeconds % 60
	
	format(szSecondsFormatted, len, "%02d:%02d:%02d", iHours, iMinutes, iSeconds)
}
__________________

Last edited by fysiks; 07-09-2020 at 23:19.
fysiks is offline
HappyTreeElf
New Member
Join Date: Jul 2020
Old 07-10-2020 , 10:30   Re: Converting a single digit string (seconds) into (hours:minutes:seconds)
Reply With Quote #3

Thank you very much
HappyTreeElf is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:13.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode