Receive Unix Timestamp and convert time to milliseconds WP7 - windows-phone-7

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;

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
)

Time duration in Google Data Studio

I have some data I collect regarding length of time that's stored in HH:MM format. The data is in relation to sleep patterns (i.e. sleep duration, time fell asleep, etc...).
I am trying to import the data in Google Data Studio (DS) as a numeric variable, but it appears as text. I can see in DS there is a duration (seconds) numeric format, how can I convert a text variable into a numeric one?
It would be easier to convert the fields in a Google Sheet, but I need them as HH:MM for other calculations.
Try this:
0) Create a new Calculated Field
1) Seconds
Use a formula to convert the Time values to a single value in Seconds, where HH:MM:SS represents the field name:
( CAST(REGEXP_EXTRACT(HH:MM:SS, "^(\\d{2})") AS NUMBER ) * 60 * 60 ) + ( CAST(REGEXP_EXTRACT(HH:MM:SS, ":(\\d{2}):") AS NUMBER ) * 60 ) + CAST(REGEXP_EXTRACT(HH:MM:SS, "(\\d{2})$") AS NUMBER )
2) Change Field Type
- Numeric > Duration (sec.)
Credit to Google support community
You can use the TODATE or MINUTE and SECOND function into a calculated field to extract minutes and second from a date. However don't expect to display minutes and second datapoint on a line chart in Data Studio, timeserie charts only support hour-level data at a minimum.

Compute differece between timestamp values in secoonds

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)

Ruby on Rails 3 convert time to DateTime in specific timezone

I have column which stores only time. I want to convert to it into datetime and then to a specific timezone. When I do it on console using only the attribute, it shows correct conversion value. But when I use it in query the query shows the current time in UTC. Suppose I have time "05:15" stored in Central time. Now when I want to fetch records for a interval of 30 minutes plus and minus of this time, I do following,
day = Time.now
# departure_time_from _db below is the name of column in table which store time
departure = departure_time_from _db.change(:day => date.day, :month => date.month, :year => date.year)
departure = departure.in_time_zone("Central Time (US & Canada)")
When I execute above code on console and see the value, it shows correct converted value. But when I use this variable in below query, current time gets used instead of the departure time.
Model.where("column_name > ? and "column_name < ?, departure - 30.minutes, departure + 30.minutes)
The time used in above query should be the time I want i.e Central time zone, but in query output, it shows UTC being used.
Please let me know what i am missing.
It shouldn't matter what time zone the query uses in the where clause as long as it's the same time representation. The time you're seeing in your query output from Rails is most likely the UTC equivalent of the ruby DateTime object being passed to the where statement. So if departure is 12:00 noon (UTC -6) in your Ruby code then the time shown in the SQL query will be 18:00, which corresponds to noon CST.

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;
}
}

Resources