Convert UNIX time_t into a date/time text string
Often times, when working with the standard Linux/UNIX time format (time_t) one needs to quickly find out what is the “human readable” date and time text string a particular time_t value corresponds to.
NOTE: The ‘C’ time_t type is the number of seconds elapsed since the “epoch”, the epoch being January 1st 1970 at 00:00:00.
As much as I tried to find a standard *NIX command to do the job I couldn’t.. Eventually I wrote a C two-liner to do the job but I am still curious if there is a standard shell command to do the job, so if anybody knows of one I would appreciate a comment… π
As far as writing it yourself this line of code will do the job :
printf("UNIX time %ld is: %s\n", time_t_var, ctime( &time_t_var ) );
, where the variable time_t_var holds the time_t value…
Because ctime is used to convert time_t into a datetime text string, the text will display the date and time taking into account the current locale settings (timezone and summer time). If you want a ‘raw’ UTC/GMT representation of the time_t value then use gmtime()..
An example using both functions follows:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main(int argc, char **argv) { time_t time_t_var; if (argc > 1) { time_t_var = atoi(argv[1]); } else { time_t_var = time((time_t *) NULL); } printf( "UNIX time %ld is: %s (ctime)\n", time_t_var, ctime( &time_t_var ) ); struct tm tm_val; gmtime_r( &time_t_var, &tm_val ); printf( "UNIX time %ld is: %s (gmtime)\n", time_t_var, asctime(&tm_val) ); return 0; }
3 Responses to Convert UNIX time_t into a date/time text string
Leave a Reply to ench0 Cancel reply
-
Categories
- Android Development
- Bash
- C programming
- dpkg/apt-get
- drupal
- Emacs
- Git
- Java
- Linux administration
- Linux device drivers
- Linux Gaming
- Linux kernel
- Linux Networking
- Linux on Windows
- Linux printing
- Linux sound and ALSA
- Package Managers
- Programming
- RPM
- Shell and environment
- Tips and tricks
- Uncategorized
- VirtualBox
- Virtualization
- web development
- wine
- WMaker
- Wordpress Tips
- X Window System
- XFCE
-
Articles
- August 2020
- August 2019
- May 2019
- July 2017
- February 2017
- January 2017
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- April 2016
- March 2016
- December 2015
- November 2015
- September 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- October 2014
- February 2014
- January 2014
- November 2013
- October 2013
- June 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- October 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- September 2011
- August 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
-
Meta
Cloud
audio bash boot compile C programming cups drupal emacs etc Fedora git grep how to httpd init kernel libc linux linux partition localtime login make mount mp3 mysql networking oracle package managers password phpMyAdmin programming rpm shell sql vbox version control system virtual box vm web server wordpress www xargs xfce xwin yum
It’s slightly arcane, but in the shell you can use the ‘date’ command to translate to and from time_t, like so:
% date –date=’2014-10-24 6:52pm utc’ ‘+%s’
1414176720
% date –date=’1970-01-01 UTC 1414176720 seconds’ +”%Y-%m-%d %T %z”
2014-10-24 14:52:00 -0400
Hi Tom
Thanks for your suggestion. It did not work for me in this exact format which you suggested, probably due to different date util versions requiring slightly different syntax or somth similar (my date util is ver 8.10 )… anyway I fugured it out. π
This did the trick for me:
# date --utc --date="2014-10-24 6:52pm" "+%s"
1414176720
Awesome – thanks again for your tip!
Note: I bet there was a –– before the date param in your post as well but wordpress seems to get rid of it in the comments, converting it into a single -. I had to enclose each of the two dashes in my comment within a HTML ‘bold’ marker to make them appear as a double dash π
And to solve the reverse problem you may use:
$ date -d @1388534400
Wed Jan 1 02:00:00 EET 2014