[CSHARP-185] DateTime timezone problem Created: 29/Mar/11  Updated: 02/Apr/15  Resolved: 30/Mar/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.0
Fix Version/s: 1.0

Type: Bug Priority: Major - P3
Reporter: Stefan Prodan Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win7 SP1 x64 / VS.NET 2010 SP1 / C# 4.0
Driver Version 1.0.0.4098



 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



 Comments   
Comment by Michael Trembovler [ 20/Feb/14 ]

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

{ get; set; }

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

Comment by Michael Trembovler [ 20/Feb/14 ]

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

Comment by Stefan Prodan [ 29/Mar/11 ]

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

Comment by Robert Stam [ 29/Mar/11 ]

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.

Comment by Stefan Prodan [ 29/Mar/11 ]

[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();
}
}

Comment by Robert Stam [ 29/Mar/11 ]

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)

Generated at Wed Feb 07 21:36:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.