Jump to content

Recommended Posts

Hello, I'm newbie in AutoIT. I'm trying to get the date and time from a web service which returns the current local time for a given timezone as either plain-text or JSON.

Here is the website: http://worldtimeapi.org/api/timezone/Asia/Bangkok

JSON: 

{"abbreviation":"+07","client_ip":"183.81.120.244","datetime":"2021-07-23T14:00:22.225651+07:00","day_of_week":5,"day_of_year":204,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":25200,"timezone":"Asia/Bangkok","unixtime":1627023622,"utc_datetime":"2021-07-23T07:00:22.225651+00:00","utc_offset":"+07:00","week_number":29}

Here is what I found

Local $String = BinaryToString(INetRead ("http://worldtimeapi.org/api/timezone/Asia/Bangkok",1))
$Obj = Json_Decode($String)
$datetime = functionOfMyDreams($Obj, "" ,"datetime")
ConsoleWrite($datetime)

Func functionOfMyDreams($decodedObject, $targetObject, $targetKeyInObject)
    $value = Json_Get($decodedObject, '["' & $targetObject & '"]["' & $targetKeyInObject & '"]')
    return $value
EndFunc   ;==>functionOfMyDreams

The UDF: https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/

Link to post
Share on other sites

@dphuc23

With a different approach, this would be helpful:

#include <Array.au3>
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>


MsgBox($MB_ICONINFORMATION, "GetDateTimeFromWorldTimeAPI", GetDateTimeFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok"))
_ArrayDisplay(GetDateTimeArrayFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok"), "GetDateTimeArrayFromWorldTimeAPI")


Func GetDateTimeFromWorldTimeAPI($strAddress)
    Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"([^"]+)"', $STR_REGEXPARRAYMATCH)[0]
EndFunc

Func GetDateTimeArrayFromWorldTimeAPI($strAddress)
    Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2}\.\d+)([+-]\d+:\d+)"', $STR_REGEXPARRAYMATCH)
EndFunc

Cheers ^_^

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to post
Share on other sites
1 minute ago, FrancescoDiMuro said:

@dphuc23

With a different approach, this would be helpful:

#include <Array.au3>
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>


MsgBox($MB_ICONINFORMATION, "GetDateTimeFromWorldTimeAPI", GetDateTimeFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok"))
_ArrayDisplay(GetDateTimeArrayFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok"), "GetDateTimeArrayFromWorldTimeAPI")


Func GetDateTimeFromWorldTimeAPI($strAddress)
    Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"([^"]+)"', $STR_REGEXPARRAYMATCH)[0]
EndFunc

Func GetDateTimeArrayFromWorldTimeAPI($strAddress)
    Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2}\.\d+)([+-]\d+:\d+)"', $STR_REGEXPARRAYMATCH)
EndFunc

Cheers ^_^

Thanks :D

Link to post
Share on other sites
5 hours ago, dphuc23 said:

The UDF: https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/

The one I've been using is this one, which includes a handy function named json_dump. This function lists the keys and their values to the console.

This --

Local $String = BinaryToString(INetRead ("http://worldtimeapi.org/api/timezone/Asia/Bangkok",1))
json_dump($String)

gives this output --

+-> .abbreviation  =+07
+-> .client_ip  =68.1.xx.xx
+-> .datetime  =2021-07-23T19:29:33.452612+07:00
+-> .day_of_week  =5
+-> .day_of_year  =204
+-> .dst  =False
+-> .dst_from  =Null
+-> .dst_offset  =0
+-> .dst_until  =Null
+-> .raw_offset  =25200
+-> .timezone  =Asia/Bangkok
+-> .unixtime  =1627043373
+-> .utc_datetime  =2021-07-23T12:29:33.452612+00:00
+-> .utc_offset  =+07:00
+-> .week_number  =29

So using this UDF, your code would look like this --

Local $String = BinaryToString(INetRead ("http://worldtimeapi.org/api/timezone/Asia/Bangkok",1))
$Obj = Json_Decode($String)
$datetime = functionOfMyDreams($Obj, "datetime")
ConsoleWrite($datetime & @CRLF)

Func functionOfMyDreams($decodedObject, $targetKeyInObject)
    $value = Json_Get($decodedObject, '["' & $targetKeyInObject & '"]')
    return $value
EndFunc   ;==>functionOfMyDreams

 

Edited by Danp2
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...