Cross platform DateTimeOffset conversion to CST Time Zone - c#

I've written the following code to convert the DateTimeOffset to CST DateTime
var bookingDateInCst = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(itemRequest.BookingDate, "Central Standard Time");
This works on my local machine, but when I deployed it on PCF cloud environment I get the following error
"The time zone ID 'Central Standard Time' was not found on the local computer."
How to fix it?

I've used TZConvert utility, by reading this block
https://devblogs.microsoft.com/dotnet/cross-platform-time-zones-with-net-core/
It helped me fix the issue using following code:
var cstTimeZoneInfo = TZConvert.GetTimeZoneInfo("Central Standard Time");
var bookingDateInCst =TimeZoneInfo.ConvertTimeFromUtc(itemRequest.BookingDate.UtcDateTime, cstTimeZoneInfo);

Related

The time zone ID 'CST' was not found on the local computer

I am having problem in creating a shipment in MAC Operating System. In windows operating system the website works fine. Any Help would be appreciated
Thanks.
Code:
createOrUpdateShipment: function (shipmentHeader) {
----------
var tzm= (/\((.*)\)/.exec(new Date().toString())[1]);
return $http.post('/shipment/CreateOrUpdateShipment?tzm='+tzm, shipmentHeader)
},
And in the Controller
public ActionResult CreateOrUpdateShipment(ShipmentInfo shipmentInfo, string tzm)
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(tzm);
shipmentInfo.ServiceInfo.READYDATETYPE = TimeZoneInfo.ConvertTime(shipmentInfo.ServiceInfo.READYDATETYPE,tzi);
Below Error I am getting when I try to create the shipment using MAC Operating System
Getting error time zone not found exception. The time zone ID 'CST' was not found
Time zones in .NET are identified by their full names, not their abbreviations. You are making a call to FindSystemTimeZoneById. This API would expect "Central Standard Time", not "CST".
I would guess that your Mac client is sending tzm=CST and your Windows client is sending tzm=Central+Standard+Time. Your server only works when the client spells it out.

DateTime.Parse works in c# console app but not asp.net

