c - Pebble C 将 char 转换为时间以在 double difftime(time_t end, time_t begin) 中使用

标签 c time standards pebble-watch pebble-sdk

我正在尝试转换

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

最佳答案

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

假设差异为 x=1 分钟int 分钟diff = 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 

关于c - Pebble C 将 char 转换为时间以在 double difftime(time_t end, time_t begin) 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37337954/

相关文章:

c - 一台服务器绑定(bind)不同的端口

获取当前时间的 Java 代码

c++ - 编译器标准支持(c++11、c++14、c++17)

c++ 标准两个不同的声明

C 代码...尝试将 while 循环与 OR 运算符结合起来以检查输入数字是否为素数

python - 是什么让 C 比 Python 更快?

c++ - 如何使用 FFMPEG 打开文件\实时流使用 openCV 编辑每个帧并将其保存为使用 FFMPEG 文件\实时流编码?

javascript - 我多久前加载了一个页面?

algorithm - 每秒更新间隔小于一秒的速度

C++ 标准(源文件的换行符结尾)