Does moment duration() and humanize() handle small decimals? - momentjs

Why does this, which I expect to output something like '18 mins'...
const shouldBe18Mins = moment.duration(0.00003408923868091461, 'years').humanize()
console.log(`Should be 18 mins: ${shouldBe18Mins}`)
... instead output a few seconds?
By my arithmetic, 0.00003408923868091461 * 365 * 24 * 60 = 17.918 mins.

Related

How to use or detect the character bits in hex value?

I take a hex or dec value and want to get its specific bits to write them on a display. For dec value, i can seperate bits one by one and write them with the code below. However, the code is for numbers, not for characters like A,B,C... in a hex value. Therefore, can someone help me about it? Even if i can detect the bit of a character, it will be enough because i can write that bits as a character
meas_value = 1100 // it works fine but for example meas_value = A010 it's not gonna work
digits[4] = (meas_value / 10000);
digits[3] = ((meas_value - (digits[4] * 10000)) / 1000);
digits[2] = ((meas_value - digits[4] * 10000
- digits[3] * 1000) / 100);
digits[1] = ((meas_value - digits[4] * 10000
- digits[3] * 1000 - digits[2] * 100)/10);
digits[0] = (meas_value - digits[4] * 10000
- digits[3] * 1000 - digits[2] * 100 - digits[1]*10);

Time Function in Progress 4GL

I would like to insert 2 shift timings to the code.
(First Shift)
Shift starts from 8:00:00:000 to 19:59:59:999
(Second Shift)
Shift Starts from 20:00:00:000 to next day 7:59:59:999
I need to get the above exact data to be added to the below code.
Please help.
Below is the code:
IF TIME < (20 * 60 * 60) THEN DO:
ASSIGN StartDDT = dt_tm2dec(DATE(TODAY), 0)
EndDDT = dt_tm2dec(DATE(TODAY),19 * 60 * 60 + 59 * 60 + 59).
END.
ELSE DO:
ASSIGN StartDDT = dt_tm2dec(DATE(TODAY),20 * 60 * 60).
EndDDT = dt_tm2dec(DATE(TODAY + 1),07 * 60 * 60 + 59 * 60 + 59).
END.
The conditions you specified can be written as:
if ( time >= ( 8 * 60 * 60 )) and ( time < ( 20 * 60 * 60 )) then
do:
message "first shift".
end.
else
do:
message "second shift".
end.
You can use the DATETIME datatype to get millisecond accuracy on your times. Build the shift times with the DATETIME function, then compare your time to them. The NOW function gives you the current time down to the millisecond:
DEFINE VARIABLE dtShift1 AS DATETIME NO-UNDO.
DEFINE VARIABLE dtShift2 AS DATETIME NO-UNDO.
ASSIGN
dtShift1 = DATETIME(TODAY, (8 * 60 * 60 * 1000))
dtShift2 = DATETIME(TODAY, (20 * 60 * 60 * 1000)).
IF NOW >= dtShift1 AND NOW < dtShift2 THEN
MESSAGE "First shift" VIEW-AS ALERT-BOX INFORMATION.
ELSE
MESSAGE "Second shift" VIEW-AS ALERT-BOX INFORMATION.
You can also check the shift time for the following day by adding a day to TODAY:
dtShift1 = DATETIME(TODAY + 1, (8 * 60 * 60 * 1000))
And if you need to handle different time zones, use the DATETIME-TZ datatype.

Converting angle of circle to time in minutes

