-2

When I use below code I am getting different result on my developer pc and my remote server.

string _QsDateTime = "12.11.2016 21:30";
var _CountryZone = DateTimeZoneProviders.Tzdb["TUR"];
var _DatePattern = LocalDateTimePattern.CreateWithCurrentCulture("yyyy-MM-dd HH:mm:ss");
var _LocalTime = _DatePattern.Parse(_QsDateTime).Value;
var _LocalTime2TargetZoneTime = _LocalTime.InZoneStrictly(_CountryZone);
var _TargetZone2Utc = _LocalTime2TargetZoneTime.WithZone(DateTimeZone.Utc).ToDateTimeUtc();
_QsDateTime = _TargetZone2Utc.ToString("yyyy-MM-dd HH:mm:ss");

Developer PC result is: "2016-11-12 19:30:00" Remote server result is: "2016-12-11 19:30:00"

Remote server specs is Windows 2012 server English Developer PC specs is Windows 7 Turkish but both of them regional date time setting are same.

Why I am getting different result?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kerberos
  • 1,228
  • 6
  • 24
  • 48
  • Not very familiar with NodaTime but the `CreateWithCurrentCulture()` call looks suspicious to me. Obviously in some cultures day comes first and in other months come first. You're parsing a `string` that has a different meaning in different cultures using the `CurrentCulture` and expect the same result... that's not possible. What exactly are you trying to achieve? The code looks prone to bugs. – AlinG Nov 13 '16 at 12:36
  • I am trying to get same result. Like this "2016-11-12 19:30:00" – Kerberos Nov 13 '16 at 12:53
  • I get that. You need to understand that on the remote PC the string `12.11.2016 21:30` means `December, 11th 2016 19:30:00` and on your PC it means `November 12th 2016 19:30:30`. You cannot use `CurrentCulture`. Specify the `Culture` that your string is **supposed to be**. – AlinG Nov 13 '16 at 13:14
  • 1
    It should be `Europe/Istanbul` in my opinion. – Soner Gönül Nov 13 '16 at 15:38
  • That code should be giving you an exception such as "Unhandled Exception: NodaTime.TimeZones.DateTimeZoneNotFoundException: Time zone TUR is unknown to source TZDB: 2016c (mapping: 12348)". Please provide a real [mcve]. – Jon Skeet Nov 14 '16 at 21:06
  • The pattern you've shown will never parse the value you've specified, either... – Jon Skeet Nov 15 '16 at 08:07

1 Answers1

1

I'm not too much familiar with Noda Time but I have a few things to say:

  1. DateTimeZoneProviders.Tzdb does not have a time zone identifier as TUR as far as I know, you should use Europe/Istanbul instead.
  2. When you create a LocalDateTimePattern with CreateWithCurrentCulture method, it uses current culture settings and these are different in your both server. Be careful about that.
  3. LocalDateTimePattern.Parse method use the rules of the current pattern. Your string is 12.11.2016 21:30 but your pattern is yyyy-MM-dd HH:mm:ss. You see my point, don't you?
  4. If you really get different results in your servers, you shouldn't blame your last line since both en-US and tr-TR cultures uses GregorianCalendar as a Calendar property and that doesn't effect the result. You might wanna check LocalDateTimePattern.Parse method line instead.

For example;

using System;
using NodaTime;
using NodaTime.Text;

public class Program
{
    public static void Main()
    {
        string _QsDateTime = "12.11.2016 21:30";
        var _CountryZone = DateTimeZoneProviders.Tzdb["Europe/Istanbul"];
        var _DatePattern = LocalDateTimePattern.CreateWithCurrentCulture("dd.MM.yyyy HH:mm");
        var _LocalTime = _DatePattern.Parse(_QsDateTime).Value;
        var _LocalTime2TargetZoneTime = _LocalTime.InZoneStrictly(_CountryZone);
        var _TargetZone2Utc = _LocalTime2TargetZoneTime.WithZone(DateTimeZone.Utc).ToDateTimeUtc();
        _QsDateTime = _TargetZone2Utc.ToString("yyyy-MM-dd HH:mm:ss");
        Console.WriteLine(_QsDateTime);
    }
}

generates

2016-11-12 19:30:00

Here a demonstration.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364