I am developing a windows application using C# and SQL
and i want to store the time of some specific events in the db , i tried using the C# Datetime
class but when i change my windows local time it does not give me the real date and then i thought maybe SQL
has it's own date system,i searched but nothing comes up so what is the best way to have a secure time system? which the user can not easily manipulate the time that is storing in the data base?

- 20,441
- 9
- 58
- 74

- 361
- 1
- 6
- 21
-
Is your database on a separate server or the same machine as the application? – jmcilhinney Nov 02 '14 at 07:27
-
What do you mean by *"secure"*? – Keith Payne Nov 02 '14 at 07:28
-
it is just on a machine – user3786134 Nov 02 '14 at 07:33
-
and all of the machines are offline – user3786134 Nov 02 '14 at 07:34
-
i realy don't understand the downvote ! – user3786134 Nov 02 '14 at 07:51
3 Answers
Usually users cannot change the system time without administrator privileges. If your code is running on a untrusted machine where the user is admin, then you need another source of time. (Admittedly, your whole program can be compromised but this is another topic).
You can query MSSQL for current time as described here
SELECT CURRENT_TIMESTAMP
GO
SELECT {fn NOW()}
GO
SELECT GETDATE()
GO
Another way would be to query a NTP server somewhere else on the internet. This requires network programming. There is this SO question about it here
-
-
The real answer is that you cannot do anything. This is the whole point of user accounts and privileges in any operating system. The user can mess with your binaries, open and modify them. There are multiple ways to make their life harder if they try that. I don't think you have to bother that much about it for now as it requires quite advanced hacking skills. It really depends on who you want to defend against. – Eric Nov 02 '14 at 07:47
If you don't trust your workstation or the server time you can get the time from a NTP server and if that's not available still fall back to the local clock. An short example how to get the current time from an NTP server can be found here https://stackoverflow.com/a/12150289/2360972

- 1
- 1

- 2,784
- 2
- 31
- 44
I am actually very late for this answer. I also had done lots of search for this answer & finally I got the best solution for this.
UTC is thestandard format to store your datatime in your application.
What is UTC?
UTC, or Coordinated Universal Time, is the standard international time that all time zones are expressed as offsets of. UTC does not get adjusted for daylight savings. To compute local time from UTC, simply add the time zone offset and then add an additional hour if daylight savings time is in effect.
Please Refer this link Your all doubts will get clear after refering this : Click To refer
UTC
time will always take server datetime & will not take your system datetime.
Also, If your application is running on different country then there is only one way to remove conflict between Time is to use UTC
This is very good Example for time related Issues : Click Here

- 1
- 1

- 1,053
- 3
- 15
- 39
-
@user3786134 I know I am very late But just wanted to share this issue with you – Hardik Parmar Nov 02 '14 at 08:02