Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Unix timestamp with ctime in c

I'm trying to format a 10-digit Unix time stamp (currently a string) using ctime.

However, ctime() expects a parameter of type time_t, not a string.

What must I do before I can use ctime? In other words, can I easily convert the string into a time_t?

like image 962
jwheels Avatar asked Sep 05 '12 17:09

jwheels


People also ask

What is Unix timestamp in C#?

Remarks. Unix time represents the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC). It does not take leap seconds into account. This method returns the number of milliseconds in Unix time.

Can Unix time be stored in int?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC. One of the primary benefits of using Unix time is that it can be represented as an integer making it easier to parse and use across different systems.

What format is Unix timestamp?

Unix epoch timestamps are supported in the following formats: 10 digit epoch time format surrounded by brackets (or followed by a comma). The digits must be at the very start of the message. For example, [1234567890] or [1234567890, other] followed by the rest of the message.


1 Answers

You're saying you have something like 1346426869 as a string and want it to be a time_t?

time_t raw_time = atoi("1346426869");
printf("current time is %s",ctime(&raw_time));

> current time is Fri Aug 31 11:27:49 2012
like image 88
Mike Avatar answered Sep 20 '22 01:09

Mike