Rizin
unix-like reverse engineering framework and cli tools
time.c File Reference
#include <rz_util.h>

Go to the source code of this file.

Macros

#define TIME_HFS_SINCE_1970   2082844800u
 
#define TIME_W32_SINCE_1970   0x2b6109100ull
 

Functions

RZ_API int rz_time_gettimeofday (struct timeval *p, struct timezone *tz)
 Get the system current time and the current time zone. More...
 
RZ_API ut64 rz_time_now (void)
 Returns the current time in microseconds. More...
 
RZ_API ut64 rz_time_now_mono (void)
 Returns the current time in microseconds, using the monotonic clock. More...
 
static long get_seconds_since_12am31Dec1969 (struct tm *time)
 
RZ_API RZ_OWN char * rz_time_stamp_to_str (ut32 timestamp)
 Converts an unix epoch timestamp to string. More...
 
RZ_API ut32 rz_time_dos_time_stamp_to_posix (ut32 timestamp)
 Converts dos timestamp to posix timestamp. More...
 
RZ_API bool rz_time_stamp_is_dos_format (const ut32 certainPosixTimeStamp, const ut32 possiblePosixOrDosTimeStamp)
 Verifies that the timestamp is in dos format. More...
 
RZ_API RZ_OWN char * rz_time_date_dos_to_string (ut32 timestamp)
 Converts a dos date (ut32) and returns the timestamp in string format. More...
 
RZ_API RZ_OWN char * rz_time_date_hfs_to_string (ut32 timestamp)
 Converts a Mac HFS+ date (ut32) and returns the timestamp in string format. More...
 
RZ_API RZ_OWN char * rz_time_date_w32_to_string (ut64 timestamp)
 Converts a Win32 date (ut64) and returns the timestamp in string format. More...
 
RZ_API RZ_OWN char * rz_time_date_now_to_string (void)
 Returns the timestamp in string format of the current time (now) More...
 
RZ_API struct tm * rz_localtime_r (RZ_NONNULL const time_t *time, RZ_NONNULL struct tm *res)
 
RZ_API struct tm * rz_gmtime_r (RZ_NONNULL const time_t *time, RZ_NONNULL struct tm *res)
 
RZ_API char * rz_asctime_r (RZ_NONNULL const struct tm *tm, RZ_NONNULL char *buf)
 
RZ_API char * rz_ctime_r (RZ_NONNULL const time_t *timer, RZ_NONNULL char *buf)
 

Macro Definition Documentation

◆ TIME_HFS_SINCE_1970

#define TIME_HFS_SINCE_1970   2082844800u

Definition at line 7 of file time.c.

◆ TIME_W32_SINCE_1970

#define TIME_W32_SINCE_1970   0x2b6109100ull

Definition at line 8 of file time.c.

Function Documentation

◆ get_seconds_since_12am31Dec1969()

static long get_seconds_since_12am31Dec1969 ( struct tm *  time)
inlinestatic

Definition at line 128 of file time.c.

128  {
129  if (time->tm_mday == 31 && time->tm_mon == 11 && time->tm_year == 69) {
130  return time->tm_hour * 3600 + time->tm_min * 60 + time->tm_sec;
131  } else if (time->tm_mon == 0 && time->tm_year == 70) {
132  return 86400 + (time->tm_mday - 1) * 86400 + time->tm_hour * 3600 + time->tm_min * 60 + time->tm_sec;
133  }
134  return -1;
135 }
static static fork const void static count static fd const char const char static newpath char char char static envp time
Definition: sflib.h:42

References time.

Referenced by rz_time_stamp_to_str().

◆ rz_asctime_r()

RZ_API char* rz_asctime_r ( RZ_NONNULL const struct tm *  tm,
RZ_NONNULL char *  buf 
)

Definition at line 304 of file time.c.

304  {
306 #if __WINDOWS__
307  errno_t err = asctime_s(buf, ASCTIME_BUF_MINLEN, tm);
308  return err ? NULL : buf;
309 #else
310  return asctime_r(tm, buf);
311 #endif
312 }
static bool err
Definition: armass.c:435
#define NULL
Definition: cris-opc.c:27
voidpf void * buf
Definition: ioapi.h:138
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define ASCTIME_BUF_MINLEN
Definition: rz_time.h:13

References ASCTIME_BUF_MINLEN, err, NULL, and rz_return_val_if_fail.

Referenced by file_fmttime(), and rz_type_format_time().

◆ rz_ctime_r()

RZ_API char* rz_ctime_r ( RZ_NONNULL const time_t timer,
RZ_NONNULL char *  buf 
)

Definition at line 314 of file time.c.

