How to convert Unix UTC ?

MH's icon

Hello

I am playing around with extracting data with the MAXURL WEATHER REPORT

Max Patch
Copy patch and select New From Clipboard in Max.

Is there a way to convert the Unix UTC value that I get for "sunset" into "human readable values"
Yr    Month    Day Hr    Min    Sec

Jan M's icon

Hi MArie-Helene,

Max Patch
Copy patch and select New From Clipboard in Max.

The easiest way is to use the JavaScript Date object. Here is an example patch:

The code inside the js object would be:

function timestamp(val) {
    var d = new Date();
    outlet(0, d.toDateString());
}

There are many formatting options - this is just one possibility. Check out the JavaScript Date . Reference for more details:

MH's icon

JAN

Thank you so much.
That is what I was looking for ... not knowing I had to use javascript (whole new world for me).

I have replaced the java for d.toString(); to get the time as well (I read the exercises).

How can I extract the time only (hour and min) to numberbox ?
Would that with fromsymbol ?
I tried but doesn't seem to work

Max Patch
Copy patch and select New From Clipboard in Max.

Also I seem to only get the actual time of the day and not the sunset as I am looking for.
Is there something wrong the way I call it ?

Jan M's icon

ouups my bad was typing too fast. the correct code line would be:

var d = new Date(val * 1000);

This way you set the time to the Unix timestamp. The multiplication is because the unix time counts seconds since Jan 1st 1970 00:00:00 while JavaScript operates on milliseconds.

The full code for just outputting the time would be:

outlets = 2;

function timestamp(val) {
var d = new Date (val * 1000);
outlet(0, d.getHours());
outlet(1, d.getMinutes());
}

the left outlet will give you the hours (0 -23) the right one the minutes (0 - 59)

Jan M's icon

only now i was able to see your patch with the notes. Here is one more version, that I hope is closer to what you are looking for.

outlets = 2;
function timestamp(val) {
    var d = new Date (val * 1000);
    outlet(0, d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCSeconds());
    outlet(1,d.getTimezoneOffset());
}

this one gives you a list out of the left outlet with the numbers for 'Year' 'Month' 'Date' 'Hours' 'Seconds'. All in GMT/UTC without the timezone offset. The right outlet will send the Timerzone offset from UTC/GMT in minutes. This way you can easily get all infos by unpacking the list.

Jan M's icon

...note that one has do add +1 to the month as for some odd reasons the JS Date objects reports that as 0 - 11 instead of 1 - 12 (not in accordance to all the other infos)...

MH's icon

Hi JAN

Wow thanks .. I am very happy to learn a bit of javascript from you.

I wrote this code to get the minutes .. I wonder if that is correct

outlets = 3;
function timestamp(val) {
    var d = new Date (val * 1000);
    outlet(0, d.getUTCHours());
    outlet(1, d.getUTCMinutes());
    outlet(2,d.getTimezoneOffset());
}

There is something funny though ... the time doesn't seem right

sunset is 18:23 for today
but I get 8:16 ?
and timezone offset 240 ?

not sure what to do with the 240 offset

MH

Jan M's icon

Hi Marie-Helene,

the code looks fine. A difference of about 8 min is strange though. You can check the Unix timepstamp that you send to the [js] easily on a webpage. there are a lot of converter pages around (i.e. http://www.epochconverter.com/). Do those pages give a different reading than Max?

the timezone offset expresses the difference in time between your local time (the time zone of your computer) and UTC/GMT time. 240 means that you are 240 minutes ahead of UTC/GMT.

Jan

MH's icon

Thank you JAN

Yep something is not right

I get the ID for Ottawa on
http://openweathermap.org/city/7626289

I put that number ID in the URL patch
and the numbers don't make sens

I'll try with coordinates

MH's icon

Hello JAN

Just to give you an update (and who ever has the same problem)
I was successful by changing the ID on the url by the city name, country
in my case ottawa,ca

So much thanks again, it works perfectly :-)

Jan M's icon

Thanks Marie-Helene,

last days I didn't had time to check here.

cheers,

Jan