I have written a C# console application that after all of it's processing outputs a DateTime to disk. It does this like so:
writer.WriteLine(myDateTime).
This same console appication has no problems with using the following to read this DateTime back:
DateTime.Parse(reader.ReadLine())
However, upon attempting to use the following code in my separate Asp.Net program I recieve an error saying that my string is not a valid DateTime which is odd to say the least.
StreamReader reader = new StreamReader(#"D:\InformerReports\Archive\ReliabilityData\StartTime.hist");
string dateString = reader.ReadLine();
return DateTime.Parse(dateString);
I have checked and the string it is reading in is 10/25/2016 12:00:00 AM.
I have also attempted to use return DateTime.ParseExact(dateString, "dd/MM/yyyy hh:mm:ss tt",null) but this returns the same error.
I can't seem to fathom why identical code performed on the same file works in one case and not the other. I'd appreciate some help.
I guess the culture of the server is different from the machine you are testing from.
The correct format you have to use seems to be:
return DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt", null)
// 10/25/2016 12:00:00 AM
As an add-on to the previous answers.
To avoid the differing cultures across clients you can set the site culture in the global.asax files Application_BeginRequest method like below:
Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");
This will force the above specified culture for each user accessing the site.
I am not sure if there are drawbacks to doing it this way, but this solved my issue in the past.

Azure culture-specific month/day formatting different than localhost

I'm running into a very strange issue where the "month/day" standard date format as specified on https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx is rendering differently on my local machine than it is on my azure cloud services and websites.
The culture in this case that is rendering differently is "en-AU". For the date of 2017-05-04 it should render as 4 May and on my local machine it does exactly that. On our website (azure cloud service) and our API (azure website) it renders as May 4. The strange part is that if I use the "short date pattern" it renders as 04/05/2017 on both azure/local. So this seems to be specific only to the "month/day" pattern.
I've tried setting
var culture = new CultureInfo("en-AU");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
and the formatting code is
string.Format(new CultureInfo("en-AU"), "Until {0:M} {0:yyyy}", endDate);
I'm wondering if its possible that the version of some culture definition is different in Azure than it is on my local machine? To my knowledge they are both running .net 4.5. I've added log statements in the code so I can confirm that the culture is set correctly on the line that the code runs, but for some reason, it is just outputting a different value in Azure than it does locally.
I have used both "en-AU" and "en-ZA" culture in both local and Azure environments.Unfortunately,I did not face the issue that you have mentioned in your question in both environments.
It seems the date format that you are getting is US format which might be due to the fact that azure data center that you are using to host your application is based in USA and your date is formatted to that culture.Anyway,give a try to format the date like :
var currentCulture = new CultureInfo("en-AU");
var formattedDate = DateTime.Now.ToString("G",currentCulture);
For the South African culture,try the following:
var currentCulture = new CultureInfo("en-ZA");
var formattedCurrency = currency.ToString("C", currentCulture);
//currency = 100000 then formattedCurrency => R 100 000,00
Good luck ..!!!

Time zone issue with "Malay Peninsula Standard Time"

I have a program that runs the following code on C#:
TimeZoneInfo localZone = TimeZoneInfo.Local;
string timeZone = TimeZoneInfo.FindSystemTimeZoneById(localZone.Id).ToString();
MessageBox.Show("Local Time Zone ID: " + timeZone);
When I run this, I got the following exception:
System.TimeZoneNotFoundException: The time zone ID 'Malay Peninsula Standard Time' was not found on the local computer.
I made a slight modification on the code and used this line:
TimeZoneInfo localZone = TimeZoneInfo.Local;
MessageBox.Show("Local Time Zone ID: " + localZone.StandardName);
And this time I got no exception, and I got the popup message with the time zone "Malay Peninsula Standard Time"
Why the first fragment of code is not working?
After some time (days), I finally found out what was happening.
Debugging the code, I see that TimeZoneInfo.Local.Id was retrieving the following value:
Malay Peninsula Standard Time
I made some investigation, and found out that all the time zones for the server are stored on this registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\
If you browse through them, you will see that no key is called "Malay Peninsula Standard Time", the value that is for that time zone is "Singapore Standard Time". The whole information for that time zone is the following:
ID: Singapore Standard Time
Display Name: (GMT+08:00) Kuala Lumpur, Singapore
Standard Name: Malay Peninsula Standard Time
Daylight Name: Malay Peninsula Daylight Time ***Does Not Have Daylight Saving Time***
Offset from UTC: 8 hours, 0 minutes
Number of adjustment rules: 0
So, as you see, the ID that I was looking for did not exist, and that's why I got that exception. I renamed the registry key to "Malay Peninsula Standard Time" and the problem was gone.

Get PC TimeZone on C#

Im currently developing on asp - c# as a backend code. I want to get the current timezone that was set on the PC.
The below code are still identifying what is the correct timezone of my current area (+8GMT) even though I already changed my PC timezone settings into another timezone.
What I want is to get the timezone offset specified on the PC date settings. Can anyone help me on this. Below is my code so far.
public TimeSpan currentOffset;
public DateTime utc;
public DateTime local;
this.utc = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, TimeZoneInfo.Utc.Id);
this.local = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, TimeZoneInfo.Local.Id);
this.currentOffset = this.local.Subtract(this.utc);
In addition to David Haney's answer.
TimeZoneInfo is caching data after first call so any changes in PC settings will not affect your application if it is already running.
You should call method:
TimeZoneInfo.ClearCachedData();
to refresh this cache.
So this one will work in your case:
TimeZoneInfo.ClearCachedData();
var offsetTimespan = DateTimeOffset.Now.Offset;
var offsetInHours = offsetTimespan.TotalHours;
You're making your life harder than it needs to be. :)
Use DateTimeOffset: http://msdn.microsoft.com/en-us/library/system.datetimeoffset.now%28v=vs.110%29.aspx
var now = DateTimeOffset.Now;
This will include the time zone offset information as well.

Categories