From dd97f67ee4203a74775c4dbd7b3bd1ec02d85616 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Thu, 27 Dec 2007 00:29:12 +0100 Subject: [PATCH] Add functions to read timestamps from ISO images. --- src/util.c | 29 +++++++++++++++++++++++++++++ src/util.h | 3 +++ test/test_util.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/util.c b/src/util.c index 65248cd..c85c709 100644 --- a/src/util.c +++ b/src/util.c @@ -600,3 +600,32 @@ void iso_datetime_17(unsigned char *buf, time_t t) buf[16] = tzoffset; } } + +time_t iso_datetime_read_7(const uint8_t *buf) +{ + struct tm tm; + + tm.tm_year = buf[0]; + tm.tm_mon = buf[1] - 1; + tm.tm_mday = buf[2]; + tm.tm_hour = buf[3]; + tm.tm_min = buf[4]; + tm.tm_sec = buf[5]; + return timegm(&tm) - buf[6] * 60 * 15; +} + +time_t iso_datetime_read_17(const uint8_t *buf) +{ + struct tm tm; + + sscanf((char*)&buf[0], "%4d", &tm.tm_year); + sscanf((char*)&buf[4], "%2d", &tm.tm_mon); + sscanf((char*)&buf[6], "%2d", &tm.tm_mday); + sscanf((char*)&buf[8], "%2d", &tm.tm_hour); + sscanf((char*)&buf[10], "%2d", &tm.tm_min); + sscanf((char*)&buf[12], "%2d", &tm.tm_sec); + tm.tm_year -= 1900; + tm.tm_mon -= 1; + + return timegm(&tm) - buf[16] * 60 * 15; +} diff --git a/src/util.h b/src/util.h index 2405f75..21ed3d8 100644 --- a/src/util.h +++ b/src/util.h @@ -102,6 +102,9 @@ void iso_datetime_7(uint8_t *buf, time_t t); /** Records the date/time into a 17 byte buffer (ECMA-119, 8.4.26.1) */ void iso_datetime_17(uint8_t *buf, time_t t); +time_t iso_datetime_read_7(const uint8_t *buf); +time_t iso_datetime_read_17(const uint8_t *buf); + typedef struct iso_rbtree IsoRBTree; /** diff --git a/test/test_util.c b/test/test_util.c index 82dcf5d..f8492a3 100644 --- a/test/test_util.c +++ b/test/test_util.c @@ -102,6 +102,33 @@ static void test_iso_bb() CU_ASSERT_EQUAL( buf[3], 0x04 ); } +static void test_iso_datetime_7() +{ + uint8_t buf[7]; + time_t t, t2; + struct tm tp; + + strptime("01-03-1976 13:27:45", "%d-%m-%Y %T", &tp); + t = mktime(&tp); + + iso_datetime_7(buf, t); + CU_ASSERT_EQUAL( buf[0], 76 ); /* year since 1900 */ + CU_ASSERT_EQUAL( buf[1], 3 ); /* month */ + CU_ASSERT_EQUAL( buf[2], 1 ); /* day */ + CU_ASSERT_EQUAL( buf[3], 13 ); /* hour */ + CU_ASSERT_EQUAL( buf[4], 27 ); /* minute */ + CU_ASSERT_EQUAL( buf[5], 45 ); /* second */ + /* the offset depends on current timezone and it's not easy to test */ + //CU_ASSERT_EQUAL( buf[6], 4 ); /* 15 min offset */ + + /* check that reading returns the same time */ + t2 = iso_datetime_read_7(buf); + CU_ASSERT_EQUAL(t2, t); + + //TODO check with differnt timezones for reading and writting + +} + static void test_iso_1_dirid() { CU_ASSERT_STRING_EQUAL( iso_1_dirid("dir1"), "DIR1" ); @@ -238,6 +265,7 @@ void add_util_suite() CU_add_test(pSuite, "iso_bb()", test_iso_bb); CU_add_test(pSuite, "iso_lsb/msb()", test_iso_lsb_msb); CU_add_test(pSuite, "iso_read_lsb/msb()", test_iso_read_lsb_msb); + CU_add_test(pSuite, "iso_datetime_7()", test_iso_datetime_7); CU_add_test(pSuite, "iso_1_dirid()", test_iso_1_dirid); CU_add_test(pSuite, "iso_2_dirid()", test_iso_2_dirid); CU_add_test(pSuite, "iso_1_fileid()", test_iso_1_fileid);