314  {
315  rz_return_val_if_fail(timer && buf, NULL);
316 #if __WINDOWS__
317  errno_t err = ctime_s(buf, ASCTIME_BUF_MINLEN, timer);
318  return err ? NULL : buf;
319 #else
320  return ctime_r(timer, buf);
321 #endif
322 }

References ASCTIME_BUF_MINLEN, err, NULL, and rz_return_val_if_fail.

Referenced by file_fmttime(), and rz_time_stamp_to_str().

◆ rz_gmtime_r()

RZ_API struct tm* rz_gmtime_r ( RZ_NONNULL const time_t time,
RZ_NONNULL struct tm *  res 
)

Definition at line 294 of file time.c.

294  {
296 #if __WINDOWS__
297  errno_t err = gmtime_s(res, time);
298  return err ? NULL : res;
299 #else
300  return gmtime_r(time, res);
301 #endif
302 }

References err, NULL, rz_return_val_if_fail, and time.

Referenced by file_fmttime(), rz_time_stamp_to_str(), and rz_type_format_time().

◆ rz_localtime_r()

RZ_API struct tm* rz_localtime_r ( RZ_NONNULL const time_t time,
RZ_NONNULL struct tm *  res 
)

Definition at line 284 of file time.c.

284  {
286 #if __WINDOWS__
287  errno_t err = localtime_s(res, time);
288  return err ? NULL : res;
289 #else
290  return localtime_r(time, res);
291 #endif
292 }

References err, NULL, rz_return_val_if_fail, and time.

Referenced by file_fmttime(), and rz_time_stamp_to_str().

◆ rz_time_date_dos_to_string()

RZ_API RZ_OWN char* rz_time_date_dos_to_string ( ut32  timestamp)

Converts a dos date (ut32) and returns the timestamp in string format.

Parameters
timestampThe number to convert to string
Returns
The timestamp in string format

Definition at line 239 of file time.c.

239  {
240  ut32 posix = rz_time_dos_time_stamp_to_posix(timestamp);
241  return rz_time_stamp_to_str(posix);
242 }
uint32_t ut32
RZ_API RZ_OWN char * rz_time_stamp_to_str(ut32 timestamp)
Converts an unix epoch timestamp to string.
Definition: time.c:143
RZ_API ut32 rz_time_dos_time_stamp_to_posix(ut32 timestamp)
Converts dos timestamp to posix timestamp.
Definition: time.c:190

References rz_time_dos_time_stamp_to_posix(), and rz_time_stamp_to_str().

Referenced by rz_cmd_print_timestamp_dos_handler().

◆ rz_time_date_hfs_to_string()

RZ_API RZ_OWN char* rz_time_date_hfs_to_string ( ut32  timestamp)

Converts a Mac HFS+ date (ut32) and returns the timestamp in string format.

Parameters
timestampThe number to convert to string
Returns
The timestamp in string format

Definition at line 250 of file time.c.

250  {
251  timestamp += TIME_HFS_SINCE_1970; // add Mac HFS+ epoch
252  return rz_time_stamp_to_str(timestamp);
253 }
#define TIME_HFS_SINCE_1970
Definition: time.c:7

References rz_time_stamp_to_str(), and TIME_HFS_SINCE_1970.

Referenced by rz_cmd_print_timestamp_hfs_handler().

◆ rz_time_date_now_to_string()

RZ_API RZ_OWN char* rz_time_date_now_to_string ( void  )

Returns the timestamp in string format of the current time (now)

Returns
The timestamp in string format

Definition at line 278 of file time.c.

278  {
279  ut64 now = rz_time_now();
280  now /= RZ_USEC_PER_SEC;
281  return rz_time_stamp_to_str(now);
282 }
#define RZ_USEC_PER_SEC
Definition: rz_time.h:9
RZ_API ut64 rz_time_now(void)
Returns the current time in microseconds.
Definition: time.c:88
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References rz_time_now(), rz_time_stamp_to_str(), RZ_USEC_PER_SEC, and ut64().

Referenced by rz_cmd_debug(), rz_cmd_print_timestamp_current_handler(), rz_diff_unified_json(), and rz_diff_unified_text().

◆ rz_time_date_w32_to_string()

RZ_API RZ_OWN char* rz_time_date_w32_to_string ( ut64  timestamp)

Converts a Win32 date (ut64) and returns the timestamp in string format.

Parameters
timestampThe number to convert to string
Returns
The timestamp in string format

Definition at line 261 of file time.c.

261  {
262  timestamp /= 10000000ll; // 100 nanoseconds to seconds
263  if (timestamp > TIME_W32_SINCE_1970) {
264  timestamp -= TIME_W32_SINCE_1970;
265  } else {
266  // TODO: this usecase is not handled and defaulted to 0
267  timestamp = 0;
268  }
269  time_t t = (time_t)timestamp;
270  return rz_time_stamp_to_str(t);
271 }
int time_t
Definition: sftypes.h:66
#define TIME_W32_SINCE_1970
Definition: time.c:8

