unix timestamp - json - help

447 views
Skip to first unread message

João Costa

unread,
Nov 21, 2014, 7:17:16 AM11/21/14
to tas...@googlegroups.com
hi
Using javascriptlet json to parse an api and it is returning a variable value unix timestamp as "1.41657e+09" exponential number, instead of "1416571161".

already tried js "new Date" and tasker's convert action (both as action and in js as secToDatetime) on the result but nothing comes through. The action fails with error message that the convert number failed.

Also tried "toString" inside js but it produces no result value.
I'm thinking about using a reviver on JSON, but not sure yet what it would validate or if it even works.

Any solution or ideas? thanks


More info:
I'm retrieving data from forecast.io api. All other variables are exporting correctly as far as I can tell.
Those variables are then exported to Zooper widget pro successfully.
Only problem is with the unix timestamp, that also gets exported but is unreadable as a time/date variable.
The unix timestamp is not essential to what I want to achieve but it's bugging me to no end that I can't manage to do it :(
I have very limited, or almost none knowledge in js. 

Pent

unread,
Nov 21, 2014, 7:45:52 AM11/21/14
to tas...@googlegroups.com
Sorry, not a JS coder.

Use the Tasker var %TIMEMS ?

Pent

João Costa

unread,
Nov 21, 2014, 7:56:32 AM11/21/14
to tas...@googlegroups.com
thanks but that does not solve my current problem. I forgot to mention that I need the times from the API itself (last time/hour the forecast was updated, forecast for x hour, and so on...)

but the %TIMEMS is in the same format unix timestamp, so it handles fine in tasker, must be a parsing error somehow

Pent

unread,
Nov 21, 2014, 8:58:10 AM11/21/14
to tas...@googlegroups.com
This topic has come up before.

Tasker does not do any numeric conversion, it stores everything as a string.

Whatever JS you are using is converting it before it's assigned to a variable.

Pent

João Costa

unread,
Nov 21, 2014, 10:39:47 AM11/21/14
to tas...@googlegroups.com
ok thanks.

I don't know js well, that's the prob. This probably a very basic noob error.
I'm leaving the code here so maybe someone can point me in the right direction regarding proper js.

info=JSON.parse(global("HTTPD"));
stores evrthg inside var info, and it's here the unwanted conversion happens... 


then I assigne vars for tasker:

setGlobal("CURRSUM", info.currently.summary);
setGlobal
("CURRICON", info.currently.icon);
setGlobal
("CURRTIME", info.currently.time)
and so on...

the vars %CURRSUM, %CURRICON, %CURRTIME etc all become usable in tasker actions, except for the time format problem.

Pent

unread,
Nov 21, 2014, 10:54:20 AM11/21/14
to tas...@googlegroups.com
I presume those are numeric fields. Try converting to a JS string first. I don't know how you
do that without the precision loss, maybe a math library or something.

Pent

João Costa

unread,
Nov 27, 2014, 11:21:20 AM11/27/14
to tas...@googlegroups.com
regarding this problem, I figured a workaround, I'll leave it here for future reference:
basically I'm relying on Variable Search Replace, with the search string \"time":[0-9]*

The full sequence is:
1. Read File (tasker's xml export from HTTP Get); 2. Variable Split (get rid of some unwanted info); 3. Variable Search Replace (store the timestamps in tasker variables); 4. Variable Section (isolate the unix timestamp within variables); 5. Variable Convert (Seconds to Date); 6. Zooper Widget Variable export.

It's a lot more work just to send time info, but it's the only way I had success exporting unix timestamps.

I'll just leave also my appreciation for Tasker. I just recently found this app and it is an amazing program, it's incredibly powerful and useful I'm still just starting to figure out how much can be achieved! 
Well worth the price, and so much fun to use. 
So thank you! 

Marcin Mally

unread,
Jul 8, 2015, 7:22:35 AM7/8/15
to tas...@googlegroups.com
Hi,
for me something like this worked to get correct format of time data from forecast.io:

firstly i had:

var weatherData = JSON.parse(wearherjson);
wtime = weatherData.currently.time;


and the variable wtime value was: 1.43635e+09


after changing to this:

var weatherData = JSON.parse(wearherjson);
wtime = (weatherData.currently.time).toString();


the variable wtime value now is: 1436351625


Reply all
Reply to author
Forward
0 new messages