Ad Widget

Collapse

UTC Time string conversion problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aratom
    Junior Member
    • Oct 2022
    • 6

    #1

    UTC Time string conversion problem

    Hey there!

    I'm using the HTTP agent to get the headers of a file, like curl -I https://example.com which works fine. The answer is successfully converted to JSON.

    My idea was using a preprocessing step to extract the "Last-Modified" from JSON. Works fine.

    Where I fail is using JS to convert the received string like
    Code:
    Thu, 26 Jan 2023 11:12:13 GMT
    I used
    Code:
    return Date.parse(value)
    , which works awesome with my dev environment in VS Code.

    After heavy searching and testing I figured that with the given duktape implementation Date.now() is implemented but the Date.parse() is not fully implemented, because I can convert a time in the format of
    Code:
    YYYY-MM-DDTHH:mm:ss.sssZ
    I was wondering whether someone had an idea on how to solve this problem or some way around it.

    Thank you!
    Thomas
  • cyber
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Dec 2006
    • 4807

    #2


    says that
    Date.parse from a string


    The method Date.parse(str) can read a date from a string.

    The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ, where:
    • YYYY-MM-DD – is the date: year-month-day.
    • The character "T" is used as the delimiter.
    • HH:mm:ss.sss – is the time: hours, minutes, seconds and milliseconds.
    • The optional 'Z' part denotes the time zone in the format +-hh:mm. A single letter Z would mean UTC+0.
    ​So, why do you think, that it should be able to read any given format? Maybe your VSCode has some fancy plugin there?

    Comment

    • aratom
      Junior Member
      • Oct 2022
      • 6

      #3
      thank you cyber for the quick reply.

      It seems that depending on the implementation this functionality is available or not.

      Luckily, after uncounted hours of research, I found this

      https://stackoverflow.com/a/22186399

      ​var string = "Tue, 04 Mar 2014 21:35:31 GMT"
      var parts = string.match(/(\w*), (\d{2}) (\w*) (\d{4}) (\d{2})\\d{2})\\d{2})/);
      var month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf (par ts[3]) / 3 + 1
      td = new Date(parts[4], month, parts[2],parts[5], parts[6], parts[7])
      td.getTime()​

      For me this works, beautiful code is something else. Interestingly I had some trouble with websites answering with "Last-Modified" and some with "last-modified" therefore this weird check.

      Code:
      var json = JSON.parse(value)
      
      var string = "Tue, 01 Jan 1970 10:00:00 GMT"
      
      if(JSON.stringify(json.header["Last-Modified"]))
          string = JSON.stringify(json.header["Last-Modified"])
      else if (JSON.stringify(json.header["last-modified"]))
          string = JSON.stringify(json.header["last-modified"])
      else
          return 0;
      
      // https://stackoverflow.com/a/22186399
      // var string = value;
      var parts = string.match(/(\w*), (\d{2}) (\w*) (\d{4}) (\d{2})\:(\d{2})\:(\d{2})/);
      var month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(parts[3]) / 3 + 1
      
      var date = new Date(parts[4], month, parts[2],parts[5], parts[6], parts[7])
      return (Date.parse(date)/1000)​

      Comment

      Working...