Compute differece between timestamp values in secoonds - time

I want to calculate the frame per second value by computing the number of images captured in 1 second. For this purpose, I want to use the Timestamps given by the camera.
I want to subtract the current Timestamp value from the initial Timestamp value to calculate the number of seconds elapsed.
An example of my Timestamp values looks like 788343977.
QUESTIONS: How can I subtract the timestamps values to calculate the time elapsed in seconds?

Since you get the timestamp t1 = 788343977 by running 3 seconds and you get the timestamp t2 = 1999854657 when running 9 seconds, a second is equivalent to almost (approximation errors) s = (t2-t1)/6 = 201918446. So your might be dealing with nanoseconds.
Try this out :
def t_elapsed(t, t_init):
if t < t_init :
raise ValueError("Given timestamp smaller than initial one")
else:
return (t-t_init)/float(1e9)

Related

SSAS Tabular - how to aggregate differently at month grain?

In my cube, I have several measures at the day grain that I'd like to sum at the day grain but average (or take latest) at the month grain or year grain.
Example:
We have a Fact table with Date and number of active subscribers in that day (aka PMC). This is snapshotted per day.
dt
SubscriberCnt
1/1/22
50
1/2/22
55
This works great at the day level. At the month level, we don't want to sum these two values (count = 105) because it doesn't make sense and not accurate.
when someone is looking at month grain, it should look like this - take the latest for the month. (we may change this to do an average instead, management is still deciding)
option 1 - Take latest
Month-Dt
Subscribers
Jan-2022
55
Feb-2022
-
option 2 - Take aveage
Month-Dt
Subscribers
Jan-2022
52
Feb-2022
-
I've not been able to find the right search terms for this but this seems like a common problem.
I added some sample data at the end of a month for testing:
dt
SubscriberCnt
12/30/21
46
12/31/21
48
This formula uses LASTNONBLANKVALUE, which sorts by the first column and provides the latest value that is not blank:
Monthly Subscriber Count = LASTNONBLANKVALUE( 'Table'[dt], SUM('Table'[SubscriberCnt]) )
If you do an AVERAGE, a simple AVERAGE formula will work. If you want an average just for the current month, then try this:
Current Subscriber Count =
VAR _EOM = CLOSINGBALANCEMONTH( SUM('Table'[SubscriberCnt]), DateDim[Date] )
RETURN IF(_EOM <> 0, _EOM, AVERAGE('Table'[SubscriberCnt]) )
But the total row will be misleading, so I would add this so the total row is the latest number:
Current Subscriber Count =
VAR _EOM = CLOSINGBALANCEMONTH( SUM('Table'[SubscriberCnt]), DateDim[Date] ) //Get the number on the last day of the month
VAR _TOT = NOT HASONEVALUE(DateDim[MonthNo]) // Check if this is a total row (more than one month value)
RETURN IF(_TOT, [Monthly Subscriber Count], // For total rows, use the latest nonblank value
IF(_EOM <> 0, _EOM, AVERAGE('Table'[SubscriberCnt]) ) // For month rows, use final day if available, else use the average
)

Caculating % Change Measure in Power BI

I have a table with 3 fields: Date, Name, and Index Value. I am trying to create a measure which will calculate the % change in Index Value over a filtered period per Name.
So if when I create a chart and adjust the time period (date period) the measure will adjust the calculation to find the % change in index value over that time period.
Here is a screenshot of some example data (which matches my dataset):
And here is a the data as text:
Date,Name,Index Value
30/09/2022,Company A,100
01/10/2022,Company A,101
02/10/2022,Company A,103
03/10/2022,Company A,101
04/10/2022,Company A,100
05/10/2022,Company A,101
06/10/2022,Company A,103
07/10/2022,Company A,101
08/10/2022,Company A,102.5
09/10/2022,Company A,103
10/10/2022,Company A,103.5
30/09/2022,Company B,104
01/10/2022,Company B,104.5
02/10/2022,Company B,105
03/10/2022,Company B,105.5
04/10/2022,Company B,106
05/10/2022,Company B,106.5
06/10/2022,Company B,107
07/10/2022,Company B,107.5
08/10/2022,Company B,108
09/10/2022,Company B,108.5
10/10/2022,Company B,109
So in this example the measure would calculate the % change for Company A and Company B over any time period, but for example it could be between 30/09/2022 to 08/10/2022
I have tried to calculate a measure - here's the code (although it's incomplete but hopefully will give you an idea of where I was trying (and failing) to go)
% Change = DIVIDE(CALCULATE(SUM(Returns[Index]),filter(ALLSELECTED(Returns),
Returns[Name] = earlier(Returns[Name]) && Returns[date] =
MAX(Returns[date] , ))))
So what I am trying to do here is divide the index value at the maximum date associated with each Name in the dataset by the index value associated with the same name at the minimum date. This will give in effect an 'end value' and a 'start value' per name and the % change is simply end_value/start_value - 1.

