getdate_r - Linux


getdate_r fetches the current date and time and converts it to a broken-down time structure. It is typically used to obtain system time information for applications that require precise timestamp handling or time-based operations.


int getdate_r(struct tm *tm);




Get the current date and time:

struct tm tm;
printf("Current date and time: %s\n", asctime(&tm));

Convert a specific timestamp to a broken-down time structure:

time_t timestamp = 1577836800;
struct tm *tm;
tm = gmtime_r(&timestamp, tm);
printf("Date for timestamp (GMT): %s\n", asctime(tm));

Common Issues

  • Incorrect time zone: If the system time zone is not correctly set, the retrieved timestamp may be inaccurate.
  • Insufficient memory: If the provided tm structure does not have enough allocated memory, the function may fail.


getdate_r can be integrated with other time-related commands and routines, such as:

  • time: Get the current time in seconds since the Epoch.
  • gmtime_r: Convert a time value to Coordinated Universal Time (UTC).
  • strftime: Format a tm structure into a custom string representation.

Related Commands

  • date: Print the current date and time.
  • cal: Display a monthly calendar.
  • timedatectl: Manage system time settings.