Commits

Trammell Hudson committed 1ab5865

Move unix_epoch_time() into lib/datetime.c

  • Participants
  • Parent commits 7503a5a

Comments (0)

Files changed (4)

File auth/src/totp.c

 /** \file
  * Google Two Factor authentication watch face.
+ *
+ * You must set GMT_OFFSET for your local timezone!
+ * -5 == NYC.
  */
 #include "pebble_os.h"
 #include "pebble_app.h"
 #include "pebble_fonts.h"
+#include "pebble_th.h"
 #include "sha1.h"
 
+#define GMT_OFFSET -5
+
 #define UUID {0x24, 0x15, 0x2D, 0xAE, 0xDD, 0x49, 0x47, 0x47, 0x96, 0xD5, 0x78, 0xB1, 0xDD, 0xBD, 0x1B, 0xB2}
 
 PBL_APP_INFO(
 	graphics_fill_rect(ctx, GRect(0, 0, (w * remaining) / 30, h), 4, GCornersRight);
 }
 
-#define GMT_OFFSET 5
-
-static uint32_t unix_epoch_time(
-	const PblTm * const curr_time
-)
-{
-  long now = 0;
-  now += (curr_time->tm_year-70)*31536000;
-  now += ((curr_time->tm_year-69)/4)*86400;
-  now -= ((curr_time->tm_year-1)/100)*86400;
-  now += ((curr_time->tm_year+299)/400)*86400;
-  now += curr_time->tm_yday*86400;
-  now += curr_time->tm_hour*3600;
-  now += curr_time->tm_min*60;
-  now += curr_time->tm_sec;
-  return now;
-}
 
 static void
 handle_tick(
 
 	// Convert the time into a unix epoch time based on our local time.
 	// this should be done in a library
-	const uint32_t now = unix_epoch_time(ptm) + 3600 * GMT_OFFSET;
+	const uint32_t now = unix_epoch_time(ptm, 3600 * GMT_OFFSET);
 	uint32_t auth = oauth_calc(now);
 
 	otp_buffer[6] = '\0';

File lib/src/Makefile

 SRCS += \
 	sprintf.c \
 	layer.c \
+	datetime.c \
 
 LIB := libpebble_th.a
 

File lib/src/datetime.c

+/** \file
+ * Date/time calculations
+ */
+
+#include <stdint.h>
+#include "pebble_os.h"
+#include "pebble_th.h"
+
+
+uint32_t
+unix_epoch_time(
+	const PblTm * const curr_time,
+	const int gmt_offset
+)
+{
+	long now = 0;
+
+	now += (curr_time->tm_year-70)*31536000;
+	now += ((curr_time->tm_year-69)/4)*86400;
+	now -= ((curr_time->tm_year-1)/100)*86400;
+	now += ((curr_time->tm_year+299)/400)*86400;
+	now += curr_time->tm_yday*86400;
+	now += curr_time->tm_hour*3600;
+	now += curr_time->tm_min*60;
+	now += curr_time->tm_sec;
+	now -= gmt_offset;
+
+	return now;
+}

File lib/src/pebble_th.h

 })
 
 
+/** Translate a Pebble time struct into unix epoch.
+ *
+ * GMT offset is in seconds east of GMT.
+ * For example, NYC EST is -5 * 3600 seconds.
+ * \todo check dst offset!
+ */
+uint32_t
+unix_epoch_time(
+	const PblTm * const curr_time,
+	const int gmt_offset
+);
+
+
 #endif