DateTime in SQLite has AM/PM - c#

I have a database that is filled with log data by an external program.
One column is a timestamp of type DateTime.
Depending on the external programs host settings of time format, it either writes into the database using 24h or 12h format with AM/PM.
This query I used to get the time (on the same machine, but two database files from different external devices):
SELECT Time FROM tabData
The output (from a machine with AM/PM time setting)
2017-05-31 8:52:26 AM
and another (from a machine with 24h time setting)
2017-05-31 08:52:26
Why does the database datetime keep that 12h mode with AM/PM? Can I somehow change the format of the database datetime by SQL?
I further want to read these data into a DataTable in C#, and I'd prefer a SQL statement to read any given datetime as 24 hour datetime.

If SQLite is anything like MSSQL and Oracle, it doesn't have a Format for datetime except for its internal storage format that you shouldn't need to know anything about. The format you see only shows up when you use a Client to query that date, and then the actual format depends on the client you use, for instance general time settings chosen on the client machine.

Related

C# Datetime is increasing by 3 hours

My dates are being stored in sql server.
Windows server region and timezone are correct, but when I see the date in database it's increased by 3 hours, I think it is comming from some server configuration that is overriding, but don't know where else to look.
When I send date from local project to production database, it saves correctly, but when I use production website it doesn't.
Where else should I check to solve this?
It depends on the codebase running your production portal.
Use Datetime as utc and cater for it accordingly. Or add DatetimeOffset and calculate the difference when needed.
If you still have the luxury of refactoring, I advise you to take a little dive into the Dates and time handling, get a solid understanding of it, then code accordingly, so it doesn't fire back on you in later stages or usages.
The problem was in some machines of the load balance, there are several machines created from an image, the original was right but the copies were not, if I tried many times to save the registry, then I happened to be redirected to the machine with the correct configuration and then the record was saved with the correct date.
I just asked devops to correct the settings on the other machines.
If you want to get advantage of your local machine timezone you can use myDateTime.ToUniversalTime() to get the UTC time from your local time or myDateTime.ToLocalTime() to convert the UTC time to the local machine's time.
// convert UTC time from the database to the machine's time
DateTime databaseUtcTime = new DateTime(2011,6,5,10,15,00);
var localTime = databaseUtcTime.ToLocalTime();
// convert local time to UTC for database save
var databaseUtcTime = localTime.ToUniversalTime();
If you need to convert time from/to other timezones, you may use TimeZoneInfo.ConvertTime() or TimeZoneInfo.ConvertTimeFromUtc().
// convert UTC time from the database to japanese time
DateTime databaseUtcTime = new DateTime(2011,6,5,10,15,00);
var japaneseTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
var japaneseTime = TimeZoneInfo.ConvertTimeFromUtc(databaseUtcTime, japaneseTimeZone);
// convert japanese time to UTC for database save
var databaseUtcTime = TimeZoneInfo.ConvertTimeToUtc(japaneseTime, japaneseTimeZone);

Managing Time zone and Daylight Saving in SQL Server

