HTTP JSON API SERVER and Times are not equal
I finished the last challenge with this error
1. ACTUAL: "{\"hour\":23,\"minute\":1,\"second\":10}"
1. EXPECTED: "{\"hour\":22,\"minute\":59,\"second\":29}"
2. ACTUAL: "{\"unixtime\":1456083070189}"
2. EXPECTED: "{\"unixtime\":1456082969439}"
And I've solved the problem as below
var http = require('http');
var url = require('url');
var querystring = require('querystring');
//Port number from program first arguman
var port = +process.argv[2];
//creating an http server
var server = http.createServer(function(req, res) {
//Writing header for json data format
res.writeHead(200, {'Content-Type' : 'application/json'});
var parsedUrl = url.parse(req.url);
var pathname = parsedUrl.pathname;
var query = querystring.parse(parsedUrl.query);
if(pathname === "/api/parsetime" && query.iso !== "") {
var dateTime = new Date();
var hr = dateTime.getHours(),
sc = dateTime.getSeconds(),
min = dateTime.getMinutes();
var time = {
hour: hr,
minute: min,
second: sc
};
res.write(JSON.stringify(time));
res.end();
}
else if(pathname === "/api/unixtime" && query.iso !== "") {
var dateTime = new Date();
var time = { unixtime : dateTime.getTime() };
res.write(JSON.stringify(time));
res.end();
}
});
server.listen(port);
So what is the reason for the about two minutes difference ?
Same here, but only the unixtime doesn't quite work. Does this have something to do with me living in a different timezone (Belgium, so UTC + 1)?
url = require('url');
http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'application/json' });
var urlQuery = url.parse(req.url, true);
if (urlQuery.pathname === '/api/parsetime' && urlQuery.query.iso) {
var timeToParse = new Date(urlQuery.query.iso);
var returnValue = {
'hour': timeToParse.getHours(),
'minute': timeToParse.getMinutes(),
'second': timeToParse.getSeconds()
};
res.end(JSON.stringify(returnValue));
}
else if (urlQuery.pathname === '/api/unixtime') {
res.end(JSON.stringify({'unixtime': Date.now()}));
}
else {
res.end('Nothing to see here');
}
});
server.listen(process.argv[2]);
And the results:
────────────────────────────────────────────────────────────────────────────────
1. ACTUAL: "{\"hour\":19,\"minute\":55,\"second\":13}"
1. EXPECTED: "{\"hour\":19,\"minute\":55,\"second\":13}"
2. ACTUAL: "{\"unixtime\":1457636214219}"
2. EXPECTED: "{\"unixtime\":1457636113611}"
3. ACTUAL: ""
3. EXPECTED: ""
────────────────────────────────────────────────────────────────────────────────
@redfast00 Your solution appears correct; I implemented it similarly the first time around and experienced this issue, too.
The expected solution wants the exact unixtime of the preceding JSON request to /api/parsetime that it sends you. I was able to pass this exercise by caching timeToParse globally, so that the side effect of the first JSON call would be there for me to call getTime() on it during the second JSON call. Naturally, writing servers functioning by side effect is not a good practice, but since even a difference of one instruction's execution makes the milliseconds differ, there's no other way to obtain an exact match.
I think learnyounode should accept any unixtime reasonably close after the time it sent you (within +1000 ms?)
@saeidalidadi learnyounode calls /api/parsetime?iso=2016-04-16T23:56:50Z (or something similar) and the JSON server needs to respond with { "hour": 23, "minute": 56, "second": 50 } based on parsing the hh:mm:ss out of the iso query parameter, not the current time.
@redfast00 your unixtime section does not work because you are calling Date.now() which will give you a timestamp a couple of milliseconds later than the one passed into your program by learnyounode. Instead of using Date.now() you need to work with the timestamp passed to you as a param e.g. var unixTime = new Date(parsed_url.query.iso).getTime();