viewpoint-particle

Author Topic: Get Date Time on File at web link  (Read 101 times)

limnos

  • Newbie
  • *
  • Posts: 17
Get Date Time on File at web link
« on: November 19, 2018, 08:11:41 am »
I got that code to work in copying down that zip file from the web link, but when I extract it, all the files have the current date\time stamp.  I guess I should have stated what I need to do.  I need to compare the date\time of that web based zip file, if it's newer than a local one, I have to copy it down and extract it out.  So, we've got the copying working.  However, I can't seem to figure out what com object to use to get a friendly date\time from that file first.  I found this code, and it works...

loXmlHttp = ObjectCreate("MSXML2.XMLHTTP")
loXmlHttp.Open("HEAD", "https://xx.xx.xx.gov/Content/doc/xx/Stnd/xx.zip", @FALSE)
loXmlHttp.Send()
loXmlHttp.getallresponseheaders()
servertime=loXmlHttp.getResponseHeader('Last-Modified')

Problem is, this returns the date\time in an awful java format (Tue, 13 NOV 2018 20:11:31 GMT) which is E, dd MMM yyyy HH:mm:ss z.  There's no reasonable way to convert that I know of (Yeah, I can do it with a bunch of code, ugh), but is there a better object to use to get that file time\date stamp in a more winbatch friendly format right up front, more akin to the FileTimeGeteEx (YYYY:MM:DD:HH:MM:SS)?

limnos

  • Newbie
  • *
  • Posts: 17
Re: Get Date Time on File at web link
« Reply #1 on: November 19, 2018, 08:50:31 am »
I found this framework code to deal with that, but I can't seem to get the right object call to make it work in WB or convert it to WB friendly calls.

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            string path = @"c:\Temp\MyTest.txt";
            if (!File.Exists(path))
            {
                File.Create(path);
            }
            else
            {


            // Get the creation time of a well-known directory.
            DateTime dt = File.GetLastWriteTime(path);
            Console.WriteLine("The last write time for this file was {0}.", dt);

        }

        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

td

  • Tech Support
  • *****
  • Posts: 2653
    • WinBatch
Re: Get Date Time on File at web link
« Reply #2 on: November 19, 2018, 09:15:48 am »
There are many ways to work with js time.  You could take the reverse approach and convert WIL YMDHMS time to js time.  For example,

Code: Winbatch
;; Convert current time to js time.
stJsTime = TimeFormat(TimeYmdHms(), "ddd', 'dd MMM yyyy HH':'mm':'ss 'GMT'")
 

For comparison purposes, you would need to convert the YMDHMS time to UTC before calling TimeFormat and you might consider verifying that above date format is compliant with the js spec.

Another approach is to exhaust your fingers writing a three line script to convert js time to YMDHSM time.

Code: Winbatch
;; Ouput time is UTC
strJsTime = 'Tue, 13 NOV 2018 20:11:31 GMT'
strJsTime = ItemExtract(2, strJsTime, ',')
strJsTime = ItemRemove(-1, strJsTime, ' ')
strYMDHMS = ObjectType('date', strJsTime)
 

Again you would need to convert the file time to UTC before comparison.  There are several recent examples of offsetting WIL YMDHMS time to and from UTC on the forum.


"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 808
Re: Get Date Time on File at web link
« Reply #3 on: November 19, 2018, 11:52:59 am »
I think in your 2nd example you meant to say convert to local, because aren't UTC and GMT the same (except for a few milliseconds).

td

  • Tech Support
  • *****
  • Posts: 2653
    • WinBatch
Re: Get Date Time on File at web link
« Reply #4 on: November 19, 2018, 01:38:37 pm »
No, I meant UTC.  The file time returned by the WIL FileTime* functions is local and that time would need to be converted to UTC before comparing that file time to the time string created by the example (which is UTC/GMT.)

