Get total time and create an average based on timestamps - math

Background: I want to use coldfusion to find the total time a process takes by taking two timestamps and then adding all of the total times to create an average.
Question: What is the best way to take two timestamps and find out the difference in time by minutes.
Example:
Time Stamp #1: 2015-05-08 15:44:00.000
Time Stamp #2: 2015-05-11 08:52:00.000
So the time between the above timestamps would be:
2 Days 6 hours 52 mins = 3,292 minutes
I want to run this conversion on a handful of timestamp's and take the total minutes and divide to get an average.
To add more information to my question. 1. Yes the values are coming from a DB MSSQL. 2. I am actually going to be using the individual time differences and showing and overall average. So in my for loop each line will have a value like 3,292 (converted to mins or hours or days) and at the end of the for loop I want to show an average of all the lines shown on the page. Let me know if I need to add any other information.

Assuming your query is sorted properly, something like this should work.
totalMinutes = 0;
for (i = 2; i <= yourQuery.recordcount; i++)
totalMinutes +=
DateDiff('n'
, yourQuery.timestampField[i-1]
,yourQuery.timestampField[i]);
avgMinutes = totalMinutes / (yourQuery.recordcount -1);

Use the dateDiff() function
diffInMinutes = dateDiff('n', date1, date2);

Related

What is the correct Smartsheet formula for retrieving the number of appointments from today plus 7

I have a table in Smartsheets that has bookings in it.
If the appointment is confirmed it changes the status to scheduled. It also has a date assigned to it. I'm trying to use countifs to see how many are scheduled for the next 7 days.
This is the formula i have.
=COUNTIFS([Shoot Setup]:[Shoot Setup], "Styleshoots", [Date start]:[Date
start], >=TODAY(7))
This does not give me the correct value.
If i dont have the 7 modifier then it gives me the correct value for items scheduled from today.
But i want to just see the total for today plus 6 days 7 days in total.
Any suggestions?
The formula you've posted will return the count of rows where:
Shoot Setup = "Styleshoots"
AND
Date start is greater than or equal to seven days from today
Try using this formula instead:
=COUNTIFS([Shoot Setup]:[Shoot Setup], "Styleshoots", [Date start]:[Date start], >=TODAY(), [Date start]:[Date start], <=TODAY(7))
This formula will return the count of rows where:
Shoot Setup = "Styleshoots"
AND
Date start is greater than or equal to Today
AND
Date start is less than or equal to 7 days from today

Is there a faster way to change and parse date time for comparison on python?

If I have 2 sets of date time data. The time interval of the first set is 1 hour, and the second set is 4 hour. This is based on candlestick data, for instance, 4 hour candle open time = 05:00, close time = 09:00. I need to find the corresponding candle on 1 hour, to match with the one on 4 hour. If the candles are based on close time, I just need to find the date time 09:00 for both 1 hour and 4 hour, simple and easy.
But since the candles are based on open time, if I need the same candle, I have to look for 05:00 on 4 hour, and the corresponding (candle or time) is 08:00 on 1 hour. Basically I need to convert them to close time for matching like this:
from dateutil.relativedelta import *
if 1H+relativedelta(hour=+1) = 4H+relativedelta(hour=+4):
The above is what I researched online, but this method is very slow for looping the data. My friend used PHP and it could be done fast. Does anyone know a faster way to do it on Python? Thanks.
I can't see how relativedelta is useful. timedelta is more natural. It's going to be hard to be much faster than this:
import datetime
oneHourStart = datetime.datetime(2018,1,16,8,0,0)
fourHourStart = datetime.datetime(2018,1,16,5,0,0)
oneHourDelta = datetime.timedelta(hours=1)
fourHourDelta = datetime.timedelta(hours=4)
if oneHourStart+oneHourDelta == fourHourStart+fourHourDelta:
print("Match")
Output:
Match

Fetch Data At every 2 Minutes SQL Lite

Please can anyone help me in fetching data from my SQL Lite Table at every 2 minutes between my start time and stop time
I have two columns Data , TimeStamp and I am filtering between two timestamp and it is working fine but what I am trying to do is to result my data at every 2 minutes interval For example my start time is 2016-12-15 10:00:00 and stop time is 2016-12-15 10:10:00 the result should be 2016-12-15 10:00:00,2016-12-15 10:02:00,2016-12-15 10:04:00 ....
Add, to your where clause, an expression that looks for 2 minute boundaries:
strftime("%s", TimeStamp) % 120 = 0
This assumes you have data on exact, 2-minute boundaries. It will ignore data between those points.
strftime("%s", TimeStamp) converts your time stamp string into a single number representing the number of seconds since Jan 1st, 1970. The % 120 does modulo arithmetic resulting in 0 every 120 seconds. If you want minute boundaries, use 60. If you want hourly, use 3600.
What's more interesting -- and I've used this -- is to take all the data between boundaries and average them together:
SELECT CAST(strftime("%s", TimeStamp) / 120 AS INTEGER) * 120 as stamp, AVG(Data)
FROM table
WHERE TimeStamp >= '2016-12-15 10:00:00' AND
TimeStamp < '2016-12-15 10:10:00'
GROUP BY stamp;
This averages all data with timestamps in the same 2-minute "bin". The second date comparison is < rather than <= because then the last bin would only average one sample whereas the other bins would be averages of multiple values. You could also add MAX(Data) and MIN(Data) columns, if you want to know how much the data changed within each bin.

Using R to subset overlapping daily sensor data

I have a data set (3.2 million rows) in R which consists of pairs of time (milliseconds) and volts. The sensor that gathers the data only runs during the day so the time is actually the milliseconds since start-up that day.
For example, if the sensor runs 12 hours per day, then the maximum possible time value for one day is 43,200,000 ms (12h * 60m * 60s * 1000ms).
The data is continually added to a single file, which means there are many overlapping time values:
X: [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5...] // example if range was 1-5 for one day
Y: [voltage readings at each point in time...]
I would like to separate each "run" into unique data frames so that I could clearly see individual days. Currently when I plot the entire data set it is incredibly muddy because in fact all of the days are being shown in the single plot. Thanks for any help.
If your data.frame df has columns X and Y, you can use diff to find every time X goes down (meaning a new day, it sounds like):
df$Day = cumsum(c(1, diff(df$X) < 0))
Day1 = df[df$Day==1,]
plot(Day1$X, Day1$Y)

ASP - How to calculate total hour and minutes within time range

I need to prepare a yearly management report to show the total overtime (OT) work hours and minutes of all staff within time range and month.
The time format in ms sql 2000 database is as follows:
Each record will contains the FROM date & time and TO date & time
The report layout is as follows:
I had no idea how to divide and calculate the total hours & minutes within the time range as each OT records will overlap several time range and date.
Please help. Thanks.
Joe
The SQL DateDiff function can be used to compute the number of minutes, i.e.
declare #fromDT datetime
declare #toDT datetime
set #fromDT = '10/22/2011 18:30'
set #toDT = '10/22/2011 22:45'
select #fromDT,#toDT,DATEDIFF(mi,#fromDt,#toDt),
ltrim(str(DATEDIFF(mi,#fromDt,#toDt)/60))+':'+
ltrim(str(DATEDIFF(mi,#fromDt,#toDt)%60)) as HoursMin
Returns
StartTime End Time Mins HoursMin
2011-10-22 18:30:00.000 2011-10-22 22:45:00.000 255 4:15

Resources