References rz_time_stamp_to_str(), and TIME_W32_SINCE_1970.

Referenced by rz_cmd_print_timestamp_ntfs_handler().

◆ rz_time_dos_time_stamp_to_posix()

RZ_API ut32 rz_time_dos_time_stamp_to_posix ( ut32  timestamp)

Converts dos timestamp to posix timestamp.

Parameters
timestampThe timestamp in dos format to convert
Returns
The converted posix timestamp

Definition at line 190 of file time.c.

190  {
191  ut16 date = timestamp >> 16;
192  ut16 time = timestamp & 0xFFFF;
193 
194  /* Date */
195  ut32 year = ((date & 0xfe00) >> 9) + 1980;
196  ut32 month = (date & 0x01e0) >> 5;
197  ut32 day = date & 0x001f;
198 
199  /* Time */
200  ut32 hour = (time & 0xf800) >> 11;
201  ut32 minutes = (time & 0x07e0) >> 5;
202  ut32 seconds = (time & 0x001f) << 1;
203 
204  /* Convert to epoch */
205  struct tm t = { 0 };
206  t.tm_year = year - 1900;
207  t.tm_mon = month > 0 ? month - 1 : month;
208  t.tm_mday = day > 0 ? day : 1;
209  t.tm_hour = hour;
210  t.tm_min = minutes;
211  t.tm_sec = seconds;
212  t.tm_isdst = -1;
213  time_t epochTime = mktime(&t);
214 
215  return (ut32)epochTime;
216 }
uint16_t ut16

References time.

Referenced by _parse_resource_directory(), and rz_time_date_dos_to_string().

◆ rz_time_gettimeofday()

RZ_API int rz_time_gettimeofday ( struct timeval p,
struct timezone tz 
)

Get the system current time and the current time zone.

See https://man7.org/linux/man-pages/man2/gettimeofday.2.html for more information.

Parameters
pPointer to a timeval structure that will be filled by this function
tzPointer to a timezone structure that will be filled by this function
Returns
0 if the function succeeds, -1 on error

Definition at line 78 of file time.c.

