-3

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?

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
user3786134
  • 361
  • 1
  • 6
  • 21

3 Answers3

2

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

Community
  • 1
  • 1
Eric
  • 19,525
  • 19
  • 84
  • 147
  • what can i do for my whole program? – user3786134 Nov 02 '14 at 07:44
  • 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
0

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

Community
  • 1
  • 1
Chief Wiggum
  • 2,784
  • 2
  • 31
  • 44
0

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

Community
  • 1
  • 1
Hardik Parmar
  • 1,053
  • 3
  • 15
  • 39