What is a Unix timestamp?
A Unix timestamp is the number of seconds that have elapsed since the Unix epoch: 1970-01-01 00:00:00 UTC. It is an integer value (commonly stored in C as time_t) that represents an absolute point in time independent of any time-zone. Note that typical Unix timestamps count whole seconds (not milliseconds) and POSIX time does not account for leap seconds.
Task
Implement two functions for working with timestamps in the chat server.
Function 1: get_current_timestamp
- Returns the current Unix timestamp (seconds since epoch)
Function Signature
// Returns the current time as a Unix timestamp
time_t get_current_timestamp(void);Function 2: format_timestamp
- Takes a Unix timestamp and formats it as a string in the format
YYYY-MM-DD HH:MM - Writes the formatted string to the provided buffer
Function Signature
// Format a timestamp as "YYYY-MM-DD HH:MM" and write to buffer
void format_timestamp(time_t timestamp, char* buffer);Timestamp Format
The timestamp format should be:
YYYY-MM-DD HH:MM
Where:
YYYYis the four-digit year, like2024MMis the two-digit month (01-12), like10for OctoberDDis the two-digit day (01-31), like06HHis the two-digit hour in 24-hour format (00-23), like09for 9am or14for 2pmMMis the two-digit minute (00-59), like01or45
All values should be zero-padded. For example:
2024-10-06 09:01(correct)2024-10-6 9:1(incorrect - missing zero padding)
Note
- For
get_current_timestamp, use thetime()system call from<time.h> - For
format_timestamp, uselocaltime()andstrftime()from<time.h> - The
strftime()function with format string"%Y-%m-%d %H:%M"produces the exact format needed - The buffer for
format_timestampwill always be large enough (at least 17 bytes) - Include
<time.h>for the time functions
Example
| Unix Timestamp | Formatted Output |
|---|---|
| 1728219660 | 2024-10-06 09:01 |
| 1728219720 | 2024-10-06 09:02 |
| 1728219840 | 2024-10-06 09:04 |
| 0 | 1970-01-01 00:00 |
| 1609459200 | 2021-01-01 00:00 |
Code
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
time_t get_current_timestamp(void)
{
return time(NULL);
}
void format_timestamp(time_t timestamp, char *buffer)
{
struct tm* timeinfo = localtime(×tamp);
strftime(buffer, 32, "%Y-%m-%d %H:%M", timeinfo);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Usage: %s <timestamp>\n", argv[0]);
return 1;
}
// Test format_timestamp with provided timestamp
time_t t = (time_t)atoll(argv[1]);
char buffer[32];
format_timestamp(t, buffer);
printf("%s\n", buffer);
// Optionally test get_current_timestamp
// time_t current = get_current_timestamp();
// printf("Current timestamp: %ld\n", current);
return 0;
}