diff options
| author | mo khan <mo@mokhan.ca> | 2014-07-24 20:33:18 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-07-24 20:33:18 -0600 |
| commit | 41d7e47ce312338c6daf967f673f9572ca0fb649 (patch) | |
| tree | 166fd478b634ae1721a68325bd63439a56287c33 | |
| parent | 208fdcefd94a525c8f444ac899a89de2087b3449 (diff) | |
add second time example.
| -rw-r--r-- | time_example2.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/time_example2.c b/time_example2.c new file mode 100644 index 0000000..032b922 --- /dev/null +++ b/time_example2.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <time.h> + +void dump_time_struct_bytes(struct tm *time_ptr, int size) +{ + int i; + unsigned char *raw_ptr; + printf("bytes of struct located at 0x%p\n", time_ptr); + raw_ptr = (unsigned char *) time_ptr; + for (i = 0; i < size; i++) { + printf("%02x ", raw_ptr[i]); + if (i % 16 == 15) + printf("\n"); + } + printf("\n"); +} + +int main(int argc, const char *argv[]) +{ + long int seconds_since_epoch; + struct tm current_time, *time_ptr; + int hour, minute, second, i, *int_ptr; + + seconds_since_epoch = time(0); + printf("time() - seconds since epoch: %ld\n", seconds_since_epoch); + + time_ptr = ¤t_time; + localtime_r(&seconds_since_epoch, time_ptr); + + hour = current_time.tm_hour; + minute = time_ptr->tm_min; + second = *((int *) time_ptr); + + printf("Current time is: %02d:%02d:%02d\n", hour, minute, second); + + dump_time_struct_bytes(time_ptr, sizeof(struct tm)); + + minute = hour = 0; + int_ptr = (int *) time_ptr; + + for (i = 0; i < 3; i++) { + printf("int_ptr @ 0x%p : %d\n", int_ptr, *int_ptr); + int_ptr++; + } + + return 0; +} |
