Base-60 arithmetic on the *nix command-line - time

What's the easiest way to calculate with a mixture of time-amounts (base 60) and other amounts? I want to be able to ask questions like:
What's 2 hours 15 minutes and 25 seconds divided by 42.195? (answer given in hours minutes and seconds for example)
My current workflow for that kind of question is to convert all the time-amounts into seconds, do the maths, then convert back again, but there must be a decent tool to do that for me? Some argument to bc perhaps?

Use the units tool:
$ units "(2 hours + 15 minutes + 25 seconds) / marathon" "minutes/km; seconds/km"
3 minutes/km + 12.558415 seconds/km

Related

Gnuplot - incremented minutes to days

I have data CNV file, where the first column is "minutes from start."
Minutes;Temperature
0;15.5
60;15.8
120;15.6
180;16.1
....
I would like to plot this data with x-axis as time (DAYS), so that every 1440 minutes is 1 day, then comes day 2... etc. What is the best way to do this?
Simply divide the minutes by 60 and by 24 (or 1440). Then you will have days.
Note, column values, e.g. $1, are always taken as float, so you don't have to worry about gnuplot's integer division (which can lead to unexpected results if you don't know about it).
plot "YourFile.dat" u ($1/1440):2 with lines

Density of time events

I am working on an assignment where I am supposed to compute the density of an event. Let's say that a certain event happens 5 times within seconds, it would mean that it would have a higher density than if it were to happen 5 times within hours.
I have in my possession, the time at which the event happens.
I was first thinking about computing the elapsed time between each two successive events and then play with the average and mean of these values.
My problem is that I do not know how to accurately represent this notion of density through mathematics. Let's say that I have 5 events happening really close to each other, and then a long break, and then again 5 events happening really close to each other. I would like to be able to represent this as high density. How should I go about it?
In the last example, I understand that my mean won't be truly representative but that my standard deviation will show that. However, how could I have a single density value (let's say between 0 and 1) with which I could rank different events?
Thank you for your help!
I would try the harmonic mean, which represents the rate at which your events happen, by still giving you an averaged time value. It is defined by :
I think its behaviour is close to what you expect as it measures what you want, but not between 0 and 1 and with inverse tendencies (small values mean dense, large values mean sparse). Let us go through a few of your examples :
~5 events in an hour. Let us suppose for simplicity there is 10 minutes between each event. Then we have H = 6 /(6 * 1/10) = 10
~5 events in 10 minutes, then nothing until the end of the hour (50 minutes). Let us suppose all short intervals are 2.5 minutes, then H = 6 / (5/2.5 + 1/50) = 6 * 50 / 101 = 2.97
~5 events in 10 minutes, but this cycle restarts every half hour thus we have 20 minutes as the last interval instead of 50. Then we get H = 6 / (5/2.5 + 1/20) = 6 * 20 / 41 = 2.92
As you can see the effect of the longer and rarer values in a set is diminished by the fact that we use inverses, thus less weight to the "in between bursts" behaviour. Also you can compare behaviours with the same "burst density" but that do not happen at the same frequency, and you will get numbers that are close but whose ordering still reflects this difference.
For density to make sense you need to define 2 things:
the range where you look at it,
and the unit of time
After that you can say for example, that from 12:00 to 12:10 the density of the event was an average of 10/minute.
What makes sense in your case obviously depends on what your input data is. If your measurement lasts for 1 hour and you have millions of entries then probably seconds or milliseconds are better choice for unit. If you measure for a week and have a few entries then day is a better unit.

