-2

我刚开始制作鹅卵石表盘,我想做一个倒计时计时器,它可以计算一天还剩多少小时,然后每天凌晨 12 点重新开始。

问题是我想不出一种方法来正确显示数学:当前时间减去 24 小时,然后在手表上显示(包括 H:M 等小时和分钟)。

// Time functions
static void update_time() {
  // Get a tm structure
  time_t temp = time(NULL); 
  struct tm *tick_time = localtime(&temp);
double munistime;
  // Create a long-lived buffer
  static char buffer[] = "00:00:00";

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

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

  // Display this time on the TextLayer
  text_layer_set_text(s_time_layer, buffer);
}

static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
 update_time();
}
// Set the count from the start of the day
Static void update_countdown () {


}

    // Window Functions
    static void main_window_load(Window *window) {
   // Create time TextLayer
  s_time_layer = text_layer_create(GRect(0, 130, 150,50));
  text_layer_set_background_color(s_time_layer, GColorClear);
  text_layer_set_text_color(s_time_layer, GColorBlack);
  text_layer_set_text(s_time_layer, "00:00:00");
  // Improve the layout to be more like a watchface
  text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_30_BLACK));
  text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);


  // Creates A count Timer Layer
  s_countdown_layer = text_layer_create(GRect(0, 40, 150, 50));
  text_layer_set_background_color(s_countdown_layer, GColorBlack);
  text_layer_set_text_color(s_countdown_layer, GColorWhite);
  text_layer_set_text(s_countdown_layer, "00:00:00");
  text_layer_set_text_alignment(s_countdown_layer, GTextAlignmentCenter);
  text_layer_set_font(s_countdown_layer,fonts_get_system_font(FONT_KEY_GOTHIC_28));


  // Add it as a child layer to the Window's root layer
  layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));
  layer_add_child(window_get_root_layer(window),text_layer_get_layer(s_countdown_layer));
   update_time();
}
4

2 回答 2

0

我会在 3 个变量中获得 tick_time - 小时、分钟和秒。计算它们的新值,然后将它们保存在缓冲区中以供显示。

于 2015-01-13T13:53:06.717 回答
0

如果您只是将时间转换为秒,并从 24 小时中减去,这还不错:

time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
static char buffer[] = "00:00:00";

// 86400 = 24 hours * 60 minutes * 60 seconds
// this gets the total number of seconds remaining before midnight
int secsUntilMidnight = 86400 - (tick_time->tm_hour * 3600 + tick_time->tm_min * 60 + tick_time->tm_sec);

然后,再次拆分值:

int hr = secsUntilMidnight / 3600;
int mn = (secsUntilMidnight - (hr*3600)) / 60;
int sc = (secsUntilMidnight - (hr*3600)) - (mn*60);

sprintf (buffer, "%02d:%02d:%02d", hr, mn, sc);

您可以通过每天设置一次 secsUntilMidnight 来提高效率,然后只需在您的 tickHandler 中减去 1 秒,您还可以使拆分值的数学运算更有效。

于 2015-10-14T04:46:16.400 回答