Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeSpan ticks get +1 after save

Tags:

c#

dapper

I have an issue with dapper, I don't know how to fix this :

I have a Poco like this :

public class Test
{
    public long Id { get; set; }
    public TimeSpan? Time { get; set; }
}

The field Time is a MySQL 'TIME'. If I load a row with Dapper with a Time field with 1000 ticks for example, and I save this Poco without change anything, reload the same row again, Time field is now at 1001 Ticks.

What am I doing wrong ?

EDIT :

How I load my row :

var testobj = Db.Query<Test>("select * from Test where Id = @id", new {id = Id});

How I save it :

Db.Execute("replace into Test values (@Id,@Time)", testObj);

EDIT 2 :

A timespan object before save :

{15:22:24}
    Days: 0
    Hours: 15
    Milliseconds: 0
    Minutes: 22
    Seconds: 24
    Ticks: 553440000000
    TotalDays: 0.64055555555555554
    TotalHours: 15.373333333333333
    TotalMilliseconds: 55344000.0
    TotalMinutes: 922.4
    TotalSeconds: 55344.0

and after save :

{15:22:25}
    Days: 0
    Hours: 15
    Milliseconds: 0
    Minutes: 22
    Seconds: 25
    Ticks: 553450000000
    TotalDays: 0.64056712962962958
    TotalHours: 15.37361111111111
    TotalMilliseconds: 55345000.0
    TotalMinutes: 922.41666666666674
    TotalSeconds: 55345.0

You can see that Ticks 553440000000 and become 553450000000

EDIT 3 :

I use Hans tip with my Test class like this :

public class Test
{
    public long Id { get; set; }
    private TimeSpan? _time;

    public TimeSpan? Time
    {
        get
        {
            if (_time.HasValue)
                return TimeSpan.FromTicks((long)Math.Floor(_time.Value.Ticks / 100000000d) * 100000000);
            return _time;
        }
        set { _time = value; }
    }
}

and it works, but it's still odd

like image 589
Bastiflew Avatar asked Nov 08 '22 23:11

Bastiflew


1 Answers

After much research, there was a mysql plugin developed by my company that doing something special in some cases. Sorry for the loss of time on this issue .

like image 169
Bastiflew Avatar answered Nov 14 '22 21:11

Bastiflew