We currently are trying to implement time zone management in our application. Our server is in India. We will be saving all entries in server time. But when coming to the client side, we need to show the data in the client time. So I came into this conclusion that getting the client time zone from the front end and converting the server time to client time using the code below in the database will solve the problem.
DECLARE #ServerTime DATETIMEOFFSET(7) = '2015-02-21 22:06:08.6862774 +05:30' -- My server time
DECLARE #ClientTime DATETIMEOFFSET(7) = '2015-02-21 12:38:09.5421899 -04:00' -- My Client Time
SELECT SWITCHOFFSET(#ServerTime, DATEPART(TZ, #ClientTime))
My question is
Is there any better option?
Can this be done from the front end (C#)?
Last and most important question is:
How can I manage if the client machine is in daylight saving?
Any support will be appreciated.
You can do this in a much simpler way in .NET, since .NET has some "automatic" mechanisms to deal with this.
For example: if your client app invokes a web service and sends as argument a DateTime in local time (date.Kind == DateTimeKind.Local) the date is serialized with the offset ("2015-08-19T14:21+01:00") on the server side the date will be deserialized and converted to the server's local time. For instance, using the date above and if the server is in UTC the DateTime object would have the value "2015-08-19 13:21:00" (however you will not have any information about the timezone where the client is and where the date was created). If the date is sent again to the client the inverse conversion is made and the date will have the original value on the client.
This way the server will always process dates using local time and the client will always process dates in local time, although they do not know the timezone of each other.
I believe this mechanism does not deal well with ancient past dates because of daylight saving time, this is probably being limited by the information windows has about daylight saving time (as an example for the timezone "E. South America Standard Time" windows 8 only has DST information from 2006 to 2040 meaning that dates previous to 2006 could be misunderstood).
Having said that, I believe the best solution to deal with different timezones is to use UTC and convert to local time when displaying the date to the user, or always use DateTimeOffset instead of DateTime (DateTimeOffset also has information about the date timezone).
You can use:
SELECT CONVERT(datetime,'2019-03-11 11:59:59')
AT TIME ZONE 'UTC'
AT TIME ZONE 'US Eastern Standard Time';
It also handles daylight saving time.
While it is better to do this in client code, if you decide you need to work with time zones directly in SQL Server, you can now use my SQL Server Time Zone Support project.
Example:
SELECT Tzdb.UtcToLocal('2015-07-01 00:00:00', 'America/Los_Angeles')

Why does date format changes to month first in date time object?

In a web application, I am passing datetime without formatting it in a XML response, just item.Date (DateTime datatype) is put in code.
No formatting is done. When I run the local server it returns the date in MM/DD/YYYY format and in live environment DD/MM/YYYY. Why this change is happening?
I checked on database collation and OS settings. Live environment had English (Singapore) in regional settings. However after changing local servers to English (Singapore) still live environment DateTime format is not being produced locally.
Collation:
In Live Environment - Latin1_General_CI_AI
In Local Servers - SQL_Latin1_General_CP1_CI_AS
What could be the reason? how to resolve this rather than doing formatting in XML?
Update
As suspected, Issue is with collation, regenerated the issue locally after creating DB with collation in live environment.
Windows stores Date/Time settings per user. If IIS is running as LocalSystem, then you'll need to change the settings of the Default user:
In regedit, go here and set the appropriate values up for the locale and format that you want.
HKEY_USERS\.DEFAULT\Control Panel\International
That said, handling dates and times types under the assumption of any given format is A Really Bad Idea.
If you need date/time in a specific format for display, interop or whatever you should format the date time appropriately with one of the available string formats.

Time conversion to client time c#

I have one application which need server to process the client request.
In this application time is very important.
Whenever client request anything, i would like to save the time of the request.
Problem is, my server is in US and client is in australia.
How can i change server time to client time and save it on database.
This should be accurate even during day light saving.
How can i achieve this
Ok i have save time in database as UTC.
On client side i have this code,
DateTime dt = booking.CreateDateTime.Value;
var localTime = TimeZone.CurrentTimeZone.ToLocalTime(dt);
When i print that localTime, it is 7 hour faster then local time.
How can i change that time to local time?
The guideline I know states that times should always be saved as UTC in the database, never local. This way you avoid many local time difference pitfalls (including daylight savings).
When you need the local time, retrieve it as UTC from the database and convert it. You can use the DateTime struct to help you out with this:
var utcNow = DateTime.UtcNow;
SaveToDB(utcNow);
var utcFromDb = RetrieveTimeFromDb();
var localTime = DateTime.ToLocalTime(utcFromDb);
It's typically best practice to store dates in UTC, and then translate to a locale when necessary.
var date = DateTime.UtcNow();
var tzi = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
var offset = tzi.GetUtcOffset(date);
Maybe take a look at the TimeZoneInfo class?
If that doesn't satisfy your needs, there's Noda Time.
Agreed that the time should be stored in UTC. The problem will comein when converting to the client's local time (not the server's). To convert to the client's local time, you will need to know the client's time zone information and store this with the UTC time in the db.
Then with both pieces of information you should be able use the above methods to convert to the client's local time.

Timezone confusion

I have a form that allows users to select a start and an end date. The start and end dates will always be in entered with PST timezone. When saving the start and end dates I convert them to UTC and store them in a database.
The Web Server has a component that checks against these dates and resides in an EST timezone. I can't wrap my head around dates.
A simplified explanation:
Data entered in PST.
Stored in DB in UTC.
Web Server hosted in EST
How to correctly handle the PST timezone?
If the Web Server is checking them in local time - meaning that the dates will be in EST, then to get your PST time, you would just need to subtract 3 hours.
However, since they are in UTC, you could just specify the time-zone when pulling from the DB.
Pass culture info in to the DateTime e.g
DateTime A = (DateTime)DT["MyDate"];System.Globalization.CultureInfo CI =
new System.Globalization.CultureInfo("YourCultureInfoCode");

Categories

Resources