Convert Milliseconds to Hours : Minutes : Seconds in Coldfusion? - coldfusion

I am getting a a field called duration_in_milliseconds from an API, is there a function or a way to convert that to hours:minutes:seconds somehow?
I've looked and can't find any solutions. Thanks for any help?

#craigster's answer is useful if you want just the number of hours or the minutes or the seconds represented by the milliseconds.
If you want all three, you need to do a bit more arithmetic.
For instance, 23 hrs 59 mins 55 seconds:
(23 * 60 * 60 * 1000)
+ (59 * 60 * 1000)
+ (55 * 1000)
= 86395000 milliseconds
To convert 86395000 back into HH:MM:SS you could do:
<cfscript>
hours = int(duration_in_milliseconds \ (60 * 60 * 1000));
mins = (duration_in_milliseconds \ (60 * 1000)) mod 60;
secs = (duration_in_milliseconds \ 1000) mod 60;
</cfscript>
<cfoutput>#hours# #mins# #secs#</cfoutput>

All you have to do is divide it.
duration_in_milliseconds / 1000 = Seconds
duration_in_milliseconds / 60000 = Minutes
(duration_in_milliseconds / 60000) / 60 = Hours
In Coldfusion Code it might look like this
If you want Hours use this line
<cfset Hours = (LSParseNumber(duration_in_milliseconds) / 60000) / 60>
If you want Minutes use this line
<cfset Minutes= LSParseNumber(duration_in_milliseconds) / 60000>
If you want Seconds use this line
<cfset Seconds= LSParseNumber(duration_in_milliseconds) / 1000>

You should start with this: createTimeStruct(), and modify it to support a mask of milliseconds as well.
Don't reinvent the wheel. Just tweak an existing wheel.

Related

PowerBI: how to get average of sum of multiple categories

For a report, I would like to have the average of a sum over 2 categories (date_type_project & ID).
In PowerBI, I would like to have 2 cards with 1 number. There'll be cards with the numbers:
data_type_project - non-standard, 30.44
data_type_project - standard, 9.47
The numbers are composed as follow: (29 + 33 + 20.25 + 39.5) / 4 = 30.44
and (3.5 + 25.5 + 9.75 + 17.5 + 17.25 + 14.5 + 1.5 + 1.25 + 4.5) / 9 = 9.47
How can I calculate this number in DAX?
You could try the following and use ISINSCOPE() function:
measure_hours =
-- will check, if 'Table'[ID] is in current filter context
var cond = ISINSCOPE('Table'[ID])
RETURN
IF(
cond,
SUM('Table'[hours]),
AVERAGE('Table'[hours])
)
You can create a measure and place it in place of hours
Measure = SUM([Hours])/COUNT([ID])
I've found a workaround that is answering my question: I've created a measure. Then, put it in a card visual and set the legend to 'data_type_project'. Thank you for the help! :)
measure =
AVERAGEX(
KEEPFILTERS(VALUES('Table'[ID])),
CALCULATE(SUM('Table'[hours]))
)

Unit testing Datetime values

I have a function that makes use of the current time (now). The Contract as a whole is a Crowdfunding token and the cost of tokens differ depending on the date and time that tokens are purchased.
How does one simulate different times when testing a Smart Contract? For instance, with regards to the code below, I would like to do unit testing to find out if the code for setting price is correct but I can't change the value of now.
Would it be a good solution to simply substitute the now keyword for another temporary testing variable, say now_sim and then manually changing now_sim during simulation?
if (now < (startTime + 1 days)) {
currentPrice = safeDiv(safeMul(price, 80), 100); // 20 % discount (x * 80 / 100)
}
else if (now < (startTime + 2 days)) {
currentPrice = safeDiv(safeMul(price, 90), 100); // 10 % discount (x * 90 / 100)
}
else if (now < (startTime + 12 days)) {
// 1 % reduction in the discounted rate from day 2 until day 12 (sliding scale per second)
// 8640000 is 60 x 60 x 24 x 100 (100 for 1%) (60 x 60 x 24 for seconds per day)
currentPrice = price - safeDiv(safeMul((startTime + 12 days) - now), price), 8640000);
}
else {
currentPrice = price;
}
If you use pyethereum for testing - which I highly recommend, it's lovely - you can directly alter the timestamp of the simulated block that is mining your transaction.
self.s = t.state()
self.s.block.timestamp = self.s.block.timestamp + 86400
self.s.mine(1)
some_val = your_contract.do_something(some_parameter)
self.assertEqual(some_val, whatever)
See a working example here (maybe a bit out-of-date): https://github.com/realitykeys/subjectivocracy/blob/master/contracts/test.py#L85

