Newtonsoft.json: IsoDateTimeConverter does not deal with DateTimeStyles.AssumeLocal

Created on 24 Jul 2018  Â·  2Comments  Â·  Source: JamesNK/Newtonsoft.Json

The Model

  public class MessageAlertLogModel
    {

        public DateTime timestamp { get; set; }

        public string channel { get; set; }

        public string service { get; set; }

        public string unique_id { get; set; }

        public string action { get; set; }

        public Dictionary<string, string> property { get; set; }
    }

My Code

               var converter = new IsoDateTimeConverter()
                {
                    DateTimeStyles = DateTimeStyles.AssumeLocal,
                    DateTimeFormat = "o"
                };
                var result = JsonConvert.SerializeObject(log, converter);
                Console.WriteLine(result);

I think result should be like this :

                {"timestamp":"2018-07-24T17:22:55.0000000+08:00","channel":"string","service":"string","unique_id":"string","action":"string","property":{}}

But in fact, the result is this:

                {"timestamp":"2018-07-24T09:22:55.0000000","channel":"string","service":"string","unique_id":"string","action":"string","property":{}}

I looked at the source code of IsoDateTimeConverter and found that it did not convert time to Local type. It only dealt with UTC type and did not deal with Local type.so even if I declare the DateTimeStyles = DateTimeStyles. AssumeLocal at my code, it also handles the datetime as an Unspecified type.

This is IsoDateTimeConverter part of the source code:

 DateTime dateTime = (DateTime) value;
        if ((this._dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal || (this._dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
          dateTime = dateTime.ToUniversalTime();
        str = dateTime.ToString(this._dateTimeFormat ?? "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK", (IFormatProvider) this.Culture);

But Datetime may need to be converted to Local type, I think it should be like this

  DateTime dateTime = (DateTime)value;
                if ((this._dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal || (this._dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
                    dateTime = dateTime.ToUniversalTime();
                if (this._dateTimeStyles == DateTimeStyles.AssumeLocal)
                    dateTime = dateTime.ToLocalTime();
                str = dateTime.ToString(this._dateTimeFormat ?? "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK", (IFormatProvider)this.Culture);

Thanks

Most helpful comment

I just reported a similar issue with AssumeUniversal ( #1804 ).

IMHO AssumeLocal and AssumeUniversal should NOT convert dates to / from local / universal

All 2 comments

I just reported a similar issue with AssumeUniversal ( #1804 ).

IMHO AssumeLocal and AssumeUniversal should NOT convert dates to / from local / universal

Hi, will it be fixed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Phreak87 picture Phreak87  Â·  11Comments

TylerBrinkley picture TylerBrinkley  Â·  37Comments

schani picture schani  Â·  11Comments

jskeet picture jskeet  Â·  20Comments

MuiBienCarlota picture MuiBienCarlota  Â·  14Comments