Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Major - P3 Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 1.0
    • Fix Version/s: 1.0
    • Component/s: None
    • Labels:
      None
    • Environment:
      Win7 SP1 x64 / VS.NET 2010 SP1 / C# 4.0
      Driver Version 1.0.0.4098
    • Operating System:
      Windows
    • # Replies:
      6
    • Last comment by Customer:
      true

      Description

      Value inserted in database
      2010-03-30 12:00:00 AM

      Value returned from database
      2010-03-29 09:00:00 PM

      My Zone is +2GMT

        Activity

        Hide
        Robert Stam
        added a comment -

        Can you provide some sample code showing how you inserted the value into the database?

        Also, can you tell me what time zone your machine is set to? You can get this from the Date and Time control panel. For example, mine is:

        (UTC-05:00) Eastern Time (US & Canada)

        Show
        Robert Stam
        added a comment - Can you provide some sample code showing how you inserted the value into the database? Also, can you tell me what time zone your machine is set to? You can get this from the Date and Time control panel. For example, mine is: (UTC-05:00) Eastern Time (US & Canada)
        Hide
        Stefan Prodan
        added a comment - - edited

        [Serializable]
        public class MyData
        {
        [BsonId(IdGenerator = typeof(CombGuidGenerator))]
        public Guid Id

        { get; set; }

        public DateTime Date { get; set; }

        }

        class Program
        {
        static void Main(string[] args)
        {
        //system time zone is (UTC+2:00) Athens, Bucharest, Istanbul

        MongoServer srv = MongoServer.Create(new MongoConnectionStringBuilder()

        { Server = new MongoServerAddress("localhost", 27017) }

        );
        MongoDatabase db = srv["Test"];
        MongoCollection<MyData> col = db.GetCollection<MyData>("MyData");

        var originalDate = new DateTime(2011, 3, 30, 0, 0, 0);
        Console.WriteLine("Original Value:

        {0}", originalDate);
        //output: 3/30/2011 12:00:00 AM

        col.Insert<MyData>(new MyData() { Date = originalDate });
        var mongoDate = col.FindOne().Date;
        Console.WriteLine("MongoDb Value: {0}

        ", mongoDate);
        //output: 3/29/2011 10:43:26 PM !!!

        Console.ReadLine();
        }
        }

        Show
        Stefan Prodan
        added a comment - - edited [Serializable] public class MyData { [BsonId(IdGenerator = typeof(CombGuidGenerator))] public Guid Id { get; set; } public DateTime Date { get; set; } } class Program { static void Main(string[] args) { //system time zone is (UTC+2:00) Athens, Bucharest, Istanbul MongoServer srv = MongoServer.Create(new MongoConnectionStringBuilder() { Server = new MongoServerAddress("localhost", 27017) } ); MongoDatabase db = srv ["Test"] ; MongoCollection<MyData> col = db.GetCollection<MyData>("MyData"); var originalDate = new DateTime(2011, 3, 30, 0, 0, 0); Console.WriteLine("Original Value: {0}", originalDate); //output: 3/30/2011 12:00:00 AM col.Insert<MyData>(new MyData() { Date = originalDate }); var mongoDate = col.FindOne().Date; Console.WriteLine("MongoDb Value: {0} ", mongoDate); //output: 3/29/2011 10:43:26 PM !!! Console.ReadLine(); } }
        Hide
        Robert Stam
        added a comment -

        When I set my timezone to Athens and run this program the output I get is:

        1. 3/30/2011 12:00:00 AM
        2. 3/29/2011 09:00:00 PM

        These are the same time. The first time is in local Athens time, and the second time is in UTC.

        MongoDB stores all DateTimes in UTC. Any local times you supply are converted to UTC when stored in the database. The recommended approach is to always convert DateTime values to UTC yourself before storing them in the database, that way you are in full control.

        You can also tell the C# driver that you want to work in LocalTime, like this:

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime Date

        { get; set; }

        Note: watch out for FindOne! It doesn't necessarily return the same document you just inserted. If the collection has other documents it's hard to know which document you will get.

        Show
        Robert Stam
        added a comment - When I set my timezone to Athens and run this program the output I get is: 1. 3/30/2011 12:00:00 AM 2. 3/29/2011 09:00:00 PM These are the same time. The first time is in local Athens time, and the second time is in UTC. MongoDB stores all DateTimes in UTC. Any local times you supply are converted to UTC when stored in the database. The recommended approach is to always convert DateTime values to UTC yourself before storing them in the database, that way you are in full control. You can also tell the C# driver that you want to work in LocalTime, like this: [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime Date { get; set; } Note: watch out for FindOne! It doesn't necessarily return the same document you just inserted. If the collection has other documents it's hard to know which document you will get.
        Hide
        Stefan Prodan
        added a comment -

        Thanks for your comments. I should have read the documentation more carefully before submitting this.
        I apologies for wasting your time.

        Show
        Stefan Prodan
        added a comment - Thanks for your comments. I should have read the documentation more carefully before submitting this. I apologies for wasting your time.
        Hide
        Michael Trembovler
        added a comment -

        I faced the problem of lack of time zone conversion to DateTime field when trying to save my objects in MongoDB ver. 2.4.5 using C # driver. In other words, the DateTime field stores (UTC / GMT). Mongo should handle the transition zone for DateTime field, but it is not happening. I have millions of records contains a DateTime field and performence is crucial for my case. I need competent answer ASAP. My e-mail : trembovler@gmail.com. BR, Michael Trembovler

        Show
        Michael Trembovler
        added a comment - I faced the problem of lack of time zone conversion to DateTime field when trying to save my objects in MongoDB ver. 2.4.5 using C # driver. In other words, the DateTime field stores (UTC / GMT). Mongo should handle the transition zone for DateTime field, but it is not happening. I have millions of records contains a DateTime field and performence is crucial for my case. I need competent answer ASAP. My e-mail : trembovler@gmail.com. BR, Michael Trembovler
        Hide
        Michael Trembovler
        added a comment -

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime Date

        { get; set; }

        This is works nice for DateTime fields.
        Sorry for trouble.

        Show
        Michael Trembovler
        added a comment - [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime Date { get; set; } This is works nice for DateTime fields. Sorry for trouble.

          People

          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since reply:
              8 weeks ago
              Date of 1st Reply: