I need to get a timestamp in integer seconds, that won't roll over.
Can be elapsed CPU seconds, or elapsed clock or epoch seconds.
'clock' gives a date/time vector in years ... seconds.
But I can't figure out how to convert this to integer seconds.
cputime returns elapsed integer seconds but "This number can overflow the internal representation and wrap around.".
What about round(3600 * 24 * now)?
According to the manual, now returns the number of days since the year 0, as a floating point number. Multiplying by 86400 should thus give seconds.
Usually it is better to use a fixed-point format for keeping track of time, but since you are only interested in integer seconds, it should not be too much of a problem. The time resolution of now due to floating point resolution can be found like this:
>> eps(now*86400)
ans =
7.6294e-06
Or almost 8 microseconds. This should be good enough for your use case. Since these are 64-bit floating point numbers, you should not have to worry about wrapping around within your lifetime.
One practical issue is that the number of seconds since the year 0 is too large to be printed as an integer on the Matlab prompt with standard settings. If that bothers you, you can do fprintf('%i\n', round(3600 * 24 * now)), or simply subtract some arbitrary number, e.g. to get the number of seconds since the year 2000 you could do
epoch = datenum(2000, 1, 1);
round(86400 * (now - epoch))
which currently prints 488406681.
Related
Unix time is useful for measuring time, whereas other formats are more useful for telling the time.
This is because (apart from time synchronization), it just ticks forward one second at a time.
It doesn't change when our clock for telling the time has an hour change, for example.
However, there does seem to be one exception. It ignores leap seconds, meaning when there is a leap second, it basically jumps back a second.
I'm wondering is there a similar format to Unix time that also includes leap seconds and has no special cases at all?
Nevermind, unix time has no exception for leap seconds.
I believe the explanation for unix time on Wikipedia is awful:
It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds
This is incorrect, it should be:
It is the number of clock / artificial Earth seconds that have elapsed since the Unix epoch, minus leap seconds
Or in simpler terms:
It is the numbers of measured seconds that have elapsed since the Unix epoch
Hope this solves anyone else's confusion.
Using a PostgreSQL database, what is the best way to store time, in hours, minutes and seconds. E.g. "40:21" as in 40 minutes and 21 seconds.
Example data:
20:21
1:20:02
12:20:02
40:21
time would be the obvious candidate to store time as you describe it. It enforces the range of daily time (00:00:00 to 24:00:00) and occupies 8 bytes.
interval allows arbitrary intervals, even negative ones, or even a mix of positive and negative ones like '1 month - 3 seconds' - doesn't fit your description well - and occupies 16 bytes. See:
How to get the number of days in a month?
To optimize storage size, make it an integer (4 bytes) signifying seconds. To convert time back and forth:
SELECT EXTRACT(epoch FROM time '18:55:28'); -- 68128 (int)
SELECT time '00:00:01' * 68128; -- '18:55:28' (time)
It sounds like you want to store a length of time, or interval. PostgreSQL has a special interval type to store a length of time, e.g.
SELECT interval'2 hours 3 minutes 20 seconds';
This can be added to a timestamp in order to form a new timestamp, or multiplied (so that (2 * interval'2 hours') = interval'4 hours'. The interval type seems to tailor-made for your use case.
I have two sets of time series data which are collected with different time intervals. One is measured every 15 minutes and the other every 1 minute.
The measured variables are oxygen concentration, oxygen saturation and time, all three of which are measured using the two different instruments which have the different time intervals (6 column arrays in total).
I have two times between which I want to find the index's of all the entries at 15 minute intervals in the time column that sit between them.
co=1;
for i = datenum('03/11/2014/10/00/00','dd/mm/yyyy/HH/MM/SS'):datenum('03/11/2014/00/15/00','dd/mm/yyyy/HH/MM/SS')-datenum('03/11/2014/00/00/00','dd/mm/yyyy/HH/MM/SS'):('03/11/2014/16/00/00','dd/mm/yyyy/HH/MM/SS');
u=find(xyl_time==i);
New_O2(co,1)=xyl_o2conc(u);
New_O2(co,2)=xyl_o2sat(u);
v=find(sg_time==i);
New_O2(co,3)=sg_o2conc(v);
New_O2(co,4)=sq_o2sat(v);
co=co+1;
end
however, this does not work. I have narrowed it down and its something to do with the time interval that I'm using. I want it at every 15 minutes, but when I produce the 15 minute interval and then datestr that number, it comes up with '12:15AM'. I think this is causing the problem, but have no idea how to produce just times alone i.e I just want 00:15 not 12:15 not 00:15 AM or PM. just spacings of 15 minutes for my for loop.
I am calculating the number of days between two dates(the 2 dates are in seconds). The following gives me the coorect result but it gives me a negative value.
For example -7.0. I am not sure why..
Also can I remove the decimal point and display?
set interval [expr { $start_date - $get_date }]
set days [expr { floor($interval / (60*60*24)) }]
puts "Start Date: $start_date <br>"
puts "Stop Date: $get_date <br>"
puts "Total number of dates betwen 2 days: $days"
You're subtracting the end (assuming that's what $get_date is) from the start. Think of it like numbers, where a later date is a bigger number - if you subtract a large number from a small number, you get a negative value, right?
So you probably just want to reverse the arithmetic:
set interval [expr { $get_date - $start_date }]
I've no idea about the decimal point part, I'm afraid... perhaps (based on this documentation) just:
set days [expr { int($interval / (60*60*24)) }]
EDIT: As noted in comments, some care is required when it comes to "days" between two events. Do you mean elapsed days, 24 hours per day, or "local" days? Are your input values in local time, UTC, or something else? Could you perhaps use a dedicated date/time library to handle this? (I have no idea whether tcl has such a thing, but I'd expect it to.)
You should think really carefully about exactly what behaviour you want in what situation, and write tests accordingly.
Not all days are 86400 seconds long, because of little things like daylight savings time. Because of this, you need to do some rounding in your calculation (and use floating point division):
set days [expr {round($interval / double(60*60*24))}]
You also usually subtract the start from the end when calculating the length of an interval, not the other way round. Gets the sign right.
I am trying to show the location coordinates in specific format like 42° 51’36.712032" N, 112° 25’ 45.069804" W (example of geographical coordinates). How can i implement this.
Thanks in advance.
From Wikipedia:
Each degree of longitude is sub-divided into 60 minutes, each of which is divided into 60 seconds. A longitude is thus specified in sexagesimal notation as 23° 27′ 30" E. For higher precision, the seconds are specified with a decimal fraction.
Thus, take the fractional part of your coordinate and multiply it by 3600 (60×60) to get the total number of seconds; to convert that to the minutes/seconds that you want, take the total seconds mod 60 to get the actual seconds, subtract that number from the fractional part you got earlier, and then divide the result by 60 to get the number of minutes.