Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print time_t in a specific format?

ls command prints time in this format:

Aug 23 06:07 

How can I convert time received from stat()'s mtime() into this format for local time?

like image 223
kBisla Avatar asked Aug 24 '13 19:08

kBisla


People also ask

How to print time_t value in printf?

printf doesn't support time_t (other than printing the numeric value of the time_t). You should consider strftime to format the time as a string then use printf to print the string. This is exactly what I want - to print numeric value, in this case 1379656246000 I'm building Win32 app under Win7 64 bit in Visual Studio 2010 Pro

How do I print current date and time in go?

In Go, the current time can be determined by using time.Now (), provided by the time package. Package time provides functionality for measuring and displaying the time. To print Current date-time you need to import the “time” package in your Go program to work with date and time.

What is the best way to format time in datetime?

The easiest and most efficient way of doing time formatting in the .NET world is to take advantage of the DateTime structure that provides methods such as DateTime.ToString and DateTime.Parse. These methods allow you to perform culture-sensitive operations on a DateTime object.

How to print date and time in Golang?

Package time provides functionality for measuring and displaying the time. To print Current date-time you need to import the “time” package in your Go program to work with date and time. To print Specific date-time in Golang use format constants provided in the time package.


1 Answers

Use strftime (you need to convert time_t to struct tm* first):

char buff[20];
struct tm * timeinfo;
timeinfo = localtime (&mtime);
strftime(buff, sizeof(buff), "%b %d %H:%M", timeinfo);

Formats:

%b - The abbreviated month name according to the current locale.

%d - The day of the month as a decimal number (range 01 to 31).

%H - The hour as a decimal number using a 24-hour clock (range 00 to 23).

%M - The minute as a decimal number (range 00 to 59).

Here is the full code:

struct stat info; 
char buff[20]; 
struct tm * timeinfo;

stat(workingFile, &info); 

timeinfo = localtime (&(info.st_mtime)); 
strftime(buff, 20, "%b %d %H:%M", timeinfo); 
printf("%s",buff);
like image 58
Nemanja Boric Avatar answered Sep 20 '22 18:09

Nemanja Boric