Converting seconds obtained with CFAbsoluteTimeGetCurrent() into date

The function CFAbsoluteTimeGetCurrent() provides the total seconds passed since Jan 1 2001.
I would like to convert this value (which look like something like this: 6.45799833464193E+8) into a date format, or into something like “19/6/2021 14:32:14”).

Any help is appreciated.

(date manipulation os not my preferred topic …)

L.

use framework "Foundation"
set totalSeconds to current application's CFAbsoluteTimeGetCurrent()

The very old CFAbsoluteTimeGetCurrent() function returns a CFTimeInterval, which is toll free bridged to an NSTimeInterval.
Which means you can create an NSDate class object expecting an NSTimeInterval, from the bridged CFTimeInterval, which can then be converted to an AppleScript Date object.
Which will then allow you to access the various parts of the AppleScript Date, like minutes, hours, weekday, month, year, etc.


use framework "Foundation"

-- Elapsed seconds since 00:00:00 UTC Jan 1 2001
set elapsedSeconds to current application's CFAbsoluteTimeGetCurrent()

-- NSDate class object created from elapsed seconds
set NSDateReference to current application's NSDate's   dateWithTimeIntervalSinceReferenceDate:elapsedSeconds

-- AppleScript date object created from NSDate object
set ASDateReference to NSDateReference as date

But you don’t really need to work with the Core Foundation function, you can get the exact same results with just the NSDate class, like this.


-- Elapsed seconds since 00:00:00 UTC Jan 1 2001
set elapsedSeconds to current application's NSDate's timeIntervalSinceReferenceDate()

-- NSDate class object created from elapsed seconds
set NSDateReference to current application's NSDate's dateWithTimeIntervalSinceReferenceDate:elapsedSeconds

-- AppleScript date object created from NSDate object
set ASDateReference to NSDateReference as date

But either way gives the same results., as NSDate’s timeIntervalSinceReferenceDate() returns the same NSTimeInterval since the 00:00:00 UTC Jan 1 2001 date.

Regards Mark

Beautiful !!!
And it automatically adjust for the time zone !!!
No need to calculate the (time to GMT)!

Thanks a million !
L.