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?
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
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With