Converter UNIXTIME para Ascii

Programação C em geral

Moderadores: 51, guest2003

Converter UNIXTIME para Ascii

Mensagempor vtrx » 23 Mai 2018 16:49

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
Avatar do usuário
vtrx
Dword
 
Mensagens: 2239
Registrado em: 20 Abr 2008 21:01

Re: Converter UNIXTIME para Ascii

Mensagempor mrgadotti » 23 Mai 2018 16:53

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...
Avatar do usuário
mrgadotti
Byte
 
Mensagens: 421
Registrado em: 02 Jun 2010 21:14
Localização: Timbó - SC

Re: Converter UNIXTIME para Ascii

Mensagempor vtrx » 23 Mai 2018 17:06

sim,um exemplo direto pois minha rotina é usada num LCD e não na serial.
Avatar do usuário
vtrx
Dword
 
Mensagens: 2239
Registrado em: 20 Abr 2008 21:01

Re: Converter UNIXTIME para Ascii

Mensagempor denis » 23 Mai 2018 19:06

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
denis
Byte
 
Mensagens: 257
Registrado em: 06 Mar 2007 12:29
Localização: Americana - SP

Re: Converter UNIXTIME para Ascii

Mensagempor pamv » 24 Mai 2018 06:47

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"
...
pamv
Word
 
Mensagens: 842
Registrado em: 20 Jun 2016 21:47

Re: Converter UNIXTIME para Ascii

Mensagempor vtrx » 24 Mai 2018 08:07

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?
Avatar do usuário
vtrx
Dword
 
Mensagens: 2239
Registrado em: 20 Abr 2008 21:01

Re: Converter UNIXTIME para Ascii

Mensagempor denis » 24 Mai 2018 08:37

A variável timer é o seu contador em segundos.
denis
Byte
 
Mensagens: 257
Registrado em: 06 Mar 2007 12:29
Localização: Americana - SP

Re: Converter UNIXTIME para Ascii

Mensagempor vtrx » 24 Mai 2018 09:50

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 *"
Avatar do usuário
vtrx
Dword
 
Mensagens: 2239
Registrado em: 20 Abr 2008 21:01

Re: Converter UNIXTIME para Ascii

Mensagempor denis » 24 Mai 2018 10:19

Faz um type cast no ponteiro.

Código: Selecionar todos
(u8 *)buffer
denis
Byte
 
Mensagens: 257
Registrado em: 06 Mar 2007 12:29
Localização: Americana - SP

Re: Converter UNIXTIME para Ascii

Mensagempor vtrx » 24 Mai 2018 10:25

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?
Avatar do usuário
vtrx
Dword
 
Mensagens: 2239
Registrado em: 20 Abr 2008 21:01

Re: Converter UNIXTIME para Ascii

Mensagempor denis » 24 Mai 2018 12:18

Troca os parâmetros na função strftime.
%Y %m ...
denis
Byte
 
Mensagens: 257
Registrado em: 06 Mar 2007 12:29
Localização: Americana - SP

Re: Converter UNIXTIME para Ascii

Mensagempor B-EAGLE » 25 Mai 2018 16:14

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
B-EAGLE
Word
 
Mensagens: 847
Registrado em: 19 Out 2006 14:12
Localização: Campo Grande - MS


Voltar para Visual C++/C/C++/C#

Quem está online

Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante

cron

x