-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
735ee53
commit 7d3f20a
Showing
4 changed files
with
152 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,14 @@ | ||
using Foundation; | ||
using System; | ||
|
||
namespace Uno.Devices.Sensors.Helpers | ||
namespace Uno.Devices.Sensors.Helpers; | ||
|
||
internal static class SensorHelpers | ||
{ | ||
internal static class SensorHelpers | ||
public static DateTimeOffset TimestampToDateTimeOffset(double timestamp) | ||
{ | ||
private static readonly DateTimeOffset NSDateConversionStart = | ||
new DateTimeOffset(2001, 1, 1, 0, 0, 0, TimeSpan.Zero); | ||
|
||
public static DateTimeOffset TimestampToDateTimeOffset(double timestamp) | ||
{ | ||
var bootTime = NSDate.FromTimeIntervalSinceNow(-NSProcessInfo.ProcessInfo.SystemUptime); | ||
var date = (DateTime)bootTime.AddSeconds(timestamp); | ||
return new DateTimeOffset(date); | ||
} | ||
|
||
public static DateTimeOffset NSDateToDateTimeOffset(NSDate nsDate) | ||
{ | ||
if (nsDate == NSDate.DistantPast) | ||
{ | ||
return DateTimeOffset.MinValue; | ||
} | ||
else if (nsDate == NSDate.DistantFuture) | ||
{ | ||
return DateTimeOffset.MaxValue; | ||
} | ||
|
||
return NSDateConversionStart.AddSeconds( | ||
nsDate.SecondsSinceReferenceDate); | ||
} | ||
|
||
public static NSDate DateTimeOffsetToNSDate(DateTimeOffset dateTimeOffset) | ||
{ | ||
if (dateTimeOffset == DateTimeOffset.MinValue) | ||
{ | ||
return NSDate.DistantPast; | ||
} | ||
else if (dateTimeOffset == DateTimeOffset.MaxValue) | ||
{ | ||
return NSDate.DistantFuture; | ||
} | ||
|
||
var dateInSecondsFromStart = dateTimeOffset | ||
.ToUniversalTime() | ||
.Subtract(NSDateConversionStart.UtcDateTime); | ||
|
||
return NSDate.FromTimeIntervalSinceReferenceDate( | ||
dateInSecondsFromStart.TotalSeconds); | ||
} | ||
var bootTime = NSDate.FromTimeIntervalSinceNow(-NSProcessInfo.ProcessInfo.SystemUptime); | ||
var date = (DateTime)bootTime.AddSeconds(timestamp); | ||
return new DateTimeOffset(date); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Foundation; | ||
|
||
namespace Windows.Extensions; | ||
|
||
internal static class NSDateExtensions | ||
{ | ||
private static readonly DateTimeOffset NSDateConversionStart = | ||
new DateTimeOffset(2001, 1, 1, 0, 0, 0, TimeSpan.Zero); | ||
|
||
public static DateTimeOffset ToDateTimeOffset(this NSDate nsDate) | ||
{ | ||
if (nsDate == NSDate.DistantPast) | ||
{ | ||
return DateTimeOffset.MinValue; | ||
} | ||
else if (nsDate == NSDate.DistantFuture) | ||
{ | ||
return DateTimeOffset.MaxValue; | ||
} | ||
|
||
return NSDateConversionStart.AddSeconds( | ||
nsDate.SecondsSinceReferenceDate); | ||
} | ||
|
||
public static NSDate ToNSDate(this DateTimeOffset dateTimeOffset) | ||
{ | ||
if (dateTimeOffset == DateTimeOffset.MinValue) | ||
{ | ||
return NSDate.DistantPast; | ||
} | ||
else if (dateTimeOffset == DateTimeOffset.MaxValue) | ||
{ | ||
return NSDate.DistantFuture; | ||
} | ||
|
||
var dateInSecondsFromStart = dateTimeOffset | ||
.ToUniversalTime() | ||
.Subtract(NSDateConversionStart.UtcDateTime); | ||
|
||
return NSDate.FromTimeIntervalSinceReferenceDate( | ||
dateInSecondsFromStart.TotalSeconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters