2016-05-20 3 views
-2

イム...ペブルCこの中で使用するには、このような何かに</p> <pre><code>static char bufferTR[] = "1645"; </code></pre> <p>を変換しようと、二重difftimeで使用するための時間にCHAR(time_tの終わり、time_t型始まる)

struct tm *tick_time = localtime(&temp); 

を変換します関数...

difftime(time_t end, time_t beginning) 

これは2秒間の時間を2倍にします。

ご協力いただければ幸いです。

更新されたコード...

#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); 
} 
+1

あなたの質問をより明確にしてください。示されているコードの各行が他の行とどのように関係しているかは正確には分かりません。 'bufferTR'は' char'バッファですが、次のコード行はcharバッファを参照しません。 'localtime'は' struct tm * 'を返しますが、次のコード行は' struct tm * 'パラメータを受け付けません。 – kaylum

+1

POSIX関数['strptime()'](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html)と標準C関数 ['mktime()'](http:// pubs。 opengroup.org/onlinepubs/9699919799/functions/mktime.html)が役に立ちます。それが 'hhmm'(時分)形式であることを意図しているのであれば、彼らは過度のものです。文字列を時間と分に変換してから乗算して加算すると、 'if(sscanf(bufferTR、"%2d%2d "、&hh、&mm)== 2){... OK} 1年、1ヶ月、1日を発明する必要なく、数秒で作業できます。 –

答えて

1

私はあなたが16時45分と午後04時45 + xとの差をつけたいと仮定します。

diffが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 
+0

今の時と16時45分の違いを取得しようとしています。これまでに持っていたupdateTime関数を追加しました。ビルドに失敗しました。 ../src/main.c:関数 'update_time'で: ../src/main.c:121:3:エラー:戻り値:戻り値、関数内でvoid [-Werror] – OscarTheGrouch

関連する問題

 関連する問題