summaryrefslogtreecommitdiff
path: root/time_example2.c
blob: 032b9222960d07bd0a6f7ca80aac6ba5f1f0e4dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 = &current_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;
}