78  {
79  return gettimeofday(p, tz);
80 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog gettimeofday
Definition: sflib.h:79
void * p
Definition: libc.cpp:67

References gettimeofday, and p.

Referenced by rz_time_now().

◆ rz_time_now()

RZ_API ut64 rz_time_now ( void  )

Returns the current time in microseconds.

Returns
The current time

Definition at line 88 of file time.c.

88  {
89  ut64 ret;
90  struct timeval now;
92  ret = now.tv_sec * RZ_USEC_PER_SEC;
93  ret += now.tv_usec;
94  return ret;
95 }
RZ_API int rz_time_gettimeofday(struct timeval *p, struct timezone *tz)
Get the system current time and the current time zone.
Definition: time.c:78

References NULL, rz_time_gettimeofday(), RZ_USEC_PER_SEC, timeval::tv_sec, timeval::tv_usec, and ut64().

Referenced by hex_add_to_pkt(), hex_add_to_stale_pkt(), hex_get_instr_at_addr(), hex_to_new_pkt(), make_next_packet_valid(), rz_debug_trace_add(), rz_file_temp(), rz_num_irand(), rz_time_date_now_to_string(), sdb_now(), w32_dbg_wait(), and windbg_wait().

◆ rz_time_now_mono()

RZ_API ut64 rz_time_now_mono ( void  )

Returns the current time in microseconds, using the monotonic clock.

Returns
The current time

Definition at line 102 of file time.c.

102  {
103 #if __WINDOWS__
104  LARGE_INTEGER f;
105  if (!QueryPerformanceFrequency(&f)) {
106  return 0;
107  }
108  LARGE_INTEGER v;
109  if (!QueryPerformanceCounter(&v)) {
110  return 0;
111  }
112  v.QuadPart *= 1000000;
113  v.QuadPart /= f.QuadPart;
114  return v.QuadPart;
115 #elif __APPLE__ && !defined(MAC_OS_X_VERSION_10_12)
116  ut64 ticks = mach_absolute_time();
117  mach_timebase_info_data_t tb;
118  mach_timebase_info(&tb);
119  return ((ticks * tb.numer) / tb.denom) / RZ_NSEC_PER_USEC;
120 #else
121  struct timespec now;
122  clock_gettime(CLOCK_MONOTONIC, &now);
123  return now.tv_sec * RZ_USEC_PER_SEC + now.tv_nsec / RZ_NSEC_PER_USEC;
124 #endif
125 }
const char * v
Definition: dsignal.c:12
#define RZ_NSEC_PER_USEC
Definition: rz_time.h:10
#define f(i)
Definition: sha256.c:46

References f, RZ_NSEC_PER_USEC, RZ_USEC_PER_SEC, timespec::tv_nsec, timespec::tv_sec, ut64(), and v.

Referenced by rz_cmd_help(), rz_cons_break_timeout(), rz_cons_is_breaked(), rz_core_esil_step(), rz_core_file_open(), rz_core_loadlibs(), rz_core_loadlibs_init(), rz_test_main(), rz_test_run_test(), search_collisions(), and subprocess_wait().

◆ rz_time_stamp_is_dos_format()

RZ_API bool rz_time_stamp_is_dos_format ( const ut32  certainPosixTimeStamp,
const ut32  possiblePosixOrDosTimeStamp 
)

Verifies that the timestamp is in dos format.

Parameters
certainPosixTimeStampCertain posix timestamp
possiblePosixOrDosTimeStampPossible posix or dos timestamp to test
Returns
true if format is in dos format, otherwise false

Definition at line 225 of file time.c.

225  {
226  /* We assume they're both POSIX timestamp and thus the higher bits would be equal if they're close to each other */
227  if ((certainPosixTimeStamp >> 16) == (possiblePosixOrDosTimeStamp >> 16)) {
228  return false;
229  }
230  return true;
231 }

Referenced by _parse_resource_directory().

◆ rz_time_stamp_to_str()

RZ_API RZ_OWN char* rz_time_stamp_to_str ( ut32  timestamp)

Converts an unix epoch timestamp to string.

Parameters
timestampThe unix epoch timestamp
Returns
The timestamp in string format

Definition at line 143 of file time.c.

143  {
144  char timestr_buf[ASCTIME_BUF_MINLEN];
145  time_t ts = (time_t)timestamp;
146  struct tm gmt_tm;
147  rz_gmtime_r(&ts, &gmt_tm);
148  struct tm local_tm;
149  rz_localtime_r(&ts, &local_tm);
150  time_t gmt_time;
151  time_t local_time;
152  long diff;
153  if (gmt_tm.tm_mday == 1 && gmt_tm.tm_mon == 0 && gmt_tm.tm_year == 70) {
154  gmt_time = get_seconds_since_12am31Dec1969(&gmt_tm);
155  local_time = get_seconds_since_12am31Dec1969(&local_tm);
156  diff = local_time - gmt_time;
157  } else {
158  gmt_time = mktime(&gmt_tm);
159  local_time = mktime(&local_tm);
160  diff = (long)difftime(local_time, gmt_time);
161  }
162  bool err = gmt_time == -1 || local_time == -1;
163  char *timestr = rz_ctime_r(&ts, timestr_buf);
164  if (timestr) {
165  rz_str_trim(timestr);
166  long hours = diff / 3600;
167  long minutes = labs(diff % 3600 / 60);
168  long seconds = labs(diff % 3600 % 60);
169  if (err) {
170  timestr = rz_str_newf("%s ERR", timestr);
171  } else if (seconds) {
172  timestr = rz_str_newf("%s UTC%+ld:%ld:%ld", timestr, hours, minutes, seconds);
173  } else if (minutes) {
174  timestr = rz_str_newf("%s UTC%+ld:%ld", timestr, hours, minutes);
175  } else if (hours) {
176  timestr = rz_str_newf("%s UTC%+ld", timestr, hours);
177  } else {
178  timestr = rz_str_newf("%s UTC", timestr);
179  }
180  }
181  return timestr;
182 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags long
Definition: sflib.h:79
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str)
Removes whitespace characters (space, tab, newline etc.) from the beginning and end of a string.
Definition: str_trim.c:190
RZ_API struct tm * rz_localtime_r(RZ_NONNULL const time_t *time, RZ_NONNULL struct tm *res)
Definition: time.c:284
RZ_API struct tm * rz_gmtime_r(RZ_NONNULL const time_t *time, RZ_NONNULL struct tm *res)
Definition: time.c:294
static long get_seconds_since_12am31Dec1969(struct tm *time)
Definition: time.c:128
RZ_API char * rz_ctime_r(RZ_NONNULL const time_t *timer, RZ_NONNULL char *buf)
Definition: time.c:314

References ASCTIME_BUF_MINLEN, err, get_seconds_since_12am31Dec1969(), long, rz_ctime_r(), rz_gmtime_r(), rz_localtime_r(), rz_str_newf(), and rz_str_trim().

Referenced by _parse_resource_directory(), bin_pe_init_hdr(), rz_time_date_dos_to_string(), rz_time_date_hfs_to_string(), rz_time_date_now_to_string(), and rz_time_date_w32_to_string().