explaining/documenting an intricate mechanism [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm torn between two worlds, I have this very intuitive (but intricate) mechanism in a command line tool, and I'm wondering as to what extent I should explain this.
I can go the simple way, not explaining it at all and trust my users to figure it out themselves, but then some users might never discover this particular feature.
I can go the scary way and put a lot of mathematical notation into the help output and the man pages, but then users might think this is too complicated and they might develop an inexplicable fear towards my tool or this particular feature.
How can I address both experimental and, let's say, conservative users (the ones that don't go the extra mile when something isn't explained properly)?
Details:
The tool is about date and time arithmetic, in particular calculating durations between two dates and/or times, and formatting the results according to format specs.
My internal design uses a multiplication table like this:
- x d t dt
x x x x x
d x D x D
t x x T x
dt x D x S
where x is unknown (unparsable) input, d is a date, t is a time and dt is a datetime, D is a date duration (resolution is 1 day), T is a time duration (resolution is 1 second), and S is a time-stamp duration (resolution 1 second).
Now the result depends on the duration type and the format specifiers given, and I'm really lacking a succinct way of explaining this, so I do it by example:
'%d' will return the duration in days (like 12 days)
'%w' will return the duration in weeks (like 1 week)
'%w %d' will return the duration in weeks and days (like 1 week and 5 days)
...
'%S' will return the duration in seconds (e.g. 86464 seconds)
'%M' will return the duration in minutes (e.g. 1441 minutes)
'%H' will return the duration in hours (e.g. 24 hours)
'%H %M %S' will return the duration in hours, minutes and seconds (24h 1m 4s)
'%H %S' will return the duration in hours and seconds (24h 64s)
...
I mean I could probably work out what I mean with just these few examples given, but there's no formal explanation or anything in there.
For clarity:
The issue I'm trying to address is that you can combine any of the flags (seconds, hours, days, months, etc.) and the program will "intelligently" give you a result. Like %Y %d would give you a year and the number of days (in the range 0 to 365) whereas %Y %m %d would give you the days in the range 0 to 30 (because the rest is "captured" in the month).
Example: %Y %d gives 1 year 90 days whereas %Y %w %d gives 1 year 12 weeks 6 days
If you're looking to create help text within the tool itself, look at the help for the linux date command.
Alternatively, you could do something like this:
$ your_app --help
usage: your_app [OPTIONS] [FORMAT]
Returns the elapsed time between blah blah....
FORMAT:
// list formats here
OPTIONS:
--help Display this help text
--help-detailed Display more extensive help text
--help-examples Display example uses
If I were the user, I'd want --help to list all of the options as a reference, and I'd want the man pages to include as much detail as possible. I tend to use --help as a reminder and the man pages as the authoritative reference.
And no matter how well-written the text may be, a few concrete examples are always valuable.

How can I do time/hours arithmetic in Google Spreadsheet?

How do I do time/hour arithmetic in a Google spreadsheet?
I have a value that is time (e.g., 36:00:00) and I want to divide it by another time (e.g., 3:00:00) and get 12. If I divide just one by the other, I get 288:00:00 when what I want is 12 (or 12:00:00).
Note that using the hours() function doesn't work, because 36:00:00 becomes 12.
When the number being returned by your formula is being formatted as a time, and you want it formatted as a plain number, change the format of the cell to a plain number format: click the cell and then click Format, Number, Normal.
Time values in Google spreadsheet are represented as days and parts of days. For example, 36:00:00 is the formatted representation of the number 1.5 (a day and a half).
Suppose you divide 36:00:00 by 3:00:00, as in your example. Google Spreadsheet performs the calculation 1.5 divided by 0.125, which is 12. The result tells you that you have 12 3-hour intervals in a 36-hour time period. 12, of course, is not a time interval. It is a unitless quantity.
Going the other way, it is possible to format any number as a time. If you format 12 as a time, it's reasonable to expect that you will get 288:00:00. 12 days contain 288 hours.
Google Sheets now have a duration formatting option. Select: Format -> Number -> Duration.
Example of calculating time:
work-start work-stop lunchbreak effective time
07:30:00 17:00:00 1.5 8 [=((A2-A1)*24)-A3]
If you subtract one time value from another the result you get will represent the fraction of 24 hours, so if you multiply the result with 24 you get the value represented in hours.
In other words: the operation is mutiply, but the meaning is to change the format of the number (from days to hours).
You can use the function TIME(h,m,s) of google spreadsheet. If you want to add times to each other (or other arithmetic operations), you can specify either a cell, or a call to TIME, for each input of the formula.
For example:
B3 = 10:45
C3 = 20 (minutes)
D3 = 15 (minutes)
E3 = 8 (hours)
F3 = B3+time(E3,C3+D3,0) equals 19:20
I had a similar issue and i just fixed it for now
format each of the cell to time
format the total cell (sum of all the time) to Duration
I used the TO_PURE_NUMBER() function and it worked.
So much simpler: look at this
B2: 23:00
C2: 1:37
D2: = C2 - B2 + ( B2 > C2 )
Why it works, time is a fraction of a day, the comparison B2>C2
returns True (1) or False (0), if true 1 day (24 hours) is added.
http://www.excelforum.com/excel-general/471757-calculating-time-difference-over-midnight.html
if you have duration in h:mm, the actual value stored in that cell is the time converted to a real number, divided by 24 hours per day.
ex: 6:45 or 6 hours 45 minutes is 6.75 hours 6.75 hours / 24 = 0.28125 (in other words 6hrs45minutes is 28.125% of a day). If you use a column to convert your durations into actual numbers (in example, converting 6:45 into 0.28125) then you can do you multiplication or division and get the correct answer.
In the case you want to format it within a formula (for example, if you are concatenating strings and values), the aforementioned format option of Google is not available, but you can use the TEXT formula:
=TEXT(B1-C1,"HH:MM:SS")
Therefore, for the questioned example, with concatenation:
="The number of " & TEXT(B1,"HH") & " hour slots in " & TEXT(C1,"HH") _
& " is " & TEXT(C1/B1,"HH")
Cheers
In an fresh spreadsheet with 36:00:00 entered in A1 and 3:00:00 entered in B1 then:
=A1/B1
say in C1 returns 12.
Type the values in single cells, because google spreadsheet cant handle duration formats at all, in any way shape or form. Or you have to learn to make scripts and graduate as a chopper pilot. that is also a option.

How to get the number of years, months, days, hours and mins of a certain number of seconds?

Given an arbitrary number of seconds, how can I get the number of years, months, days, hours and mins?
The algorithm should first compute the maximum number of years, then the number of months and so on...
What is an efficient way to do this?
It's mostly down to plain division. As you may know...
A minute has 60 seconds:
number_of_minutes := floor(number_of_seconds / 60)
An hour has 60 minutes:
number_of_hours := floor(number_of_minutes / 60) or
number_of_hours := floor(number_of_seconds / (60 * 60))
A day has 24 hours (at least usually... see below.)
A month has anything between 28 to 31 days.
A year has 365 or 366 days, or 365.2425 days on average.
The last two I mentioned may require you to think more about the stated problem. Either you define an "average" month, which then allows you to say "x seconds equal y average months"; or you don't convert your seconds to months at all.
(Thinking about it, if you were talking to an astronomer or alike, they would probably tell you that a day is not always exactly 24 hours, due to the occasional leap second.)

Resources