【问题标题】:Pebble C convert char to time for use in double difftime(time_t end, time_t beginning)Pebble C 将 char 转换为时间以用于双 difftime(time_t end, time_t begin)
【发布时间】:2016-09-17 04:52:53
【问题描述】:

我正在尝试转换

static char bufferTR[] = "1645";

到这样的事情......

struct tm *tick_time = localtime(&temp);

在这个函数中使用...

difftime(time_t end, time_t beginning)

这将给我以秒为单位的两者之间的时间,作为双倍。

任何帮助将不胜感激。

更新代码...

#include <stdio.h>
#include <time.h>

static void update_time() {
      // Get a tm structure
      time_t temp = time(NULL);


  struct tm *tick_time = localtime(&temp);

  // Create a long-lived buffer
  //static char buffer[] = "00";
      char buffer[80];

  static char buffer2[] = "00";
  static char buffer3[] = "Xxx Xxx 00"; //Day of Week, Month, Date

  strftime(buffer3, sizeof(buffer3), "%a %b %e", tick_time);

  // Write the current hours and minutes into the buffer
  if(clock_is_24h_style() == true) {
    // Use 24 hour format
    strftime(buffer, sizeof("00"), "%M", tick_time);

    strftime(buffer2, sizeof("00"), "%H", tick_time);

  } 
  else {
    // Use 12 hour format
    strftime(buffer, sizeof("00"), "%M", tick_time);

    strftime(buffer2, sizeof("00"), "%I", tick_time);
  }

  //https://sourceware.org/newlib/libc.html#Timefns

    static char bufferTR[6] = "1645";
    time_t beginning;
    struct tm info;

    int hh;
    int mm;

    int minutediff = 1;

    if (sscanf(bufferTR, "%2d%2d", &hh, &mm) == 2) {

        printf("hh %d mm %d\n", hh, mm);

        info.tm_year = 2001 - 1900;
        info.tm_mon = 7 - 1;
        info.tm_mday = 4;
        info.tm_hour = hh;
        info.tm_min = mm;
        info.tm_sec = 1;
        info.tm_isdst = -1;

        beginning = mktime(&info);
        if( beginning == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer, sizeof(buffer), "%c", &info );
        }

        time_t end;
        struct tm info2;
        char buffer2[80];

        info2.tm_year = 2001 - 1900;
        info2.tm_mon = 7 - 1;
        info2.tm_mday = 4;
        info2.tm_hour = hh;
        info2.tm_min = mm+minutediff;
        info2.tm_sec = 1;
        info2.tm_isdst = -1;

        end = mktime(&info2);
        if( end == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer2, sizeof(buffer2), "%c", &info2 );
        }

        printf("%f", difftime(end, beginning));

    }

  // Display this time on the TextLayer
  text_layer_set_text(s_time_layer, buffer);
  text_layer_set_text(s_time_layer2, buffer2);
  text_layer_set_text(s_time_layer3, buffer3); 

  text_layer_set_text(s_temp_layer, bufferTR);
  text_layer_set_text(s_temp_layer2, buffer2);
}

【问题讨论】:

  • 请让您的问题更清楚。目前还不清楚显示的每一行代码如何与其他行相关。 bufferTRchar 缓冲区,但下一行代码没有引用任何字符缓冲区。 localtime 返回一个 struct tm * 但下一行代码不接受任何 struct tm * 参数。
  • POSIX 函数 strptime() 和标准 C 函数 mktime() 可能会有所帮助。但是,如果那是 hhmm (小时,分钟)格式,那么它们就大材小用了。您可以使用if (sscanf(bufferTR, "%2d%2d", &amp;hh, &amp;mm) == 2) { …OK… } 将字符串转换为小时和分钟,然后相乘和相加将为您提供以秒为单位的时间,而无需发明年、月、日。

标签: c time standards pebble-watch pebble-sdk


【解决方案1】:

我假设您想要 16:45 和 16:45 + x 之间的差异。

假设差异是 x=1 分钟 int minutediff = 1;。然后我们可以在不同类型之间进行转换。

#include <stdio.h>
#include <time.h>

int main ()
{

    static char bufferTR[5] = "1645";
    time_t beginning;
    struct tm info;
    char buffer[80];

    int hh;
    int mm;

    int minutediff = 1;

    if (sscanf(bufferTR, "%2d%2d", &hh, &mm) == 2) {

        printf("hh %d mm %d\n", hh, mm);

        info.tm_year = 2001 - 1900;
        info.tm_mon = 7 - 1;
        info.tm_mday = 4;
        info.tm_hour = hh;
        info.tm_min = mm;
        info.tm_sec = 1;
        info.tm_isdst = -1;

        beginning = mktime(&info);
        if( beginning == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer, sizeof(buffer), "%c", &info );
        }

        time_t end;
        struct tm info2;
        char buffer2[80];

        info2.tm_year = 2001 - 1900;
        info2.tm_mon = 7 - 1;
        info2.tm_mday = 4;
        info2.tm_hour = hh;
        info2.tm_min = mm+minutediff;
        info2.tm_sec = 1;
        info2.tm_isdst = -1;

        end = mktime(&info2);
        if( end == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer2, sizeof(buffer2), "%c", &info2 );
        }

        printf("difftime %f ", difftime(end, beginning));

    }

    return(0);
}

输出

hh 16 mm 45
difftime 60.000000 

【讨论】:

  • 我试图找出现在和 16:45 之间的差异。我添加了到目前为止的 updateTime 函数,它无法构建。 ../src/main.c:在函数'update_time'中:../src/main.c:121:3:错误:'return'有一个值,在函数返回void [-Werror]
猜你喜欢
  • 2014-11-07
  • 2011-01-05
  • 2016-11-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 2013-03-22
  • 2015-03-22
相关资源
最近更新 更多