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);
}
Related
suppose that data are stored on 4.7 Mbytes CDs that weight 50 grams each.suppose that an airplane carries 10^4 kg of these CDs at a speed of 1500 km/h over a distance of 5000 km.what is data transmission rate in bits per second of this system?
I would not call this 'data-transmission', but:
Amount of CD's = 10^4 / 0.05 = 2e5
Amount of bits = 4.7e6 * 8 * amount of CD's = 7.52e12
Seconds to carry data = 5000e3 / (1500/3.6) = 12000 seconds
(7.52 * 10^12)/ 12000) = 0.6Gbits/s
This should be simple enough but the maths for this eludes me. I am looking to express this in C++ but some psuedo code will happily do, or just the maths really.
The function will be given a number of a container and it will return the number of items in that container. The number of items is based on their number and halves at certain number height.
The first halving is at number 43,200 and then every time after it is the gap number of containers between the previous halving plus 43,200
It may sounds confusing, it will look like the following.
1 to 43200 = 512
43201 to 86400 = 256
86401 to 129600 = 128
129601 to 172800 = 64
172801 to 216000 = 32
216001 to 259200 = 16
and so
So if a number up to 43,200 is given the result is 512, the number 130,000 will return 64. The value can be less than 1 taking up several decimal places.
global halvingInterval = 43200
global startingInventory = 512
def boxInventory(boxNumber):
currentInventory = startingInventory
while(boxNumber > halvingInterval):
currentInventory = currentInventory/2
boxNumber -= halvingInterval
return currentInventory
This code will take the box number. It will keep subtracting the halving interval until you get to the right inventory area, and then return the inventory when it is done.
N = (noitems + 1) / 43200;
L2 = log(512) / log(2);
answer = exp( log(2) * (1 + L2 - N) );
I am very bad at math, but what is the mathematical equation (in its simplest possible form) to convert 3605 seconds to hours, minutes and seconds?
3605 / 60 / 60 = 1,0013888888 hours
Am I on the right track?
time.hours = seconds / 3600;
time.minutes = (seconds - 3600 * time.hours) / 60;
time.seconds = seconds - 3600 * time.hours - 60 * time.minutes;
This doesn't work, but it should, shouldn't it?
Yes, you are on the right track. Here's a breakdown.
3605 secs
60 sec in a minute 3605/60 = 60.083 minutes
60 minutes in an hour 60.083 = 1.001 hour
24 hours in a day 1.001/24 = .0417 of a day
Let t be the total number of seconds
h = int(t / 3600)
m = int((t - 3600 * h) / 60)
s = t - 3600 * h - 60 * m
See code here: http://ideone.com/OzLF8g
1 minute = 60 seconds
1 hour = 60 minutes = 60*60 seconds
So,
if
x = 3605
a = number of hours
b = number of minutes
c = number of seconds
then
a = ⌊ x / (60*60) ⌋
b = ⌊ (x - (a*60*60)) / 60 ⌋
c = x - ((a*60*60) + (b*60))
⌊⌋ means quotient
hours = seconds // 3600
minutes = (seconds // 60) % 60
sec = seconds % 60
try it
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!
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)