learnyounode
learnyounode copied to clipboard
Http Json Api Server: verify wrong? and unixtime
So my first problem is that I somehow have a difference of two hours between my outcome of the time and the outcome from the learnyounode verify command:
Maybe it is important to say that 18 was the time on my computer at the time I made the screenshot. Anyway when I used the learnyounode run command I saw that my time matched the time from the query:
My second problem is that my unix timestamp isn't right, but I can't figure out why. Are there any extra things like leap years? Or do I have a fault in my code?
Code from main file:
var http = require('http');
var url = require('url');
var toUnixtime = require('./unixtime-module.js');
http.createServer(function(request, response) {
var urlObject = url.parse(request.url);
response.writeHead(200, { 'Content-Type': 'application/json' });
if(request.method == 'GET') {
if (urlObject.pathname == '/api/parsetime') {
//regex convert nonummeric characters to a space
var dateTime = urlObject.query.replace(/\D/g, " ").split(" ");
//GMT+ 2 hours (the time given by the verify is GMT + 0(mine is +2))
dateTime[7] = Number(dateTime[7]) + 2;
response.write(JSON.stringify({hour: Number(dateTime[7]), minute: Number(dateTime[8]), second: Number(dateTime[9])}));
response.end();
} else if (urlObject.pathname == '/api/unixtime') {
response.write(toUnixtime(urlObject.query));
response.end();
}
}
}).listen(process.argv[2]);
Code from the unixtime-module:
var miliseconds = 0;
function leapYear(year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? true : false;
}
function yearsToMiliseconds(year) {
//-1 because the year you are in isn't a full year
year = year - 1
for(; year >= 1970; year--) {
miliseconds += (leapYear(year) === true) ? 366 * 86400000000 : 365 * 86400000000;
}
}
function monthsAndDaysToMiliseconds(day, month, year) {
//-1 because the day you are in isn't a full day
miliseconds += (day - 1) * 86400000000;
months = { 01 : 31, 02 : 28, 03 : 31, 04 : 30, 05 : 31, 06 : 30, 07 : 31, 08 : 31, 09 : 30, 10 : 31, 11 : 30, 12 : 31 };
//remove the zero so I can do minus 1
month = month.substring(1);
//-1 because the month you are in isn't a full month
for(; month - 1 > 0; month--) {
miliseconds += (leapYear(year) === true && month == 02) ? 29 * 86400000000 : months[month] * 86400000000;
}
}
function timeToMiliseconds(hour, minutes, seconds, milisec) {
//86400000000 miliseconds per day / 24 = 3600000 per hour / 60 = 60000 per minute / 60 = 1000 per second
miliseconds += hour * 3600000;
//GMT+ 2 hours (the time given by the verify is GMT + 0(mine is +2))
miliseconds += 3600000 * 2;
miliseconds += minutes * 60000;
miliseconds += seconds * 1000;
miliseconds += milisec;
}
module.exports = function toUnixtime(query) {
//regex convert nonummeric characters to a space
var dateTime = query.replace(/\D/g, '/');
//don't take to much slashes since we will split on that
dateTime = dateTime.slice(4, 26);
dateTime = dateTime.split('/');
//datetime after: [0] = year [1] = month [2] = day [3] = hour [4] = minutes [5] = seconds [6] = miliseconds
yearsToMiliseconds(dateTime[0]);
monthsAndDaysToMiliseconds(dateTime[2], dateTime[1], dateTime[0]);
timeToMiliseconds(dateTime[3], dateTime[4], dateTime[5], dateTime[6]);
//return as JSON string and make a number of 13 characters
return JSON.stringify({ "unixtime": Number(miliseconds.substring(0, 13)) });
}
PS: I know I can get the unix timestamp in a shorter way, but I want to learn more and challenge myself.
Did you check the time zones? GMT time versus local time?
I see that the GMT time is now: 18:09 and my time is 20:09. So I guess I have to add 2 hours extra to the total of miliseconds and also add two hours to the first JSON response. But then we still have the second problem left.
Did you know that you can pass hour, day, month etc. to the date object? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Also: it is possible to add formatted code to github: https://help.github.com/articles/github-flavored-markdown/#syntax-highlighting
Edit: The timezone problem also exists for the unix time.
Yes I know I can use the date object to get the unix timestamp, but I want to do it without the date object.
PS: Thanks for the information about the formatted code. I didn't know that.
Edit: I know that the problem also exists for the unix time. I added two hours to both responses I need to return.