I am using Dot Net Core 3 and inside my code using this to get the current Date
DateTime.Now
Trying out with this logs
Console.WriteLine("----- Local Time : " + DateTime.Now + "UTC : " + DateTime.UtcNow);
Giving OutPut Like : ----- Local Time : 01/12/2023 11:27:49 UTC : 01/12/2023 11:27:49
But, when i am checking my logs it's prining the UTC time now the Local Time.
Below i have attached the screen showing time on my server . This output i have received using
timedatectl
Anyone knows what's going here ?
Related
I facing an issue when retrieving date and time along with time zone in C# using couchbase SDK. Please find the details below.
This is the couchbase Insert Query which is having different country date and time along with time zone
Upsert Query
UPSERT INTO DtTest (KEY,VALUE)
VALUES ( "TZ:Dates", {
"Tokyo" : "2020-06-18T15:26:00+09:00",
"Beijing" : "2020-06-18T14:26:00+08:00",
"Moscow" : "2020-06-18T09:26:00+03:00",
"Paris" : "2020-06-18T08:26:00+02:00",
"LosAngeles" : "2020-06-17T23:26:00-07:00"
})
This is couchbase Select Query, where we can check by using console
Select Query
`select * from DtTest use keys "TZ:Dates"`
Couchbase Select Query Result in console and we didnt see any issue here
{
"Beijing": "2020-06-18T14:26:00+08:00",
"LosAngeles": "2020-06-17T23:26:00-07:00",
"Moscow": "2020-06-18T09:26:00+03:00",
"Paris": "2020-06-18T08:26:00+02:00",
"Tokyo": "2020-06-18T15:26:00+09:00"
}
When we are trying to retrieving from Couchbase SDK in C#
Couchbase SDK code in C#
`_bucket.Get<dynamic>("TZ:Dates");`
Couchbase SDK Result in C# which is converting the timezone to my system local timezone
{
"Beijing": "2020-06-18T11:56:00+05:30",
"LosAngeles": "2020-06-18T11:56:00+05:30",
"Moscow": "2020-06-18T11:56:00+05:30",
"Paris": "2020-06-18T11:56:00+05:30",
"Tokyo": "2020-06-18T11:56:00+05:30"
}
Note : Please help me on this to retrieve exact time zones how we are getting from Couchbase Console
I am logging time in many places
If Request.DynamicSettings.AirlineSettings.AirlineGeneralSettings.TimeLogEnabled Then
StartTime = DateTime.Now
LogTime(Reflection.MethodBase.GetCurrentMethod.DeclaringType.FullName, Reflection.MethodBase.GetCurrentMethod.Name, StartTime, DateTime.Now, "AB-SCR(I)", 0,)
End If
all places i have used
DateTime.Now
I am facing an issue now,
I am currently hosting this in a gulf server, GMT +4:00
I need to host this same project for another country at Gmt +3Gmt
for this hosting i need time to be logged using that country's local time.
Is there any way to do this, without having to modify each and every line of my code.
i have seen this article timzone with asp.net but as my service is already up i have a lot of codes to change, i am looking for a simpler solution.
thanks.
A few things:
You cannot change the time zone in the IIS configuration or web.config. This is not an IIS problem, but rather a problem in your application code.
DateTime.Now should never be used in a server side application, such as an ASP.Net web application. Read The case against DateTime.Now.
If you are just timing how long something takes to run, don't use DateTime at all. Instead, use System.Diagnostics.Stopwatch.
Stopwatch sw = Stopwatch.StartNew();
// ...do some work ...
sw.Stop();
TimeSpan elapsed = sw.Elapsed; // how long it took will be in the Elapsed property
If you actually want the current time in a specific time zone, then you need to know the time zone identifier. (GMT+4 and GMT+3 are not time zones, but rather time zone offsets see "Time Zone != Offset" in the timezone tag wiki.) You can see a list of Windows time zones by using TimeZoneInfo.GetSystemTimeZones(), or by calling tzutil /l on the command line.
Then in your application:
string tz = "Arabian Standard Time";
DateTime now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
You should probably refactor your code such that this is done inside your LogTime method. Then you will have only one place to set the time zone for your application.
When we use System.DateTime.Now it displays the DateTime as per what is set in our system (if I am not wrong).
Like currently my system DateTime settings are as below :
Now I created a simple Console Application with a small snippet :
Console.WriteLine(System.DateTime.Now);
Console.WriteLine(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern+" "+System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
System.IO.File.WriteAllText("E:\\perls.txt", System.DateTime.Now.ToString() + "\r\n" + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
The output both console and text is :
Now same thing I am doing via windows service and Logging some information in a Text File and even creating the same file as created in above sample:
(I have a simple Log class which logs the info i am not getting into its details)
So code is something like below:
Log.Info("System Date Time : " + System.DateTime.Now);
Log.Info("Date Time format : " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
System.IO.File.WriteAllText("E:\\perls-service.txt", System.DateTime.Now.ToString() + "\r\n" + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
Now in my Log File (a text file) and the text file which I created gives following output:
Both samples are executed in the same system then why is this difference?
Where am I wrong?
Or this problem is in my system only.
You must be running into the issue described here
https://social.msdn.microsoft.com/Forums/vstudio/en-US/8fbca78b-5078-4a12-8abb-4051076febbb/c-windows-service-culture-info-problem
Your Windows Service most likely runs under one of the service accounts with the culture defined in the registry.
See if you updating the registry settings below fixes the problem.
[HKEY_USERS\.DEFAULT\Control Panel\International]
"Locale"="00000409"
"LocaleName"="en-US"
I have a C# Web Service running on a Windows machine, and i have this code:
Details = tInfoRequestHistory.Select(i => new AppJsonDetails
{
Type = i.PlatformUserId == null ? "Request" : "Response",
Date = i.Date.ToString("D"),
Time = i.Date.ToString("hh:mm tt"),
Text = i.Comment,
}).ToArray()
And i'm receiving this JSON in my app:
{
Date = "lunes, 12 de enero de 2015";
Text = "402\\nGarcia\\n01/12/15 12:52 \\nBla bla";
Time = "11:52 ";
Type = Request;
}
As you can see i'm missing the PM/AM (in fact there is a blank space) in the node "Time" that is generated with this instruction i.Date.ToString("hh:mm tt")
If i execute my code in my development machine everything is working ok.
I'm using windows server 2012 and the last IIS 8.0 and i have just change the regional settings of my server to match my development machine so i'm reading in my server clock "12:00 PM"
I'm sure that this is some configuration i'm missing in the Server or in the IIS or maybe in the Web.config.
Any help or idea will be really appreciated.
Well, since you using DateTime.ToString() without any IFormatProvider, sounds like your CurrentCulture's AMDesignator or / and PMDesignator properties are empty string.
I would suggest a use InvariantCulture as a IFormatProvider like;
i.Date.ToString("hh:mm tt", CultureInfo.InvariantCulture);
Or change current culture that have non-empty AMDesignator and PMDesignator properties in your windows machine region settings.
I´m having trouble when working with dates in my Web MVC project. It is running in my local IIS.
The problem is, I live in Brazil, Rio de Janeiro, so according to a this my TimeZone is
E. South America Standard Time
That means Brasilia with GMT-3. And it is perfect and correct.
But when running my unit test in NCrunch, for the code above:
System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
var zone = TimeZone.CurrentTimeZone;
var time = zone.GetDaylightChanges(DateTime.Today.Year);
var start = time.Start;
var end = time.End;
var offset = zone.GetUtcOffset(DateTime.Now);
I ´m receiving the following results:
zone.DaylightName = "E. South America Daylight Time" (CORRECT)
time.Start = {18/10/2014 23:59:59} (CORRECT FOR 2014)
time.End = {15/02/2014 23:59:59} (CORRECT FOR 2014)
offset = {-2:00:00} (WRONT, it is -3) -> it would be right if there was no daylight saving time
TimeZone.IsDaylightSavingTime(DateTime.Now) = true
The problem is that when I send it to Postgres DB it saves as timestampZ so, it saves correct
for example 01/01/01 01:01:01 -3
But then when retrieving from database, it comes the mistake, because of the wrong offset I get
01/01/01 02:01:01 -3
It is runnin in local IIS but it should use the local computer time, right? If I go to my computer settings is is correct (Brasilia Time, GMT-3)
How should I proceed?
Thanks in regards,
Edit
Actually the problem is in my PostgresSQL. It is saving the current time with offset -3 instead of -2 and that is all the problem. When i get it back in server it adds one hour...
How do I enable DST in Postgres?!?