xref: /illumos-gate/usr/src/contrib/mDNSResponder/mDNSPosix/posix_utilities.c (revision 472cd20d26008f77084ade4c2048159b98c2b705)
1 //
2 //  posix_utilities.c
3 //  mDNSResponder
4 //
5 //  Copyright (c) 2019 Apple Inc. All rights reserved.
6 //
7 
8 #include "posix_utilities.h"
9 #include "mDNSEmbeddedAPI.h"
10 #include <stdlib.h>                 // for NULL
11 #include <stdio.h>                  // for snprintf
12 #include <time.h>
13 #include <sys/time.h>               // for gettimeofday
14 
getLocalTimestamp(char * const buffer,mDNSu32 buffer_len)15 mDNSexport void getLocalTimestamp(char * const buffer, mDNSu32 buffer_len)
16 {
17     struct timeval      now;
18     struct tm           local_time;
19     char                date_time_str[32];
20     char                time_zone_str[32];
21 
22     gettimeofday(&now, NULL);
23     localtime_r(&now.tv_sec, &local_time);
24 
25     strftime(date_time_str, sizeof(date_time_str), "%F %T", &local_time);
26     strftime(time_zone_str, sizeof(time_zone_str), "%z", &local_time);
27     snprintf(buffer, buffer_len, "%s.%06lu%s", date_time_str, (unsigned long)now.tv_usec, time_zone_str);
28 }
29