#property(readonly) NSTimeInterval timestamp;
denotes the time when the event occurred in seconds since system startup. I want to compare the timestamps of two events but their differences can also be in milliseconds. So, is there any way to get the timestamp of an event in milliseconds. I know I can use the methods mentioned in this SO question but would that be appropriate to use for events ?
If you want milliseconds you can multiply by 1000.0 but you can compare without multiplying. Documentation of NSTimeInterval:
typedef double NSTimeInterval;
NSTimeInterval is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.
Related
I am trying to measure how many minutes have elapsed from the time I turn my code from a fixed time, lets say 8:30 am of that day. If I turn my code on at 12am it should see that 210 minutes have elapsed. Any suggestions would help greatly. Thank you
You can import the datetime module, which has a class with the same name, with method datetime.datetime.now().
This returns an object representing the time when it is called.
This object has the method replace(), which can be used to 'change' the time to 8:30, if you call it like so - replace(hour=8, minute=30).
You can then create another similar object but without replacing the time, then you can simply subtract the first from the second to get the elapsed time as a datetime object.
This will then have elapsed_time.seconds to give you the time change in seconds, which can be divided by 60 if you want for the time in minutes.
Example
import datetime
time_A = datetime.datetime.now()
time_A = time_A.replace(hour=8, minute=30)
time_B = datetime.datetime.now()
elapsed_time = time_B - time_A
print(elapsed_time.seconds, "seconds have passed since 8:30 this morning")
If you wanted this for a specific timezone, you can add or subtract the offset from your current timezone. So if you are for example, 5 hours ahead of CST, you can have it get the difference from 3:30 instead.
I used below simple expression for getting duration:
doc['endTime'].date.millisOfDay - doc['startTime'].date.millisOfDay
But the problem starts when, endTime crosses the startTime day.
Example: If startTime is 23:50 and endTime for the same is 00:12, we
crossed by midnight, which changes the date as well.
In that way I am getting absolutely wrong duration, except all the scenarios when both time lies with in the same day result is as expected.
Help on how exactly i can make this.
You should simply subtract the absolute milliseconds value since the epoch (instead of milliseconds since the start of the day):
doc['endTime'].date.millis - doc['startTime'].date.millis
I am trying to figure out what the value of t is ? Is it seconds or milliseconds ? The steady_clock reference does not mention the unit used.
auto t = std::chrono::steady_clock::now() / 1000;
auto p = t/1000;
I am thinking now() returns seconds and t is in milliseconds and p is in microseconds. Let me know if I am getting this right ?
It's std::chrono::time_point<std::chrono::steady_clock> (the documentation on CppReference is generally better quality).
Guessing your next question — to convert from that to seconds you would use time_since_epoch() (the documentation has an example of extracting a dimension-free number of seconds from it), or alternatively as (now - epoch) / 1_second
Unit of value returned by std::chrono::steady_clock::now() is not defined by standard (it is general value of type std::chrono::time_point).
The resolution of the std::chrono::time_point (it stores a value of type Duration indicating the time interval from the start of the Clock's epoch) is implementation dependent (platforms/compiler), and you shouldn't rely on it.
To get a desired unit, you can easily convert the time_point to a value in seconds, milliseconds, etc. by duration casting:
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
(time_since_epoch() returns a duration representing the amount of time between *this and the clock's epoch).
I have an NSTimer with a time interval set to 10 seconds with repeats.
I have a scenario where I'm starting the timer, and let's say that after 15 seconds I want to check the interval of the timer. I expect to get the value 5.
Is this kind of thing possible?
NSTimer has a property fireDate which contains an NSDate object representing the next absolute date the timer fires.
timer.fireDate.timeIntervalSinceDate(NSDate())
returns the difference from now to the next fire date as NSTimeInterval
I am trying to call liveReport service of kaltura but I m not sure how to specify time in the request filters. It asks time to be in int format but when i canver time in milli seconds I get long value, but API is expecting int value.
can anyone provide help in this?
If you are referring to livereports.getreport service then fromTime & toTime properties expect an integer timestamp in seconds. You can also provide a timestamp that is lower then 315360000 seconds and it will be treated relative to NOW, so -1440 will be converted to NOW - 1440
Seems like other report services are expecting an integer that is created using conversion of date format YYYYMMDD to integer, so for May 18th, 2015 you would send 20150318.