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?
I'm not too much familiar with Noda Time but I have a few things to say:
DateTimeZoneProviders.Tzdb does not have a time zone identifier as TUR as far as I know, you should use Europe/Istanbul instead.
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.
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?
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.
Related
I am trying to do a UWP app that changes wallpaper and lockscreen based on the time of the day (as a start). I am facing difficulties implementing this:
I have the task "SetWallpaperAsync" on main page (I am making sure it works before shifting it to a background task) as fallowing:
private async Task<bool> SetWallpaperAsync()
{
bool success = false;
if (UserProfilePersonalizationSettings.IsSupported())
{
var imageID = DateTime.Now.Hour.ToString("HH");
var uri = new Uri($"ms-appx:///Dynamic/Dynamic-{imageID}.jpg");
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
success = await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(file);
success = await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(file);
}
return success;
}
But it only worked once then it stopped, which lead me to believe that the imageID string is not returning the correct value (the value is between 00 and 23, corresponding to the hours in a day in a 24 format).
I have also tried to link a textblock to the same value and different formats, but it did not appear on the app homepage. I am not sure at all where its going wrong!
// Time format
var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour");
DateTime dateToFormat = DateTime.Now;
var mydate = formatter.Format(dateToFormat);
TimeDescriptionTextBlock.Text = mydate.ToString();
// another try with the time
TimeHours.Text = DateTime.Now.ToString("HH:mm:ss");
How can I debug this code and see where its stopping?
If you output the imageID variable in the Output windows using Debug.Writeline:
var imageID = DateTime.Now.Hour.ToString("HH");
Debug.WriteLine(imageID);
You can see that the output is: HH.
The reason for this is that the format string HH works on DateTime variables, but you are actually calling it on the Hour variable only, which is just a number.
There are two possible solutions:
Remove the .Hour from the call: var imageID = DateTime.Now.ToString("HH");
Just use the Hour as a number: var imageID = DateTime.Now.Hour.ToString();
Both solutions are equivalent and will yield a 24-hour format hour number.
Basically I can able to detect list of system time zones using following code:
foreach(TimeZoneInfo info in tz)
Debug.Log("time zone id : " + info.Id + " display name : " + info.DisplayName);
Running this code, I have following output in console.
In this list, I want to know, which one is my current system is following?
Because I want specific information about that time zone. As like in following code, I was written clear "Asia/Kolkata".
TimeZoneInfo infos = System.TimeZoneInfo.FindSystemTimeZoneById("Asia/Kolkata");
By running which code, I can able to get "Asia/Kolkata" as output?
EDIT: I was already using NodaTime here and code running for this purpose.
// Create a NodaTime DateTimeZoneSource object for IANA time zone names
var dateTimeZoneSource = TzdbDateTimeZoneSource.Default;
// Get the Windows time zone by name (or use TimeZoneInfo.Local)
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
// Convert between Windows and IANA time zone names
var tzdbTimeZoneInfo = dateTimeZoneSource.MapTimeZoneId(timeZoneInfo);
Console.WriteLine(tzdbTimeZoneInfo);
But in this, I am getting exception regarding,
Exception of type 'System.TimeZoneNotFoundException' was thrown.
I am using Mac system and editor as Mono Develop - Unity and my target devices are iPhones.
Using the code below you can get local TimeZoneInfo object.
TimeZoneInfo infos = TimeZoneInfo.Local;
Set your .NET API compatibility to 4.6. Here are the test results in my iOS device:
TimeZoneInfo.Local.Id
Editor: Local
iOS Device: Local
TimeZoneInfo.Local.DisplayName
Editor: Local
iOS Device: (GMT+03:30) Local Time
TimeZoneInfo.Local.StandardName
Editor: +0330
iOS Device: +0330
TimeZoneInfo.Local.DaylightName
Editor: +0430
iOS Device: +0430
TimeZoneInfo.Local.BaseUtcOffset.Hours + ":" + TimeZoneInfo.Local.BaseUtcOffset.Minutes);
Editor: 3:30
iOS Device: 3:30
maybe help u this links :
here
https://msdn.microsoft.com/en-us/library/gg154758.aspx
and
here
List of Timezone ID's for use with FindTimeZoneById() in C#?
Use this
TimeZoneInfo infos = TimeZoneInfo.FindSystemTimeZoneById(
{ "Asia/Kolkata", "India Standard Time" });
You can see this link for TimeZoneIds
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html
On window Unity5.1.3 editor, TimeZoneInfo.GetSystemTimeZones() return empty list.
TimeZoneInfo.FindSystemTimeZoneById() and TimeZoneInfo.CreateCustomTimeZone() both raise System.TimeZoneNotFoundException.
but it work well in android device.
private static DateTime ToSeoulTime(DateTime dateUtc, bool ignoreUnspecified = false)
{
if (ignoreUnspecified == false && dateUtc.Kind == DateTimeKind.Unspecified)
{
if (UnityEngine.Application.isEditor)
{
throw new Exception("dateUtc.Kind == DateTimeKind.Unspecified");
}
else
{
UnityEngine.Debug.LogWarning("dateUtc.Kind == DateTimeKind.Unspecified");
}
}
try
{
var seoulTimezone = TimeZoneInfo.FindSystemTimeZoneById("Korea Standard Time");
return TimeZoneInfo.ConvertTime(dateUtc, seoulTimezone);
}
catch (System.TimeZoneNotFoundException e)
{
return new DateTime(dateUtc.AddHours(9).Ticks, DateTimeKind.Unspecified);
}
}
Update your player settings to use .NET 4.6
Using C#, I'm trying to set dhcp option 12 using ManageDHCP.dll. In a nutshell, this is what I'm doing:
var dhcpManager = new DHCPManager(dhcpServerName)
var hostRecord = new HostInfo
{
Name = deviceDhcpName,
IPv4 = deviceIpAddress,
IsReserved = true,
MAC = deviceMacAddress
};
dhcpManager.SetRecord(dhcpServerScope, hostRecord);
String hostName = deviceDhcpName + "." + dnsZoneName;
var hostnameOpt = new Option
{
ID = 12,
Values = new List<string> { hostName }
};
dhcpManager.SetOption(hostnameOpt, dhcpServerScope, deviceIpAddress);
The thing is, is that it just dies at this point and the logs don't record an exception. So I was thinking maybe the format I'm sending it is not correct. So I went to try and get an option. So instead, after creating the reservation, with SetRecord function above - I called this instead, just to try an see if I could get an option to see the correct format:
Option test = dhcpManager.GetOption(3, dhcpServerScope, deviceIpAddress);
Which throws this exception:
ManageDHCP.DhcpException: DhcpGetOptionInfo
at ManageDHCP.DHCPManager._GetOption(OptionTarget target, UInt32 optionID, String subnet, String reservedIP)
I'm new to C# and I'm not really sure what's causing this exception. Here is a link to the source code for ManageDHCP. What am I doing wrong? Is my format incorrect?
It seems like the 32-bit dll's setOption function does not work (DhcpGetOptionInfo throws an exception), so I switched to 64 bit dll.
I use WebClient class to send a response acknowledge message in an mvc 3 project. Message is sending from one action method on a project to another projects action method.
Surprisingly the date parameter is 3 hours later, on recieving data.
For example if my sending date is receving data is "2012-08-14 13:42:50Z" i see "2012-08-14 16:42:50Z" on the other side.
Here is a simplified code sample of my case;
NameValueCollection ack = new NameValueCollection();
ack.Add("RESID", form.RESPONSE.ID.ToString());
ack.Add("A_DateTime", DateTime.Now.ToString("u")); //2012-08-14 13:42:50Z
using (var client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
var result = client.UploadValues("http://localhost:11578/HPM/ResponseAck", ack);
}
//HPM Controller:
ResponseAck(HttpPostResponseAckMessage response)
{
//Here response.Date vale is 2012-08-14 16:42:50Z ???
}
It seems to me its about sneaky little serialization monsters changing it cause of some culture specific issue. But i don't know the real cause so the solution.
Edit:
public class HttpPostResponseAckMessage
{
public int RESID { get; set; }
public DateTime A_DateTime { get; set; }
}
You should either change the culture of the current thread or convert and process all dates in a fixed format such as UTC.
You can change the current culture of the thread using the following code:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-EN");
Ok, I think its about expecting mvc model binder to parse a formated datetime.
With the "u" format model binder thinks the datetime is UTC.
So my solution will be changing the type of property A_DateTime to string and will parsing it internaly.
Hope this helps someone else like me.
Thanks all.
I have made a windows application using C# that creates a dataset with data from a Dbase database file using a OleDBConnection.
My problem is that this runs just fine on my own computer (swedish) but when i run this on my server (english) the swedish letters (å,ä,ö,Å,Ä,Ö) gets messed up.
I have tried this with no luck:
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("sv-SE");
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("sv-SE");
Ok well so i kinda fixed it myself, not the best of ways but it will work for me.
Since i will be running if on a English OS computer i made function that changed all the weird characters to swedish ones.
public string changeSpecialCharacters(string kund)
{
StringBuilder b = new StringBuilder(kund);
b = b.Replace("σ", "å");
b = b.Replace("┼", "Å");
b = b.Replace("Σ", "ä");
b = b.Replace("─", "Ä");
b = b.Replace("÷", "ö");
b = b.Replace("╓", "Ö");
return b.ToString();
}
Until i find a way to do this differently ill stick with it.