Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeOffset not being saved as UTC but only on server? #900

Closed
VictorioBerra opened this issue Oct 26, 2019 · 5 comments
Assignees

Comments

@VictorioBerra
Copy link

@VictorioBerra VictorioBerra commented Oct 26, 2019

The issue

Locally when I debug on Windows, if I set a local date and time to a DateTimeOffset and save, it looks like it gets saved in UTC form as expected. However, when I publish as a self-contained linux deployment and I push it out to my EC2 Amazon LKinux 2 VM it saves the same local time as a local time. It doesnt convert to UTC.

My VM time looks to be in UTC as well:

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip- ~]$ date
Sat Oct 26 15:40:29 UTC 2019

Here is a screenshot from PHPMyAdmin of my MySql table and the column I am using:

image

This is after clicking save 13 minutes apart, the first time locally while debugging, and the second when deployed. Save exact version of my software, same database. The only difference is the host.

Further technical details

MySQL version: 5.7.?
Operating system: Linux Amazon 2 EC2
Pomelo.EntityFrameworkCore.MySql version: 3.0.0-rc1.final
Microsoft.AspNetCore.App version: 3

@lauxjpn

This comment has been minimized.

Copy link
Collaborator

@lauxjpn lauxjpn commented Oct 27, 2019

The following test works as expected:

using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace IssueConsoleTemplate
{
    public static class TestHelpers
    {
        internal static DateTimeOffset TestValue = new DateTimeOffset(
            2019, 12, 24,
            18, 11, 42,
            TimeSpan.FromHours(1));
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public DateTimeOffset CreatedUtcDateTime { get; set; }
    }

    public class Issue900Context : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder
                .UseMySql("server=127.0.0.1;port=3306;user=root;password=;database=Issue900")
                .EnableDetailedErrors();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>()
                .HasData(new Blog { BlogId = 1, CreatedUtcDateTime = TestHelpers.TestValue});
        }
    }

    internal class Program
    {
        private static void Main()
        {
            using var context = new Issue900Context();

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var theBlog = context.Blogs.Single();

            // 01:00:00 == 01:00:00
            Debug.Assert(TestHelpers.TestValue.Offset == TimeSpan.FromHours(1));
            
            // 00:00:00 == 00:00:00
            Debug.Assert(theBlog.CreatedUtcDateTime.Offset == TimeSpan.Zero);

            // 2019-12-24 17:11:42 == 2019-12-24 17:11:42
            Debug.Assert(theBlog.CreatedUtcDateTime.UtcDateTime == TestHelpers.TestValue.UtcDateTime);

            // 2019-12-24 17:11:42 +00:00 == 2019-12-24 17:11:42
            Debug.Assert(theBlog.CreatedUtcDateTime == TestHelpers.TestValue.UtcDateTime);

            // 2019-12-24 17:11:42 != 2019-12-24 18:11:42
            Debug.Assert(theBlog.CreatedUtcDateTime.DateTime != TestHelpers.TestValue.DateTime);

            // Converted to your local systems timezone
            Debug.Assert(theBlog.CreatedUtcDateTime.LocalDateTime == TestHelpers.TestValue.LocalDateTime);

            // 2019-12-24 17:11:42 +00:00 == 2019-12-24 18:11:42 +01:00
            Debug.Assert(theBlog.CreatedUtcDateTime == TestHelpers.TestValue);
        }
    }
}

So I cannot reproduce this issue.
Please provide us with a sample program (or just alter the code I used), so we can reproduce the issue.

Please beware, that there was an issue regarding DateTimeOffset that was fixed with #845. This fix is part of 3.0.0-rc1.final.
Are you sure, that the linux container is using 3.0.0-rc1.final?

@VictorioBerra

This comment has been minimized.

Copy link
Author

@VictorioBerra VictorioBerra commented Oct 27, 2019

Yes I'm sure of the version. Did the server you test on have time set to UTC?

@lauxjpn

This comment has been minimized.

Copy link
Collaborator

@lauxjpn lauxjpn commented Oct 27, 2019

I checked this with UTC and UTC+1 on the server (not a docker container, but directly on the Ubuntu AMI) and client. It works every time:

mysql> select * from blogs;
+--------+----------------------------+
| BlogId | CreatedUtcDateTime         |
+--------+----------------------------+
|      1 | 2019-12-24 17:11:42.000000 |
+--------+----------------------------+
1 row in set (0.00 sec)

This is likely a user or configuration error on your end.

Does my provided test code run, or does any of the assert statements trigger?

@lauxjpn lauxjpn self-assigned this Oct 27, 2019
@VictorioBerra

This comment has been minimized.

Copy link
Author

@VictorioBerra VictorioBerra commented Oct 27, 2019

I will have to test when I get to a computer tomorrow morning.

@VictorioBerra

This comment has been minimized.

Copy link
Author

@VictorioBerra VictorioBerra commented Oct 28, 2019

I tried to repro this with your sample and another isolated sample I had and I am struggling to do so. There were many more layers in my app like MVC Model Binding, AutoMapper going from Dtos to my entities, so maybe something somewhere in all that cause my issue. Thanks for looking into it.

In the end, I changed my entity data type to a DateTime and then I do all UTC conversions on the client, this way any issue with the automatic conversions can only happy on the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.