I shoulda made that clearer.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 808
Re: Get Date Time on File at web link
« Reply #5 on: November 21, 2018, 05:05:49 am »
OK. To make things clearer to me, if I received a string with 'GMT' I might try below:


Code: Winbatch

#definefunction UTCToLocal(_YmdDate)
   nTimeBias = RegQueryDword(@REGMACHINE, "System\CurrentControlSet\control\TimeZoneInformation[ActiveTimeBias]")
   if nTimeBias > 0 then return TimeSubtract(_YmdDate,'00:00:00:00:':nTimeBias:':00')
   else if nTimeBias < 0 then return TimeAdd(_YmdDate,'00:00:00:00:':Abs(nTimeBias):':00')  
   return _YmdDate
#endfunction


;; Time String is GMT/UTC
strJsTime = 'Tue, 13 NOV 2018 11:11:31 GMT'
strJsTime = ItemExtract(2, strJsTime, ',')
strJsTime = ItemRemove(-1, strJsTime, ' ')
t1 = ObjectType('date', strJsTime)
;convert to Local
t2= UTCToLocal(t1)
Message("GMT To Local","GMT ":t1:@CRLF:"Local ":t2)
Exit
 

td

  • Tech Support
  • *****
  • Posts: 2653
    • WinBatch
Re: Get Date Time on File at web link
« Reply #6 on: November 21, 2018, 07:26:42 am »
Obviously, you can go either way.  You can convert your web time to local time or the file time of the file on the local system to UTC. 

Code: Winbatch
;; Returns input YMDHMS time converted to UTC.
#definefunction TimeUTC(_YmdDate)
   nTimeBias = RegQueryDword(@REGMACHINE, "System\CurrentControlSet\control\TimeZoneInformation[ActiveTimeBias]")
   if nTimeBias > 0 then return TimeAdd(_YmdDate,'00:00:00:00:':nTimeBias:':00')
   else if nTimeBias < 0 then return TimeSubtract(Y_YmdDate,'00:00:00:00:':Abs(nTimeBias):':00')  
   return _YmdDate
#endfunction


;; Convert Web javascipt formated to YMDHMS.   It is UTC.
strJsTime = 'Tue, 13 NOV 2018 20:11:31 GMT'
strJsTime = ItemExtract(2, strJsTime, ',')
strJsTime = ItemRemove(-1, strJsTime, ' ')
strYMDHMSWeb = ObjectType('date', strJsTime)

;Get the modify date of a local file.
strYMDHMSFile = FileTimeGetEx('C:\Temp\dummy.txt', 2)

; Convert file's modify date to UTC for comparison purposes.
strYMDHMSFile = TimeUTC(strYMDHMSFile)

; Compare times.
nDiff = StrCmp(strYMDHMSWeb, strYMDHMSFile)
if nDiff > 0 then strText = 'Web file is newer'
else if nDiff < 0 then strText = 'Local file is newer'
else strText = 'Files are the same age'

Message('File Time Comparison Results', strText)

GMT is a time zone that happens to be UTC 0 offset while UCT is an international time standard.  GMT is only used for part of the year in Great Britain.  Great Britain switches to BST during the summer.

Correction: it should be "The United Kingdom" instead of "Great Britain". Lest my Northern Ireland kin never forgive me...
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 808
Re: Get Date Time on File at web link
« Reply #7 on: November 21, 2018, 12:06:44 pm »

Correction: it should be "The United Kingdom" instead of "Great Britain". Lest my Northern Ireland kin never forgive me...


Guess you'll have a write a Brexit() function.


Code: Winbatch


#DefineFunction Brexit(pounds)
   Return(dollars)
#EndFunction


retval = Brexit(50)


;returns $.50


 

td

  • Tech Support
  • *****
  • Posts: 2653
    • WinBatch
Re: Get Date Time on File at web link
« Reply #8 on: November 21, 2018, 01:31:32 pm »
At least, a trip to Belfast might get affordable.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates