Página 1 de 1

Converter UNIXTIME para Ascii

MensagemEnviado: 23 Mai 2018 16:49
por vtrx
Alguém sabe o algorítmico ou uma rotina(c) para usar no micro,para 'extrair' um valor de 32 bits,do RTC,e gerar uma array com caracteres ASCII?
Tem um monte de site que faz online,tipo o valor 0x00015180 é convertido para 01/01/1970 @ 12:00am (UTC).
https://www.unixtimestamp.com/index.php

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 23 Mai 2018 16:53
por mrgadotti
vtrx escreveu:Alguém sabe o algorítmico ou uma rotina(c) para usar no micro,para 'extrair' um valor de 32 bits,do RTC,e gerar uma array com caracteres ASCII?


Se for usando C, tem várias bibliotecas prontas ou quebrar o int32, como o sprintf...

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 23 Mai 2018 17:06
por vtrx
sim,um exemplo direto pois minha rotina é usada num LCD e não na serial.

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 23 Mai 2018 19:06
por denis
Veja se antende:

Código: Selecionar todos
#include <stdio.h>
#include <time.h>

int main()
{
    time_t timer;
    char buffer[26];
    struct tm* tm_info;

    time(&timer);
    tm_info = localtime(&timer);

    strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
    puts(buffer);

    return 0;
}


Deve imprmir algo assim: 2018-05-23 22:07:08

fonte:
https://stackoverflow.com/questions/367 ... 181754-811

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 24 Mai 2018 06:47
por pamv
Se você tiver acesso a um linux tente

Código: Selecionar todos
$ man 3 ctime


para ter uma descrição das funções padrão para lidar com tempo:

Código: Selecionar todos
      The ctime(), gmtime() and localtime() functions all take an argument of
       data  type time_t, which represents calendar time.  When interpreted as
       an absolute time value, it represents the  number  of  seconds  elapsed
       since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

       The asctime() and mktime() functions both take an argument representing
       broken-down time, which is a representation separated into year, month,
       day, and so on.

       Broken-down  time  is  stored  in the structure tm, which is defined in
       <time.h> as follows:

           struct tm {
               int tm_sec;    /* Seconds (0-60) */
               int tm_min;    /* Minutes (0-59) */
               int tm_hour;   /* Hours (0-23) */
               int tm_mday;   /* Day of the month (1-31) */
               int tm_mon;    /* Month (0-11) */
               int tm_year;   /* Year - 1900 */
               int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
               int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
               int tm_isdst;  /* Daylight saving time */
           };
      The call ctime(t) is equivalent to asctime(localtime(t)).  It  converts
       the calendar time t into a null-terminated string of the form

              "Wed Jun 30 21:49:08 1993\n"
...

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 24 Mai 2018 08:07
por vtrx
denis escreveu:Veja se antende:

Código: Selecionar todos
#include <stdio.h>
#include <time.h>

int main()
{
    time_t timer;
    char buffer[26];
    struct tm* tm_info;

    time(&timer);
    tm_info = localtime(&timer);

    strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
    puts(buffer);

    return 0;
}


Deve imprmir algo assim: 2018-05-23 22:07:08

fonte:
https://stackoverflow.com/questions/367 ... 181754-811


Onde entra a minha variável de 32 bits que contem o resultado do RTC?

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 24 Mai 2018 08:37
por denis
A variável timer é o seu contador em segundos.

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 24 Mai 2018 09:50
por vtrx
Usando o Keil no STM a função trava o sistema...

Trava em:
Código: Selecionar todos
time(&timer);


Funcionou assim:

Código: Selecionar todos
void TimeShow(void) 

    time_t timer;
    char buffer[26];
    struct tm* tm_info;

     timer = RTC_GetCounter();
  // time(&timer);
    tm_info = localtime(&timer);
    strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
 //   puts(buffer);
      GUI_Text(2,120,buffer,19,White,Black);// plotar no lcd
}


Só obtive um warning:
main.c(518): warning: #167-D: argument of type "char *" is incompatible with parameter of type "u8 *"

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 24 Mai 2018 10:19
por denis
Faz um type cast no ponteiro.

Código: Selecionar todos
(u8 *)buffer

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 24 Mai 2018 10:25
por vtrx
denis escreveu:Faz um type cast no ponteiro.

Código: Selecionar todos
(u8 *)buffer


Resolveu o warning,mas de qualquer jeito a saída(texto) está correta.
Como mudo a ordem da apresentação,tipo dia,mes e ano?

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 24 Mai 2018 12:18
por denis
Troca os parâmetros na função strftime.
%Y %m ...

Re: Converter UNIXTIME para Ascii

MensagemEnviado: 25 Mai 2018 16:14
por B-EAGLE
Se não tens as as funções unix padrão, escrevi ha uns anos uma funçãozinha bem simples pra converter pra decimal, dá uma olhada pq fiz o inverso também, dá uma olhada no outro repositório:

https://github.com/brunoeagle/unixtime-to-date