Check System.DateTime in UTC

I have a requirement to store all dates recorded in the database in UTC. For now, I can do this using the Noda library with the following method:

    public static DateTime NowUtc(string timeZoneId)
    {
        var timeZone = GetTimeZone(timeZoneId);
        var instant = SystemClock.Instance.Now;
        return instant.InZone(timeZone).ToDateTimeUtc();
    }

I am going to check that every date that has passed since the data access level should be in UTC format.

How do I achieve this?

thanks

Note. I created a special class library that used Noda as the main core, and the result is converted back to System.DateTime.

+4
source share
2 answers

I'm not quite sure what you are asking, but here are some tips:

  • If you just need it now as UTC DateTime, just use it DateTime.UtcNow.

  • Noda Time DateTime, instant.ToDateTimeUtc(). , UTC.

  • a DateTime UTC, :

    dateTime.Kind == DateTimeKind.Utc
    
  • , , DateTimeKind.Unspecified DateTime, UTC, Noda Time Instant:

    DateTime dt = (DateTime) dataReader["YourDataField"];
    DateTime utc = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
    Instant instant = Instant.FromDateTimeUtc(utc);
    
  • , , UTC . . , " UTC" " UTC", " UTC".

+11

http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx

DateTimeOffset localServerTime = DateTimeOffset.Now;

DateTimeOffset utc = localServerTime.ToUniversalTime();

Console.WriteLine("UTC:{0}", utc);
0

Source: https://habr.com/ru/post/1523849/


All Articles