How to find difference between two timestamps in Talend HI

I am new to Talend I want to find the difference between the two timestamps.
I am having two columns start_time and end_time.
I want to make a table in destination that will show the difference in both the timestamps, specifically I want to show hours mins and seconds.
Also I want time in timestamp not in ling format, how can I achieve this
start_time- 2021-06-18 08:27:52.000000
end_time- 2021-06-18 08:29:59.000000
I tried-
creating a variable 'ms' of long type in tmap = TalendDate.diffDate(row181.start_time,row181.end_time,"mm")
for converting into hh:mm:ss
String.format("%02d:%02d:%02d.%d", (Var.ms / (1000 * 60 * 60)) % 24, (Var.ms / (1000 * 60)) % 60, (Var.ms / 1000) % 60, Var.ms % 1000)
if I make table as string I am getting this err-
column "call_duration" is of type bigint but expression is of type character varying
Above T-map expression returning zero also I have to use long in the destination column type, but I want date type
Pattern "MM" refers to months, not minutes. Use "mm" instead.
How could you return a date type for a difference between two dates ? The result is necessarily a number (long/double...) .
If you want your output with hours/mins/seconds, you should use diffDate with "ss" pattern to get a long representing the duration in seconds. Then you'll have to transform this to get hours and minutes (e.g 3700 s would give you 1 hour, 1 minute, 40 seconds) . You also have to determine what kind of output you want (one column for each, a string with the concatenation of hours/minutes/seconds...)
Example : with row1.diffDate being your diffdate in seconds in input of a tMap, you could separate in three different columns. Then you'll only have to concatenate all values in a string. if you want a string output with ":" separator.

BIRT report cross tabs: How to calculate and display durations of time?

I have a BIRT report that displays some statistics of calls to a certain line on certain days. Now I have to add a new measeure called "call handling time". The data is collected from a MySQL DB:
TIME_FORMAT(SEC_TO_TIME(some calculations on the duration of calls in seconds),'%i:%s') AS "CHT"
I fail to display the duration in my crosstab in a "mm:ss"-format even when not converting to String. I can display the seconds by not converting them to a time/string but that's not very human readable.
Also I am supposed to add a "grand total" which calculates the average over all days. No problem when using seconds but I have no idea how to do that in a time format.
Which data types/functoins/expressions/settings do I have to use in the query, Data Cube definition and the cross tab cell to make it work?
Time format is not a duration measure, it cannot be summarized or used for an average. A solution is to keep "seconds" as measure in the datacube to compute aggregations, and create a derived measure for display.
In your datacube, select this "seconds" measure and click "add" to create a derived measure. I would use BIRT math functions to build this expression:
BirtMath.round(measure["seconds"]/60)+":"+BirtMath.mod(measure["seconds"],60)
Here are some things to watch out for: seconds are displayed as single digit values (if <10). The "seconds" values this is based on is not an integer, so I needed another round() for the seconds as well, which resulted in seconds sometimes being "60".
So I had to introduce some more JavaScript conditions to display the correct formatting, including not displaying at all if "0:00".
For the "totals" column I used the summary total of the seconds value and did the exact same thing as below.
This is the actual script I ended up using:
if (measure["seconds"] > 0)
{
var seconds = BirtMath.round(BirtMath.mod(measure["seconds"],60));
var minutes = BirtMath.round(measure["seconds"]/60);
if(seconds == 60)
{
seconds = 0;
}
if (seconds < 10)
{
minutes + ":0" + seconds;
}
else
{
minutes + ":" + seconds;
}
}

Receive Unix Timestamp and convert time to milliseconds WP7

I receive a unix timestamp in my wp7 app and i want to convert it to milliseconds..
I make this:
time.Ticks / 10000;
Is this correct? this give the total time in milliseconds or only the milliseconds?
I want the total time in milliseconds..
my method to get the time is this:
void _ntpClient_TimeReceived(object sender, NtpClient.TimeReceivedEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
DateTime time = e.CurrentTime;
long milliseconds = time.Ticks / 10000;
});
}
Unix generally stores time as either seconds, or a struct timespec that contains both seconds and microseconds for further precision. When referring to dates, it is the number of seconds (or seconds and microseconds) elapsed since January 1, 1970.
However, these are never referred to as "ticks". "Ticks" generally refer to the Windows/.NET style time units - a "tick" is equal to 100 nanoseconds. When referring to dates, it is the number of hundred nanosecond units that have elapsed since January 1, 0001.
If you have an object with "ticks", then yes, simply dividing by 10000 will convert the units to milliseconds. Alternately, you can use a TimeSpan:
TimeSpan ts = new TimeSpan(ticks);
millis = ts.TotalMilliseconds;

Resources