Posted By

Scooter on 11/15/08


Tagged

DateTime atomic


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wizard04


UTC and Atomic Time


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2008/11/utc-and-atomic-time.html

How to obtain UTC time values and use them to make a more accurate local time value.

  1. <%
  2. ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  3. '
  4. ' This work is licensed under the Creative Commons Attribution License. To view
  5. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  6. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  7. ' 94305, USA.
  8.  
  9. ' Returns the current date and time (UTC) from the NIST server in Boulder, Colorado.
  10. function utcnow()
  11. dim xmlhttp
  12. dim response
  13.  
  14. ' Server to query datetime from
  15. Const TimeServer = "http://time.nist.gov:13"
  16.  
  17. ' Use XML HTTP object to request web page content
  18. Set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP")
  19. xmlhttp.Open "GET", TimeServer, false, "", ""
  20. xmlhttp.Send
  21. response = xmlhttp.ResponseText
  22. set xmlhttp = nothing
  23.  
  24. ' Parse UTC date
  25. utcnow = cDate(mid(response, 11, 2) & "/" & mid(response, 14, 2) & "/" & mid(response, 8, 2) & " " & mid(response, 16, 9))
  26. end function
  27.  
  28. ' Returns the current date and time, offset to local time zone, from the NIST server in Boulder, Colorado.
  29. ' This is more accurate than VBScript's built-in Now() function in situations where the local server is not synchronized.
  30. ' There is expected to be some lag caused by this function, but the order of magnitude should only be milliseconds.
  31. ' REQUIRES: utcnow()
  32. function atomicnow()
  33. dim utc
  34. dim offset
  35.  
  36. utc = utcnow()
  37.  
  38. ' The order of the dates is important here!
  39. offset = DateDiff("h", utc, now())
  40.  
  41. atomicnow = DateAdd("h", offset, utc)
  42. end function
  43. %>

Report this snippet  

You need to login to post a comment.