I have been trying to develop a clock control with a help of a circle. So if the circle is at 360 degree its 3'O clock, if its 90 degree then it will be 12'o clock. I can't figure out what formula can be used to find the time in hh:mm format if I have the angle of the circle.
eg.
9'o clock is 180 degree.
3'o clock is 360 degree
12'o clock is 90 degree and so on.
There's probably a more concise way to do this, but here's what you need to do.
Calculate the decimal value of the time, using 3 - (1/30) * (angle mod 360)
If the result is less than zero, add 12.
Take the integer portion to use as the hours, replacing a value of zero with 12.
Take the remainder (the decimal portion of the calculation from step 1) and convert to minutes by multiplying by 60.
Format your hours and minutes, padding with leading zeros if less than 10, and combine them with the colon.
An example implementation in java:
public static String convertAngleToTimeString(float angle) {
String time = "";
float decimalValue = 3.0f - (1.0f/30.0f) * (angle % 360);
if (decimalValue < 0)
decimalValue += 12.0f;
int hours = (int)decimalValue;
if (hours == 0)
hours = 12;
time += (hours < 10 ? "0" + hours: hours) + ":";
int minutes = (int)(decimalValue * 60) % 60;
time += minutes < 10 ? "0" + minutes: minutes;
return time;
}
You could check out these formula's on Wikipedia, they might help: http://en.wikipedia.org/wiki/Clock_angle_problem
But other than that, you could do something simple like, degrees/360 * 12. + offset.
For example, if we set that 360 degrees is 12 o'clock, and you had 90 degrees. You would get. 90/360 * 12 = 3. So that would be 3'o clock. Then you could add your offset, which in your case is 3 so it would be 6 o'clock.
First assume 12 o'clock is at 0 degrees. This makes sense because the degrees just increases when going around the clock once, and so does the hours (there's no wrap-around), so maybe we can just multiply the hours by a constant.
To have it reach 360 again at 12 o'clock, we need to multiply the hours by 360/12 = 30.
So now 3 o'clock is at 90 degrees and 12 o'clock is at 0.
Subtract 90 and we have 3 o'clock at 0 and 12 o'clock at -90.
Then make it negative and we have 3 o'clock at 0 and 12 o'clock at 90, as required.
There we go, now you just need to put it all together.
hrs=date +%H #H >>
min=date +%M #min >>
dim=echo "scale=2; $hrs * 60 + $min" | bc #day in minutes >>
dis=echo "scale=2; $dim * 60 + $sec" | bc #day in seconds >>
did=echo "scale=2; $dis / 240" | bc #day in degree >>
The Planet revolution is One degree per 240 seconds.
I work to make a sumerian-time-sys .
One sumerian hour is equivant with two current HRS or 30 revolution-degree. search for sexagesimal systems.
Have fun!

Split down a number in seconds to days, hours, minutes, and seconds?

I've heard that it's possible to accomplish this using the modulus % operator present in most programming languages. The real question is, how? I'm unfamiliar with how modulus works, so I've had difficulties using it in the past. Given the present time here in seconds since 1970, 1307758473.484, how can I calculate how many years that is, days that is, hours that is, and minutes that is using modulus?
I'm essentially looking to format it like this: "5 years, 10 days, 12 hours, 7 minutes, and 18.56 seconds". How would I do this? I'm really interested in learning the logic behind this and not interested in a simple drop-in solution.
When you do integer division, you get quotient and remainder. For example,
5 divided by 3 is quotient 1 with remainder 2.
In programming languages, this is usually expressed as:
5 / 3 # => 1
5 % 3 # => 2
The conversion you want is just a repeatation of this. It's easier to to start from the lower unit and go higher on.
First, you have
1307758473.484 seconds
Since 60 seconds is 1 minute, and
1307758473.484 / 60 = 21795974 (intended to be integer division)
1307758473.484 % 60 = 33.484,
it is the same as
21795974 minutes 33.484 seconds
Since 60 minutes is 1 hour, and
21795974 / 60 = 363266
21795974 % 60 = 14
it is further the same as
363266 hours 14 minutes 33.484 seconds
Now, there is a little bit of difficulty. Most days are 24 hours. When there is a leap second, it is not. If you ignore leap seconds and assume 1 day is 24 hours, then, by doing the calculation,
363266 / 24 = 15136
363266 % 24 = 2
it is further the same as
15136 days 2 hours 14 minutes 33.484 seconds.
Similarly, Most years are 365 days. When there is a leap day (year), it is not. If you ignore leap days and assume that 1 year is 365 days, then by doing the calculation,
15136 / 365 = 41
15136 % 365 = 171
it is further the same as
41 years 171 days 2 hours 14 minutes 33.483 seconds
Modulus returns the remainder when performing integer division.
I think its easiest to understand how to use Mod by working backwards through a problem first.
Lets start simple with hours, minutes and seconds - 1 hour, 10 minutes and 30 seconds to be precise.
First, you have 30 seconds. This is easy - it's just 30. No brainer.
Now add minutes - to determine minutes as seconds you multiply them times 60. Thus 10 minutes and 30 seconds = 630 seconds.
Now we see how mod works - because if you divide 630 by 60 you get 10.5 but if you ignore the fraction (whole integer division) you get 10. The remainder is the seconds.
So if you MOD 630 by 60 you get 30 - the remainder left over when dividing 630 by 30.
So to determine minutes and seconds, divide by 60 for the minutes, and mod by 60 for the seconds.
Now add an hour. One hour = 60 minutes and 60 minutes is 60*60 seconds so 1 hour = 3600 seconds. 3600 + 600 + 30 = 4230 seconds.
4230 / 3600 (1 hour) = 1 - so we have one hour
4230 % (mod) 3600 = 630 - grab this and now we process for minutes.
So if you flesh this out further and add a day - 1 day = 24 hours = 24*3600 = 86400
86400+3600+600+30 = 90630
90630 / 86400 = 1 -> 1 day
90630 % 86400 = 4230 -> seconds left over
4230 / 3600 = 1 -> 1 hour
and repeat the above logic.
Hope that helps clear it up - you keep repeating that iteration further and you can do weeks and years, but months are special since they're irregular, and so are leap years.
Whenever converting from a small base unit (seconds) to a series of larger units (minutes/hours/days/years/decades/centuries/millennia) you can use the modulo (%) operator to keep track of the remaining base units as you extract each large unit.
It is an elegant/simple way to keep a sort of running total in base units. Start extracting BaseUnits with the largest unit you want and work your way back down until you get to the original BaseUnit.
This only works when the extracted unit is nonzero. If it's zero then you have extracted no base units at all and don't need the modulo operator.
It is important to remember that the result of the modulo operation will always be in the original base unit. That can get confusing.
Let's restate 1 million seconds as larger time units. Let 1 year = 31,536,000 seconds and no leap years or other calendar adjustments.
#include <cstdio>
#define SEC2CENT 3153600000
#define SEC2DEC 315360000
#define SEC2YR 31536000
#define SEC2MONTH 2592000
#define SEC2WEEK 604800
#define SEC2DAY 86400
#define SEC2HOUR 3600
#define SEC2MIN 60
main()
{
unsigned int sec = 1000000; //I am 1 million seconds old or...
unsigned int centuries = sec / SEC2CENT;
if (centuries) sec = sec % SEC2CENT; //if nonzero update sec
unsigned int decades = sec / SEC2DEC;
if (decades) sec = sec % SEC2DEC; //the purpose of modulo for units is this running total of base units
unsigned int years = sec / SEC2YR;
if (years) sec = sec % SEC2YR;
unsigned int months = sec / SEC2MONTH;
if (months) sec = sec % SEC2MONTH;
unsigned int weeks = sec / SEC2WEEK;
if (weeks) sec = sec % SEC2WEEK;
unsigned int days = sec / SEC2DAY;
if (days) sec = sec % SEC2DAY;
unsigned int hours = sec / SEC2HOUR;
if (hours) sec = sec % SEC2HOUR;
unsigned int minutes = sec / SEC2MIN;
if (minutes) sec = sec % SEC2MIN;
unsigned int seconds = sec; //seconds should now be less than 60 because of minutes
printf("I am now exactly %u centuries, %u decades, %u years, %u months, %u weeks, %u days, %u hours, %u minutes, %u seconds old and that is very old indeed.", centuries, decades, years, months, weeks, days, hours, minutes, seconds);
}