Sun Movement (Y/Z movement)

I'm wondering the best way to simulate a sunrise/sunset on the y and z axis. At the moment it does what I want but is moving too fast (say every 3 seconds it's already completed an entire sun path and is already moving in reverse back to the origin).
I know this has to do with seconds variable combined with sin and cos, as this function is called and moves the light's position every frame. Basically I want it to be linked to my game's timer of 50:
50 seconds it's sunrise
25 seconds it's noon
0 seconds it's sunset/game over
Currently I have:
lightPosition = Point_E3d(0,
std::abs(100 *std::cos(seconds)),
-100 * std::sin(seconds));
Wondering what's the correct code format to achieve this effect.
This is just simple trigonometry. The period (Time until the function repeats) of sine(x * n) and cosine(x * n) are both 2*pi / n. In this case, n = 1, so the period is 6.28, meaning one full day (and night) will last 6.28 seconds. If you want to increase the period, multiply your seconds argument by a number smaller than one. A little bit of algebra shows that:
period of sin(x * n) = 2*pi / n
period of sin(.1256 * x) = 2*pi / .1256 = 6.28 / 0.1256 = 50
Therefore, take sine and cosine of seconds * 0.1256, rather than just seconds.
lightPosition = Point_E3d(0,
std::abs(100 *std::cos(seconds * 0.1256 )),
-100 * std::sin(seconds * 0.1256));
Note that this is also including the night time. If you want just the 12 hour day time period to last 50 seconds, multiply by half of 0.1256, aka 0.0628.

Slider bar math help needed see inside

Hey all i stink at math here's what i want to do in QML:
I have a slider bar that I want the min left edge to be 30 and the max right edge to be 100
what math puts 30 on the left end but gives all the percentages in between 30-100 to reflect 0-100% on the screen?
Thanks,
Chris
Given a value between 30 and 100:
percent = 100 * (value - 30) / 70
Or more generally:
percent = 100 * (value - min) / (max - min)

Calculating proportion with negative float values

I'd like to know if there's any way in C++ to calculate a proportion involving possibily negative values in both vars and extremes.
My goal is to sync a float text input widget with fixed extremes ( eg the user can input any double value between A (min) and B (max) with A,B=any_constant_real_number ) with a slider who can only slide between 0 and 100 ( to simplify ).
If A and B are positive everything is trivial. as
val_slider = ((val_textin-A)*100)/(B-A)
but as A and B can be assumed real it looks to me the only possibility is to use several if/cases, or huge formulas involving a lot of abs() and checks over 0-divisions, whose are quite error prone and very cost intense compared to such an easy task.
Is there any faster and shorter way to achieve the same in c/c++/stl?
Pardon my bad english. Any hint? Thank you.
Your formula should work fine with negative values of A and B as well, as log as A < B.
Example, if you want the user to be able to enter values from -100 to 100, and map these to a slider which goes from 0 - 100, when the user enters -90 you get:
((-90 - A) * 100) / (B - A) = ((-90 - (-100)) * 100) / (100 - (-100))
= 10 * 100 / 200
= 5
An input value of 50 results in a slider value of:
((50 - A) * 100) / (B - A) = ((50 - (-100)) * 100) / (100 - (-100))
= 150 * 100 / 200
= 75
I don't know C++, but I do know Math, so try:
val_slider = 100 * ( val_textin - A ) / ( B - A )
Hey wait. That's exactly what you have. Test case..
A=-200, B=+200, val_texin = 100 (75% of bar, right?)
val_slider = 100 * ( 100 - -200 ) / ( 200 - - 200 )
= ( 300 ) / ( 400 ) * 100
= 75
See, you got it right. The only thing that COULD happen is B==A, but that can't be accounted for with math and requires a single IF. If they are equal, val_slider is exactly B (or A, as they are equal).