[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 |
||
| Description |
|
Value inserted in database Value returned from database My Zone is +2GMT |
| Comments |
| Comment by Michael Trembovler [ 20/Feb/14 ] |
|
[BsonDateTimeOptions(Kind = DateTimeKind.Local)] This is works nice for DateTime fields. |
| 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. |
| 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 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)] 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 DateTime Date { get; set; } } class Program MongoServer srv = MongoServer.Create(new MongoConnectionStringBuilder() ); var originalDate = new DateTime(2011, 3, 30, 0, 0, 0); //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); 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) |