GetNIST

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

GetNIST

Post by JRL » Tue Jun 11, 2013 3:33 pm

Here's a script to get the current time from NIST. Anyone coded a better way? There's always a better way.

Code: Select all

Let>vMyOffset=-5

DeleteFile>%temp_dir%Time.txt
HTTPRequest>http://www.time.gov/timezone.cgi?UTC/s/0/java,%temp_dir%Time.txt,Get,,ResVar,,,,
ReadFile>%temp_dir%Time.txt,vData
Separate>vData,NISTSendTime = new Date( ",vTimeItem
Separate>vTimeItem_2,"),vTimeItem
VBSTART
VBEND
VBEval>FormatDateTime("%vTimeItem_1%"),vFormattedTime
StringReplace>vFormattedTime,/,-,vFormattedTime
VBEval>(DateAdd("h",%vMyOffset%,"%vFormattedTime%")),vMyTime

MDL>vMyTime

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Tue Jun 11, 2013 4:19 pm

I don't have a better way.

This is one of my old script I dug up (Nothing special) that retrieves Universal Time, Eastern Time, Central, Mountain, Pacific, Alaska, and Hawaii-Aleutian Time from the US Naval Observatory Master Clock.

Code: Select all

HTTPRequest>http://tycho.usno.navy.mil/timer.html,,GET,,strHTML,,,,
Position><PRE>,%strHTML%,1,Pos1
Add>Pos1,5
Position></PRE>,%strHTML%,1,Pos2
Sub>Pos2,%Pos1%
MidStr>%strHTML%,%Pos1%,%Pos2%,strHTML
StringReplace>%strHTML%,<BR>,CRLF,strHTML
StringReplace>%strHTML%,%TAB%%TAB%,%TAB%,strHTML
Trim>%strHTML%,strHTML
mdl>US Naval Observatory Master Clock Time%CRLF%%strHTML%

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jun 11, 2013 4:49 pm

I don't have a better way.
Well.. yes you do. I didn't realize that if you don't put a file name in the HTTPRequest "local file" parameter, the website data will show up in the result variable. That saves a couple of lines. We're all about saving lines.

Here's a variation that will set your computer to the date and time found on time.gov. Note the use of result variable in this one...

Be sure to set the "vMyOffset" variable to your correct timezone offset.

Thinking of how to make this adjust automatically for DST vs standard time.

Code: Select all

Let>vMyOffset=-5

HTTPRequest>http://www.time.gov/timezone.cgi?UTC/s/0/java,,Get,,vData,,,,
Separate>vData,NISTSendTime = new Date( ",vTimeItem
Separate>vTimeItem_2,"),vTimeItem
VBSTART
VBEND
VBEval>FormatDateTime("%vTimeItem_1%"),vFormattedTime
StringReplace>vFormattedTime,/,-,vFormattedTime
VBEval>(DateAdd("h",%vMyOffset%,"%vFormattedTime%")),vMyTime
DeleteFile>%temp_dir%Time.txt

Separate>vMyTime, ,vDatePart
StringReplace>vDatePart_1,/,-,vDatePart_1
Let>RP_Wait=1
Let>RP_Windowmode=0
RunProgram>cmd /c Echo %vDatePart_1%>%temp_dir%Time.txt
RunProgram>cmd /c date < %temp_dir%Time.txt

If>vDatePart_3=PM
  Separate>vDatePart_2,:,vTimePiece
  Add>vTimePiece_1,12
  Let>vDatePart_2=%vTimePiece_1%:%vTimePiece_2%:%vTimePiece_3%
EndIf

RunProgram>cmd /c Echo %vDatePart_2%>%temp_dir%Time.txt
RunProgram>cmd /c time < %temp_dir%Time.txt
DeleteFile>%temp_dir%Time.txt

MDL>Date / Time has been reset to:%crlf%%vDatePart_1%   %vDatePart_2%

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Wed Jun 12, 2013 1:56 am

Is there a certain format you want the date & time displayed?

Eg I currently have the below code & it returns the date and time as: Tuesday, June 11, 2013 - 21:18:56

Code: Select all

Let>COMMA=,
HTTPRequest>http://www.time.gov/timezone.cgi?Central/d/-6,,Get,,ResVar
Let>text=%ResVar%
Let>pattern=(?:2[0-3]|[01][0-9])[:.][0-5][0-9][:.][0-5][0-9]|(?:Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday),[ \t]+(?:January|February|March|April|May|June|July|August|September|October|November|December)[ \t]+(?:1[0-2]|[1-9]),[ \t]+[0-9]{4}
RegEx>pattern,text,0,Matches,NumMatches,0,,
MDL>%matches_3% - %matches_2%
Formatted in the style you listed (6-11-2013 10:14:13 PM)

Code: Select all

Let>COMMA=,
HTTPRequest>http://www.time.gov/timezone.cgi?Central/d/-6,,Get,,ResVar
Let>text=%ResVar%
Let>pattern=(?:2[0-3]|[01][0-9])[:.][0-5][0-9][:.][0-5][0-9]|(?:January|February|March|April|May|June|July|August|September|October|November|December)[ \t]+(?:1[0-2]|[1-9]),[ \t]+[0-9]{4}
RegEx>pattern,text,0,Matches,NumMatches,0,,
StringReplace>%matches_3%,%COMMA%,,matches_3
Let>DandT=%matches_3% %matches_2%
VBSTART
VBEND
VBEval>FormatDateTime("%DandT%"),vFormattedTime
StringReplace>vFormattedTime,/,-,vFormattedTime
MDL>%vFormattedTime%
FIREFIGHTER

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Jun 12, 2013 3:25 am

Cool! Regex... Another improvement.
Is there a certain format you want the date & time displayed?
If the date is formatted mm-dd-yy and the time is hh:mm:ss then you can take the result straight from the regex and plug it into DOS to reset your computer's date/time.

I noticed you put the central time zone directly into the html. I'm pointing that out for the less experienced. The reason I made a time offset variable and placed it at the top of the scripts, then had the html grab universal time and added the offset on to the UTC was to make it easier and more obvious for novice scripters. As posted your script works for me but hardly anyone else. Certainly doesn't work for you... Are we about 12 hours different?

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Wed Jun 12, 2013 3:31 am

JRL wrote:If the date is formatted mm-dd-yy and the time is hh:mm:ss then you can take the result straight from the regex and plug it into DOS to reset your computer's date/time.
Can't You Just Use Windows Update For That Though?
JRL wrote:I noticed you put the central time zone directly into the html.
Correct, I was wondering why you chose to do it that way and not link directly to GMT, however I see you explained that.
JRL wrote:As posted your script works for me but hardly anyone else. Certainly doesn't work for you... Are we about 12 hours different?
Correct in which case I would change the URL to reflect my timezone (+9:30).

Yeah after a comment about RegEx I thought I would look into it a bit more, so this was more of a practice as such. I must say the application RegEx Magic helps alot, although still alot to learn.
FIREFIGHTER

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts