Unix questions - HowTo convert "seconds since `00:00:00 1970-01-01 UTC" into a human readable

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > April 2004 > HowTo convert "seconds since `00:00:00 1970-01-01 UTC" into a human readable





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author HowTo convert "seconds since `00:00:00 1970-01-01 UTC" into a human readable
Hermann Peifer

2004-04-14, 2:38 pm

Hi All,

Here my small problem:

I have a list with timestamps expressed as
"seconds since `00:00:00 1970-01-01 UTC", see here:

1081784344
1081784346
1081784347
1081784545
1081784546
1081784658
....

I would like to convert them into human readable dates, e.g.

2004-04-13 20:06:37

IMHO, the solution seems to be around strftime() or strptime() functions,
but I don't manage to get it right.

Any solution is welcome, preferably a simple one, e.g. a shell oneliner.

Regards, Hermann
Jens.Toerring@physik.fu-berlin.de

2004-04-14, 2:38 pm

Hermann Peifer <peifer@gmx.net> wrote:
> Here my small problem:


> I have a list with timestamps expressed as
> "seconds since `00:00:00 1970-01-01 UTC", see here:


> 1081784344
> 1081784346
> 1081784347
> 1081784545
> 1081784546
> 1081784658
> ...


> I would like to convert them into human readable dates, e.g.


> 2004-04-13 20:06:37


> IMHO, the solution seems to be around strftime() or strptime() functions,
> but I don't manage to get it right.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main( void )
{
char buf[ 100 ];
long tval = 1081784344;

strftime( buf, sizeof( buf ), "%Y-%m-%d %T",
localtime( ( time_t * ) &tval ) );
printf( "%s\n", buf );
strftime( buf, sizeof( buf ), "%Y-%m-%d %T",
gmtime( ( time_t * ) &tval ) );
printf( "%s\n", buf );

return EXIT_SUCCESS;
}

Use the first for the local time, the second for GMT.

> Any solution is welcome, preferably a simple one, e.g. a shell oneliner.


perl -e 'use POSIX; print strftime "%F %T\n", localtime 1081784658'

or

perl -e 'use POSIX; print strftime "%F %T\n", gmtime 1081784658'

BTW, you can also use "%F" instead of "%Y-%m-%d" in the C version if
you have a C99 compliant compiler (it works e.g. with gcc but you
may get a warning).
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Chris F.A. Johnson

2004-04-14, 2:38 pm

On Tue, 13 Apr 2004 at 18:09 GMT, Hermann Peifer wrote:
> Hi All,
>
> Here my small problem:
>
> I have a list with timestamps expressed as
> "seconds since `00:00:00 1970-01-01 UTC", see here:
>
> 1081784344
> 1081784346
> 1081784347
> 1081784545
> 1081784546
> 1081784658
> ...
>
> I would like to convert them into human readable dates, e.g.
>
> 2004-04-13 20:06:37
>
> IMHO, the solution seems to be around strftime() or strptime() functions,
> but I don't manage to get it right.
>
> Any solution is welcome, preferably a simple one, e.g. a shell oneliner.


gawk 'BEGIN {print strftime("%Y-%m-%d %H:%M:%S",1081784344)}'


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Randal L. Schwartz

2004-04-14, 2:38 pm

>>>>> "Hermann" == Hermann Peifer <peifer@gmx.net> writes:

Hermann> Any solution is welcome, preferably a simple one, e.g. a shell oneliner.

perl -lpe 'print scalar localtime $_' <file_of_numbers >list_of_strings

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment PERL training!
Hermann Peifer

2004-04-14, 2:38 pm

Hermann Peifer wrote:

> Hi All,
>
> Here my small problem:
>
> I have a list with timestamps expressed as
> "seconds since `00:00:00 1970-01-01 UTC", see here:
>
> ...
>
> I would like to convert them into human readable dates, e.g.
>


Thanks to all of you. All proposed solutions do work fine.

Regards, Hermann
gmt

2004-04-14, 9:34 pm

Hermann Peifer wrote:

> Hi All,
> I have a list with timestamps expressed as
> "seconds since `00:00:00 1970-01-01 UTC", see here:
> 1081784658
> ...
> I would like to convert them into human readable dates, e.g.
>
> 2004-04-13 20:06:37
>
> IMHO, the solution seems to be around strftime() or strptime() functions,
> but I don't manage to get it right.
>
> Any solution is welcome, preferably a simple one, e.g. a shell oneliner.



just use date

inode@breadbox:~$ date -d '1970-01-01 1081784658 sec' +"%Y-%m-%d %T %z"
2004-04-12 15:44:18 +0100

Bill Marcum

2004-04-15, 1:55 pm

On Thu, 15 Apr 2004 01:26:31 GMT, gmt
<gmt@gotmail.com> wrote:
> Hermann Peifer wrote:
>
>
> just use date
>
> inode@breadbox:~$ date -d '1970-01-01 1081784658 sec' +"%Y-%m-%d %T %z"
> 2004-04-12 15:44:18 +0100
>

That works if you have GNU date.

--
Giraffe: a ruminant with a view.
Web Surfer

2004-04-18, 10:42 am

[This followup was posted to comp.unix.questions]

11.news.tele.dk>, peifer@gmx.net says...
> Hi All,
>
> Here my small problem:
>
> I have a list with timestamps expressed as
> "seconds since `00:00:00 1970-01-01 UTC", see here:
>
> 1081784344
> 1081784346
> 1081784347
> 1081784545
> 1081784546
> 1081784658
> ...
>
> I would like to convert them into human readable dates, e.g.
>
> 2004-04-13 20:06:37
>
> IMHO, the solution seems to be around strftime() or strptime() functions,
> but I don't manage to get it right.
>
> Any solution is welcome, preferably a simple one, e.g. a shell oneliner.
>
> Regards, Hermann
>


Here is the source code for a very small C program that can do the job :

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
time_t clock;

clock = (time_t)atoi(argv[1]);
printf("%s",ctime(&clock));
exit(0);
}
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com