Unix Programming - Convert seconds from 1970 to YYYYMMDDhhmmss

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2004 > Convert seconds from 1970 to YYYYMMDDhhmmss





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 Convert seconds from 1970 to YYYYMMDDhhmmss
Adarsh

2004-06-15, 5:56 pm

Hi,

Is there any way to convert back the seconds from 1970 back to
YYYYMMDDHHMMSS format?
For ex if I give the input as 31536000, it should return me as
19710101000000
Can u plz provide me some help in this regard

Regards
Adarsh S
Nils O. Selåsdal

2004-06-15, 5:56 pm

Adarsh wrote:
> Hi,
>
> Is there any way to convert back the seconds from 1970 back to
> YYYYMMDDHHMMSS format?
> For ex if I give the input as 31536000, it should return me as
> 19710101000000
> Can u plz provide me some help in this regard

If you mean convert to a string, check your man page
for localtime(3) for converting it to a struct tm, and
the manpage for strftime(3) to convert it to a string.
Martin Blume

2004-06-15, 5:56 pm

"Adarsh" schrieb
> Hi,
>
> Is there any way to convert back the seconds from 1970 back
> to YYYYMMDDHHMMSS format?
> For ex if I give the input as 31536000, it should return me
> as 19710101000000
> Can u plz provide me some help in this regard
>


$ date --date="00:00:00 1970-01-01 + 31536000 seconds"
Fri Jan 1 00:00:00 CET 1971

HTH
Martin



Dragan Cvetkovic

2004-06-15, 5:56 pm

"Martin Blume" <mblume@socha.net> writes:

> "Adarsh" schrieb
>
> $ date --date="00:00:00 1970-01-01 + 31536000 seconds"
> Fri Jan 1 00:00:00 CET 1971


% date --date="00:00:00 1970-01-01 + 31536000 seconds"
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te=00:00:00 1970-01-01 + 31536000 seconds
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]

% uname -sr
SunOS 5.10

Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
Martin Blume

2004-06-15, 5:56 pm

"Dragan Cvetkovic" schrieb
>
> % date --date="00:00:00 1970-01-01 + 31536000 seconds"
> date: illegal option -- -
> date: illegal option -- d

Too bad for you ... :-(

>
> % uname -sr
> SunOS 5.10
>

$ date --version
date (coreutils) 4.5.8
Written by David MacKenzie.

Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.
$ uname -sr
Linux 2.4.20-4GB

Perhaps I should have indicated that this only applies to
GNU date.

Sorry.
Martin


David Schwartz

2004-06-15, 5:56 pm

Adarsh wrote:
> Hi,
>
> Is there any way to convert back the seconds from 1970 back to
> YYYYMMDDHHMMSS format?
> For ex if I give the input as 31536000, it should return me as
> 19710101000000
> Can u plz provide me some help in this regard


Are you looking for C code? A shell script? A program? What? Local time?
Or UTC?

Most likely, you are looking for localtime, gmtime, and strftime.

DS


Martin Blume

2004-06-16, 5:57 pm

"David Schwartz" schrieb
>
> Are you looking for C code? A shell script? A program?
> What? Local time? Or UTC?
>
> Most likely, you are looking for localtime, gmtime, and
> strftime.
>

He can't have looked for C code, can he? A quick look at
$ man 2 time
and others gives rise to this program:
$ cat tt.c
#include <time.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
// TBD some error checking
time_t tt = atol(argv[1]);
printf("%s", ctime(&tt));
return 0;
}

$ ./tt 31536000
Fri Jan 1 01:00:00 1971
$ ./tt `date +%s`
Wed Jun 16 20:24:11 2004

(Note for those not having "date" from a large African
antelope with a drooping mane and a beard:
date +%s returns the number of seconds since the epoch.
IIRC this is a GNU extension).

HTH
Martin



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com