Question about the time informed via mqtt

Turbovixa year ago

Dear all, I have a question, in the traccar UI the time that is displayed when the module sends the data is correct (according to my Debian server timezone), but the data received via mqtt is related to UTC time, and not to my timezone. Is there a way to get around this?

Anton Tananaeva year ago

Doesn't it include timezone?

Turbovixa year ago

Hi Anton.
No, the data that arrives via mqtt arrives in UTC time.

"serverTime": "2024-08-09T14:16:27.440+00:00",
"deviceTime": "2024-08-09T14:16:24.000+00:00",
"fixTime": "2024-08-09T14:16:24.000+00:00",

Anton Tananaeva year ago

It does include timezone, so you should be fine. You can convert to whatever local time you need.

Turbovixa year ago

I understand, so I will write a function in javascript to do the conversion. Thanks.

Anton Tananaeva year ago

Typically you don't need to write any functions. It will automatically take this ISO time and handle it correctly. Something like this:

new Date('2024-08-09T14:16:24.000+00:00')
Turbovixa year ago

Sorry, I am receiving data from Traccar server on Node-red and I need to do an automation based on this information, so here will need some lines to do this job.

Anton Tananaeva year ago

You can't use standard JavaScript there?

Turbovixa year ago

Yes, the function node in Nodered works exclusively with Javascript.
I have this code and it is working correctly.

function converterParaAmericaSaoPaulo(data) {
    // Converter a string de data para um objeto Date
    const date = new Date(data);

    // Opções para o formato de data e fuso horário
    const options = {
        timeZone: 'America/Sao_Paulo',
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false
    };

    // Criar um formatador de data com o fuso horário America/Sao_Paulo
    const formatter = new Intl.DateTimeFormat('pt-BR', options);

    // Formatando a data e extraindo partes
    const formattedDateParts = formatter.formatToParts(date);

    // Montando a string final no formato ISO
    const formattedDate = `${formattedDateParts.find(part => part.type === 'day').value}-` +
                          `${formattedDateParts.find(part => part.type === 'month').value}-` +
                          `${formattedDateParts.find(part => part.type === 'year').value} ` +
                          `${formattedDateParts.find(part => part.type === 'hour').value}:` +
                          `${formattedDateParts.find(part => part.type === 'minute').value}:` +
                          `${formattedDateParts.find(part => part.type === 'second').value}`;

    return formattedDate;
}

// Exemplo de uso
const data = msg.payload;
const dataConvertida = converterParaAmericaSaoPaulo(data);
msg.payload = dataConvertida
return msg
Anton Tananaeva year ago

Are you sure this doesn't work?

const date = new Date(time);
const formatter = new Intl.DateTimeFormat('pt-BR', options);
const formattedDate = formatter.format(date);
Turbovixa year ago

Yes, it works! lol...