Operating System - HP-UX
1833144 Members
3189 Online
110051 Solutions
New Discussion

Converting timestamp to date/time

 
SOLVED
Go to solution
Joel Shank
Valued Contributor

Converting timestamp to date/time

Can anyone tell me the equation that will convert seconds (since epoch) to month/day/year hour:min:sec format?

Also, is there a function that will do this? If so, how is it used?

Any help will be greatly appreciated!

Thanks,
jls
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: Converting timestamp to date/time

Hi Joel:

You can do this:

# echo "0d1000000000=Y"|adb

replacing the 1000000000 with your timestamp value.

Regards!

...JRF...

S.K. Chan
Honored Contributor

Re: Converting timestamp to date/time

I have this saved-up some time ago. Hope it helps ..
----------------------------------------

Here is the information on how to convert from EPOCH time (time in seconds since 1/1/1970) to our HUMAN time.

There is also an SQL select statement example showing how to convert from EPOCh to HUMAN time included below.

In addition, there is code included that shows how to print the current EPOCH time.

1. Conversion from EPOCH to HUMAN time:

The following is the source code in 'C':

#include
main(argc, argv)

int argc;
char **argv;
{
time_t foo;
foo = atoi(argv[1]);
printf("%s",ctime(&foo));
}

1a. Create a vi file (with extension .c) that includes the above lines.

For example: vi time.c

From the command line type the following:
cc (filename)

For example: cc time.c

This should create an a.out file in same directory.

1b. Simply run the command:

For example:

./a.out 913097859 <<<---digits you want converted

Your output will look like:

Tue Dec 8 00:17:39 1998

2. The SQL 'select' statement can be used to convert ITO Oracle EPOCH fields to human time:

select

TO_DATE(TRUNC(creation_time/ 86400) + 2440588, 'J'),
TO_CHAR(TO_DATE(MOD(creation_time, 86400), 'SSSSS'),'HH24:MI:SS')
from opc_op.opc_act_messages
where node_id = '';

3. Here is source code in 'C' to print current time in EPOCH time:

#include

main()
{
printf("%ld\n",time(NULL));
}

3a. Create a vi file (with extension .c) that include the above lines.

For example: vi time.c

From the command line type the following:
cc (filename)

For example: cc time.c

This should create an a.out file in same directory.

3b. Simply run the command:

For example:

./a.out

Remember EPOCH fields will expire (reach their limit at) year 2037

Steven Gillard_2
Honored Contributor

Re: Converting timestamp to date/time

Try this little C program:

#include
int main(argc, argv)
int argc;
char **argv;
{
time_t t=strtoul(argv[1], NULL, 0);
printf("%s", ctime(&t));
}
/* end */

$ cc -o ctime ctime.c
$ ./ctime 1007570579
Wed Dec 5 16:42:59 2001

Regards,
Steve
Sanjay_6
Honored Contributor

Re: Converting timestamp to date/time

Hi Joel,

Try this,

http://www.faqs.org/faqs/hp/hpux-faq/section-160.html

Hope this helps.

Regds
Joel Shank
Valued Contributor

Re: Converting timestamp to date/time

This is exactly what I needed.

THANKS TO ALL!

jls
Craig Rants
Honored Contributor

Re: Converting timestamp to date/time

I use the method James mentioned a lot. Especially in converting the information found in the /tcb account files into useful information about when users have last logged in and stuff like that.

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
harry d brown jr
Honored Contributor

Re: Converting timestamp to date/time

THis is a cute little program, btw, does anyone think M$ will have replaced their 32bit os by then ( I personally doubt it )?

#include
#include
#include

main()
{
time_t maxt = LONG_MAX; /* 2147483647L */
printf("The end of time as we know it is %s GMT\n",
asctime(gmtime(&maxt)));
maxt = maxt + 1;

printf("One second after will be (in 32bits) %s GMT\n",
asctime(gmtime(&maxt)));
}

$ ./timemach
The end of time as we know it is Tue Jan 19 03:14:07 2038
GMT
One second after will be (in 32bits) Fri Dec 13 20:45:52 1901
GMT
$

live free or die
harry
Live Free or Die