This question already has answers here:
Compare DateTime ticks on two machines
(5 answers)
Closed 3 years ago.
In one of my code I am doing so that, there is a process running different systems and I save some data to database with that I saved field DateTime with value DateTime.UtcNow (C#) , and then one of the systems I collect all the data from different systems then order it by DateTime field, to order the data has been saved.
In this I also check if SystemA written data DateTime field is greater than SystemB written data DateTime field and then which ever data is latest I save that.
Now, I would like to know whether this kind of DateTime comparision from different systems is a bad idea?
I am not considering for scenarios where the system clock are very wrong.
The local clock on a PC is not that accurate. Its entirely possible that comparing two timestamps between systems will result in incorrect results. Instead have the database apply the timestamp from its own server as it writes the record.
Related
This question already has answers here:
How to Query an NTP Server using C#?
(6 answers)
Closed 3 years ago.
I need to get the same time on all Instances of my program. How do I do that. DateTime.Now is not accurate(is different on different hardware) enough, I need to get the Time down to 100 ms difference-precision.
You don't want it read locally from each computer, as you don't know that each PC's clock is perfectly in sync (and you can see that they aren't).
So you have two options:
Write something on each computer to maintain precise time.
Get a web based time and all of your computers will be reading the same data. Here is an example from SO: How to get DateTime from the internet?
This question already has answers here:
What is the correct SQL type to store a .Net Timespan with values > 24:00:00?
(10 answers)
Closed 5 years ago.
I have class which has property type of TimeSpan.
At some point I need to store value properties in database.
For this purpose I need to create appropriate table in database so i wonder what is appropriate type in SQL for C# type TimeSpan?
There's no direct mapping, but it would probably make the most sense to use a bigint column and store the Ticks property of TimeSpan.
To store data into the column, use TimeSpan.Ticks. To extract data out of the column, use new Timespan(ticks).
It depends on how precise you want to be: seconds, milliseconds, minutes, hours. Whatever works. I suppose you could do ticks if you need that kind of precision.
This question already has answers here:
How to get client date and time in ASP.NET?
(6 answers)
How to detect the timezone of a client?
(4 answers)
Closed 6 years ago.
I am using ASP.NET Webforms to get the data of client. till now I figured out the IP address of client and the city & country of the client. I am trying to find a way of how to retrieve the time zone by using its IP Address or country name or city name. I have googled and couldn't find any appropriate answer as everyone talks about injecting javascript to get timezone.
My question is: Is there any way to find timezone of the client? If so then how?`
It is not possible to do this in a reliable way purely with C#. The HTTP spec does not provide any hints of the client's time zone.
You can guess at a client's time zone by using IP geolocation, and there are many free and commercial services that provide time zone information. However there is only a limited degree of accuracy with such an approach.
Even with JavaScript, the current solutions can still only guess the client's time zone. There are improvements happening for this with in the ECMAScript Intl API, but not all browsers fully implement that yet. See more here.
Ultimately, if you need to know the time zone of the client from your back end code, the best thing to do is to ask the user. Provide a time zone picker somewhere in your application.
You may also find this related answer useful.
Have you tried using the TimeZoneInfo Class
It represents any time zone in the world.
https://msdn.microsoft.com/en-us/library/system.timezoneinfo(v=vs.110).aspx
This question already has answers here:
How to handle Azure's UTC time
(2 answers)
Closed 8 years ago.
I am currently saving the datetime.utcnow in the database on azure but im not sure how to display the correct time depending on the client.
Should I be using utc?
Should I save the timezone so I can recalculate the time?
Can I use the culture to change the time to the client time?
Your best option is to store the DateTime as UTC time and adjust it to local time (for the user's current location) each time you read it from the database.
You can use JavaScript to determine the timezone of the user:
new Date().gettimezoneOffset()
This question already has answers here:
How to best handle the storage of historical data?
(2 answers)
Closed 8 years ago.
What is the best practice of keeping past data in the database? For example lets say we have transaction tables like AttendanceTrans, SalaryTrans in a payroll solution. So every month we have to insert hundreds or thousands of new records to these tables. So all the past and current data in same table.
Another approach would be to keep AttendanceHistory and SalaryHistory tables. So that end of every period (month) we empty the Trans tables after coping the data to respective History tables.
When considering factors like performance, ease of report generation, ease of coding and maintenance, what would be the optimum solution?
Note : RDBMS is SQL Server 2008 and programming environment is .NET (C#)
In general you should keep all the data in the same table. SQL Server is great at handling large volumes of data and it will make your life a lot easier (reporting, querying, coding, maintenance) if it's all in one place. If your data is indexed appropriately then you'll be just fine with thousands of new records per month.
In my opinion, best solution in sql server is CDC (Change Data Capture). It very simple to use. You can change volume of historical data with changing schedule of clear job.
I think this is the best way for performance because CDC gets changes from transaction log (it is not triggers on table), but you need to use Full Recovery Model for you database.