Convert decimal representing time to hour, minutes, seconds

I have a decimal. The range of this decimal is between 0 and 23.999999. This decimal represents a time. For example, if the decimal is 0.25, then the time it represents is 12:15 AM. If the decimal is 23.50, the time it represents is 11:30 PM.
I have three variables:
- Hours
- Minutes
- Seconds
Using this decimal, how do I fill in the Hours, Minutes, and Seconds values?
Well, here's an answer in C#, but it's generally the same idea in most languages:
int hours = (int)hoursDecimal;
decimal minutesDecimal = ((hoursDecimal - hours) * 60);
int minutes = (int)minutesDecimal;
int seconds = (int)((minutesDecimal - minutes) * 60);
The hours should be pretty easy.
There are 60 minutes in 1 hour, so get the decimal part, multiply it by 60, and take the integer. Take the decimal again, multiply it again by 60, and you have your seconds.
For example, let's take the number 20.38490
We know it's hour 20, or 8 PM.
This leaves us with the number .38490
Multiplying with 60, we get 23.094 minutes.
Multiplying .094 with 60, we get 5 seconds.
you can use the floor function to strip off the hours and leave the minuites and seconds as a fraction of an hour. Then you can use the floor function again to strip off the minuites as a fraction of an hour. you are then left with the seconds ( as fractions of an hour )
below a simple example to print hours and mins sunrise is in fractional hours since midnight
printf( "sunrise %ld:%ld, \n",
(long)floor( sunrise ),
(long)(floor( sunrise * 60 ) - 60 * floor( sunrise )) );
Whatever language you use, you can do this using the math functions: MOD and FLOOR/TRUNC
Let "dec" be the decimal variable
trunc(mod(dec, 1)) => hours
trunc(mod(dec * 60, 60)) => minutes
trunc(mod(dec * 3600, 60)) => seconds
In C#, you can truncate a decimal to int using just explicit casting, e.g.
int seconds = (int) ((dec * 3600) % 60)

Resources