How to record UTC time

topbktopbk AUMember ✭✭

Hello,

I have a Xamarin cross-platform app. In there I need to record Created At Time in UTC for audit purpose. Currently, I'm using DateTime.UtcNow.

The issue is if the user changes the Date Time from the phone then the Created At Time also recorded base on the phone date. What we need real date and time for the audit.

Any idea..?

Thanks
Sid

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    @topbk said:
    Hello,

    I have a Xamarin cross-platform app. In there I need to record Created At Time in UTC for audit purpose. Currently, I'm using DateTime.UtcNow.

    The issue is if the user changes the Date Time from the phone then the Created At Time also recorded base on the phone date. What we need real date and time for the audit.

    Any idea..?

    Thanks
    Sid

    You could ping a web service that returns the utc. I dont know of one but it would be very easy to write one.

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    @JohnH said:

    @topbk said:

    You could ping a web service that returns the utc. I dont know of one but it would be very easy to write one.

    https://www.pool.ntp.org/en/

  • JohnHardmanJohnHardman GBUniversity mod

    @topbk said:
    The issue is if the user changes the Date Time from the phone then the Created At Time also recorded base on the phone date. What we need real date and time for the audit.

    If the app has connectivity, use a NTP server (see https://www.pool.ntp.org/zone/@ ).

    If the app has to still work when the phone does not have connectivity, then you have to use a time value generated locally, so either the time from the phone itself or a time that your app manages. As you said, the time from the phone could be changed in Settings. However, attempting to manage a time in your app, even if it is seeded from the NTP server when connectivity is available, is both tricky (you'd have to cope with the app being backgrounded or terminated) and unreliable (timer lag is likely). You might find it easier to use the phone's time but add logging that records when the phone's time goes backwards. It still wouldn't be 100%, but it's a reasonable compromise.

  • topbktopbk AUMember ✭✭

    Thanks for the ideas. My APP does not have internet. The user only connects to the internet when they want to sync the data to could. Can't we get the time from the telco provider?

  • JohnHardmanJohnHardman GBUniversity mod
    edited July 19

    Whilst the carrier does provide time information, the question is whether there is any way to access it programmatically. The other alternative is GPS. The problem with those is that for the carrier you need phone signal, and for GPS you need to be picking up signal from 3 or more satellites (hmmm... actually, not sure about that. Need 3 or more for position, assuming that you do for an accurate time, but perhaps 1 would be enough for an almost accurate time. Not sure). I know of locations near me where I get neither carrier nor GPS signal, so neither is a 100% solution.

    For GPS, I believe you can get the time on Android, iOS and UWP. For carrier, I believe you can on Android, but I'm not sure about iOS (I'd have to research that one).

  • cbdcbd Member ✭✭

    All GSM networks include a time signal along with a timezone offset for the current cell location.
    There's a discussion on stackoverflow that may help (at least for android) :smile: https://stackoverflow.com/questions/8049912/how-can-i-get-the-network-time-from-the-automatic-setting-called-use-netw

Sign In or Register to comment.