How to check if 24 hours have passed in Swift - swift3

I have to implement in my app where after a user had saved his recording, I will have to determine if 24 hours have passed from the creation date of that recording. So far what I have at the moment is just to determine if current date is not equal to the creation date. I really appreciate anybody's help, thanks in advance.

You can use UserDefault to save the date upon creation of the record. The syntax will be
UserDefaults.standard.set(Date(), forKey:"creationTime")
Whenever you want to check the saved date, retrieve it in this way
if let date = UserDefaults.standard.object(forKey: "creationTime") as? Date {
if let diff = Calendar.current.dateComponents([.hour], from: date, to: Date()).hour, diff > 24 {
//do something
}
}

You can easily check if the time interval since date is greater or equal to a TTL of 24 hours.
let start = Date()
let timeToLive: TimeInterval = 60 * 60 * 24 // 60 seconds * 60 minutes * 24 hours
let isExpired = Date().timeIntervalSince(start) >= timeToLive

Related

How do I sum the available hours in a given period in PowerBI based on the following?

I have some data I am working with that has the number of minutes someone works in a given day
I would like to sum this amount based on the filter context being daily weekly monthly etc
`
Utilization_Rate =
Var Actual_Job_Length =
SUMX(
FILTER(
MATCHED_JOBID,
MATCHED_JOBID[ACTUAL_END]>MATCHED_JOBID[ACTUAL_START]),
DATEDIFF(
MATCHED_JOBID[ACTUAL_START],
MATCHED_JOBID[ACTUAL_END],
MINUTE))
VAR UTILIZATION =
DIVIDE( Actual_Job_Length, TIME AVAILABLE)
RETURN UTILIZATION
`
My issue is with this time available function. I want to be able to utilization based on the day, week start date, month, quarter, and year
I need to sum the available hours based on a 5 day work week. If a employee works more than 30 they must have 8 hour days (fulltime) if an employee works 30 or less then 6 hour days.
I do not know how to do the above and have function for different period lengths. If I need multiple measures that is fine too. Just don't know how to approach it
I have not figured out anything to do
I got further
PT-FT =
CALCULATE(IF( [Total_Job_Duration]>= VALUES('Calendar Lookup'[PT_hours]), VALUES('Calendar Lookup'[FT_Hours]), VALUES('Calendar Lookup'[PT_hours])), FILTER(Matched_JobID, Matched_JobID[Actual_Start].[Date]))
This works to some extent but I still have a problem with it not summing for the number of work days in a period
Result of the above code
I want it for example in this case to sum for the week as the other values have so I would see 40 instead of 8 and 30 instead of 6
Please use the below measure. Its working fine at my end:
Quantity_based_OnSlicer =
var Daily = SELECTEDVALUE('Product'[Date])
var Monthly = year(selectedvalue('Product'[Date])) & MONTH(SELECTEDVALUE('Product'[Date]))
var weekly = WEEKNUM(SELECTEDVALUE('Product'[Date]))
Return
SWITCH(SELECTEDVALUE('Ex5-Slicer'[Value]),
"Daily",CALCULATE(SUM('Product'[Quantity]),'Product'[Date] = Daily,ALLEXCEPT('Product','Product'[Date])),
"Monthly",CALCULATE(SUM('Product'[Quantity]),year('Product'[Date])&month('Product'[Date]) = Monthly,ALLEXCEPT('Product','Product'[Date])),
"Weekly",CALCULATE(SUM('Product'[Quantity]),WEEKNUM('Product'[Date]) = weekly,ALLEXCEPT('Product','Product'[Date])),
0
)

How to convert Unix date time into seconds in Power Query

If i have a column like this
How would i convert this into seconds?
In PowerQuery change the type of the column to Date/Time
Add a Custom Column with the formula
= ([business_duration] - #datetime(1970,1,1,0,0,0)) * 24 * 60 * 60
Change the type of the new column to Whole Number
You can use this function:
let
fun_DateTimeFromUnix = (DateTime as number) as datetime =>
let
Source = #datetime(1970,1,1,0,0,0) + #duration(0,0,0,DateTime)
in
Source
in
fun_DateTimeFromUnix
As shown:

Same Period Last Month

I'm struggling to get the same period of last month.
I want to compare the current month period, for instance, today is 16June2021 so I want to get the sales from May 1st to May 16th.
I'm using this formula but I get the whole month total:
Prev MTD = calculate(sum(Sales[Sales_Amount]),DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH))
Creating a table with DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH), I also get every day of last month.
Is it mandatory to use a Date Table? Already tried but the results came empty.
Is it something regarding my date format? From the import it comes as date/time format.
Thank you very much
Try with:
Prev MTD = calculate(sum(Sales[Sales_Amount]),filter(ALL(Sales[Sale_Date]),Sales[Sale_Date] >= DATE( YEAR(TODAY()), MONTH(TODAY())-1, 1) && Sales[Sale_Date]<=today()))
where order_date between '2022-08-01' and DATE_SUB(CURRENT_DATE(),32)
last month 31 days that's why i am calculated 32 days.
if last month 30 day than you need to calculate 31 days.

Get Local Time on Power BI Service

As you all might already know, TODAY() function returns UTC time when published to Power BI Service.
Our requirement is to return the local (EST) date. As a solution, we created a custom measure that adds UTC offset hours for EST in NOW() and returns the resultant date.
However, this does not handle daylight saving changes as offset changes during these periods.
What are the possible ways to handle this?
You can try something like this:
ESTnow=
VAR currentTime = NOW()
VAR timeYear = YEAR(currentTime)
VAR dstStart = DATE(timeYear, 03, 14 - MOD((1+FLOOR(timeYear*5/4,1)),7)) + TIME(2,0,0)
VAR dstEnd = DATE(timeYear, 11, 7 - MOD((1+FLOOR(timeYear*5/4,1)),7)) + TIME(2,0,0)
RETURN IF(currentTime >= dstStart && currentTime <= dstEnd, currentTime - TIME(4,0,0), currentTime - Time(5,0,0))
Daylight savings start on the second Sunday of March and end on the first Sunday of November.
A more flexible way to convert UTC date/time for any location (time zone) in the world, and taking Daylight Saving into account, would be to use a Time API service like the one at https://www.timeanddate.com/services/api/time-api.html

Difference between utc time zone and device timezone in swift

I am working on a chat app. I want to determine the time difference between UTC time zone and device current time zone. How to do that in swift3 ?
// Returns a Date initialized to the current date and time
let currentDate = Date()
// Returns the time zone currently used by the system
let timeZone = NSTimeZone.system
// Returns the difference in seconds between the receiver and Greenwich Mean Time at a given date.
let timeZoneOffset = timeZone.secondsFromGMT(for: currentDate) / 3600
print(timeZoneOffset, "hours offset for timezone", timeZone)
// example output for Denver (considers daylight saving time)
// -6 hours offset for timezone America/Denver (current)