a gettime() function that returns a uint16_t value in C

1.3k views Asked by At

Hi I'm trying to do a getTime() function that returns uint16_t in C.

I do can successfully get an array of chars now

Here's my code for a output of array of chars.

Say today is Dec 9 2014 the output will be 091214

So how can i make the output a uint16_t type?

in this case can i keep the "0" in 091214 if it's a uint16_t type?

char *getT(){
struct tm *tm;
time_t t;
static char str_date[10];

t = time(NULL);
tm = localtime(&t);

strftime(str_date, sizeof(str_date), "%d%m%y", tm);
return str_date;}

and there's another approach i modified from the code i found on-line.

this one returns the sum of 9+12+14....

uint16_t getD(){
struct tm *tm_time;
time_t ti;
//const time_t create_time;
uint16_t t, d;
ti = time(NULL);
tm_time = localtime(&ti);
t = (tm_time->tm_sec >> 1) + (tm_time->tm_min << 5) + (tm_time->tm_hour << 11);
d = (tm_time->tm_mday>>1) + ((tm_time->tm_mon+1) << 5) + ((tm_time->tm_year-80) << 9);
printf("%d %d %d %d \n",d,tm_time->tm_mday>>1,tm_time->tm_mon<<5,(tm_time->tm_year-80)<<9);
return d;}
1

There are 1 answers

2
chux - Reinstate Monica On BEST ANSWER
uint16_t getD() {
  time_t ti;
  ti = time(NULL);
  struct tm tm_time;
  tm_time = *localtime(&ti);

  //const time_t create_time;
  uint16_t t, d;
  d = tm_time.tm_mday
      + (tm_time.tm_mon + 1) * 32
      + (tm_time.tm_year - (1980-1900)) * 512;

  // Print ddmmyy
  printf("%02d%02d%02d\n", 
      (int) d%32, (int) (d/32)%16, (int) ((d/512)%128 + (1980-1900))%100);
  return d;
}