简体   繁体   English

如何在c#中设置毫秒精度

[英]How can set the precision of millisecond in c#

I have a Rest ws in c#. 我在c#中有一个休息。 With this ws, I can put a date time with millisecond. 有了这个ws,我可以用毫秒来设置日期时间。 This is an example of my request: 这是我的请求的一个例子:

{ "sensorId": 656, 
"value": "128", 
"timestamp": "2016-06-01 11:20:50.125", 
"values":
    [{"value":"2064","timestamp":"2016-06-09 13:23:50.100"}]
}

As you can see there are two date with millisecond. 如您所见,有两个日期以毫秒为单位。 The first date, finish with .125 millisecond. 第一次约会,以.125毫秒结束。

In my code I use this code to deserialize the field Date. 在我的代码中,我使用此代码反序列化字段Date。

public class SensorDateTimeConverter : DateTimeConverterBase//IsoDateTimeConverter
    {
        private const string Format = "yyyy-MM-dd HH:mm:ss.fff";

        public override void WriteJson(JsonWriter writer, 
                                       object value, 
                                       JsonSerializer serializer)
        {
            writer.WriteValue(((DateTime)value).ToString(Format));
            writer.Flush();
        }

        public override object ReadJson(JsonReader reader, 
                                        Type objectType, 
                                        object existingValue, 
                                        JsonSerializer serializer)
        {
            if (reader.Value == null)
            {
                return null;
            }

            var s = reader.Value.ToString();
            DateTime result;

            if (DateTime.TryParseExact(s, 
                                       Format, 
                                       CultureInfo.InvariantCulture,
                                       DateTimeStyles.None, 
                                       out result))
            {
                return result;
            }

            return DateTime.Now;
        }
    }

If I try to inspect on the method ReadJson the data are correctly parse and the millisecond is "125". 如果我尝试检查方法ReadJson,数据是正确解析的,毫秒是“125”。

If I try to save this value of my database I see the same date but with .127 millisecond. 如果我尝试保存我的数据库的这个值,我看到相同的日期,但是.127毫秒。

Why? 为什么?

If I try to inspect on the method ReadJson the data are correctly parse and the millisecond is "125". 如果我尝试检查方法ReadJson,数据是正确解析的,毫秒是“125”。

This tells you that the problem is not with JSON conversion. 这告诉您问题不在于JSON转换。 All the code posted is therefore unrelated to the problem. 因此,所有发布的代码都与问题无关。 You already succeeded in bisecting where the problem is but you investigated the wrong partition. 您已经成功地将问题分成两部分,但是您调查了错误的分区。

SQL Server's datetime datatype has limited precision. SQL Server的datetime数据类型具有有限的精度。 Use datetime2 . 使用datetime2

If I try to save this value of my database I see the same date but with .127 millisecond. 如果我尝试保存我的数据库的这个值,我看到相同的日期,但是.127毫秒。

Assuming that you're saving to an SQL database as a datetime this is the limit of the precision you can get: 假设你是保存到一个SQL数据库的datetime ,这是精度就可以得到限制:

datetime values are rounded to increments of .000, .003, or .007 seconds, as shown in the following table. datetime值四舍五入为.000,.003或.007秒的增量,如下表所示。

Source 资源

If you want more precision then you should use different column types: 如果您想要更高的精度,那么您应该使用不同的列类型:

Use the time, date, datetime2 and datetimeoffset data types for new work. 使用time,date,datetime2和datetimeoffset数据类型进行新工作。 These types align with the SQL Standard. 这些类型与SQL标准一致。 They are more portable. 它们更便携。 time, datetime2 and datetimeoffset provide more seconds precision . time,datetime2和datetimeoffset提供更多的秒精度 datetimeoffset provides time zone support for globally deployed applications. datetimeoffset为全局部署的应用程序提供时区支持。

(From the same page with added emphasis). (从同一页面得到更多重视)。

The datetime2 page has more information on this structure and its limitations. datetime2页面提供了有关此结构及其局限性的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM