1

嗨,我正在尝试做一个在 CgetTime()中返回的函数。uint16_t

我现在确实可以成功获得一系列字符

这是我输出字符数组的代码。

说今天是Dec 9 2014输出将是091214

那么我怎样才能使输出成为一种uint16_t类型呢?

在这种情况下"0"091214如果它是一种uint16_t类型,我可以保留吗?

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;}

我从网上找到的代码中修改了另一种方法。

这个返回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;}
4

1 回答 1

0
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;
}
于 2014-12-10T02:11:01.310 回答