how to convert given no. into unix dd:mm:yyyy -hh:mm:ss format

how to convert given no. into unix dd:mm:yyyy -hh:mm:ss format

Post by cgdeth » Sun, 20 May 2001 11:52:43



consider that I have a number with me as follows ,
948978564 which is total number of seconds elapsed from 1st Jan 1970.
Now I want to extract hour,minute and seconds embeded into this number.
how do I do it? Which C functions helps me?

--
with regards,
-cgdethe.

 
 
 

how to convert given no. into unix dd:mm:yyyy -hh:mm:ss format

Post by Tim Heane » Mon, 21 May 2001 00:21:59



> consider that I have a number with me as follows ,
> 948978564 which is total number of seconds elapsed from 1st Jan 1970.
> Now I want to extract hour,minute and seconds embeded into this number.
> how do I do it? Which C functions helps me?

You want either gmtime or localtime, whichever the case may be. Read
the man page carefully; some of the elements of a struct tm are
perhaps not what you would guess. For example, the month

  tm_mon  The number of months since January, in the range 0 to 11.

and the year

  tm_year  The number of years since 1900.

are probably not the same as your mm and yyyy. Of course, if you
really wanted ctime, then you don't have to worry about that.

Note that you can access these same functions in Perl, so you can do
something quick like

  $ perl -e 'print scalar gmtime(948978564), "\n"'
  Thu Jan 27 13:09:24 2000

Hope this helps,

Tim