LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   convertion of seconds to date and time in linux shell script (https://www.linuxquestions.org/questions/linux-newbie-8/convertion-of-seconds-to-date-and-time-in-linux-shell-script-825997/)

naveenese 08-13-2010 08:32 AM

convertion of seconds to date and time in linux shell script
 
Hi,

I am looking for command or function to convert seconds to date.

In shell script i will get seconds into one varible, I need to convert seconds to date and append to file(Ex: filename.<date-time-format>)

I.e
ts=1280353895 -->This is date and time in seconds.This needs to convert
YYYYMMDDHHMMSS format.

Through date command with options i can get this format.

#date +%Y%m%d%H%M%S ; date +%s; date
20100813061446 --->YYYYMMDDHHMMSS format.(I am looking this format)
1281705286 ----> time in seconds(getting into variable.need to convert)
Fri Aug 13 06:14:46 PDT 2010---->date


Can any one help to get this format?

Thanks in advance.

druuna 08-13-2010 08:39 AM

Hi,

Are you looking for this: date -d @$ts
Or formatted differently: date -d @$ts +"%d-%m-%Y %T %z"

Hope this helps.

sem007 08-13-2010 08:41 AM

Try this

Code:

date --date=@1280353895

sem007 08-13-2010 08:43 AM

druuna you beat me :)


@ OP

as your require format you can use

Code:

date --date=$ts +"+%Y%m%d%H%M%S"
regards,

naveenese 08-16-2010 07:34 AM

Hi sem/druuna,

Thanks for your immediate response to my query.

I got the required format and i used following command.

date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S" [ts=1280353895]

O/P:20100728225135 [YYYYMMDDHHMMSS]

Thanks
Naveen

druuna 08-16-2010 07:44 AM

Hi,

Nice to see you got a working solution!

The 1970-01-01 part is not needed. This date is a standard default on unix/linux systems. You do not need to mention it in your command.

This will do the same as the one you posted: date -d @$ts +"%Y%m%d%H%M%S"

BTW: You're welcome :)

naveenese 08-16-2010 11:41 PM

Druuna,

you are correct The 1970-01-01 part is default linux/unix system time.

with date -d @$ts +"%Y%m%d%H%M%S" this command i am getting following message
date: invalid date `@1280353895'

In my script i am assigning this format to one variable(DATE=`date -d @$ts +"%Y%m%d%H%M%S"`) and using this "DATE" in different places.

But if i use the epoch time (DATE=`date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S"`) i am getting the the required format.

Please correct me if i am wrong.

druuna 08-17-2010 12:56 AM

Hi,

To put the output of a command into a variable you need to do the following:

DATE=$(date -d @$ts +"%Y%m%d%H%M%S")
or
DATE=`date -d @$ts +"%Y%m%d%H%M%S"` -> those are back ticks.

Now you can use $DATE.

Hope this helps.

konsolebox 08-17-2010 01:41 AM

Quote:

Originally Posted by naveenese (Post 4068491)
with date -d @$ts +"%Y%m%d%H%M%S" this command i am getting following message
date: invalid date `@1280353895'

Maybe you're using an old version of date or something that's not compliant to default linux/unix system time.

druuna 08-17-2010 04:35 AM

Hi,

I have to agree with konsolebox on this issue.

What does date --version tell you?

Hope this helps.

naveenese 08-17-2010 06:38 AM

Hi druuna,
Following is the date version.

#date --version
date (coreutils) 5.2.1
Written by David MacKenzie.

Copyright (C) 2004 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.


with following commands i am getting date: invalid date `@1280353895'
DATE=$(date -d @$ts +"%Y%m%d%H%M%S")
or
DATE=`date -d @$ts +"%Y%m%d%H%M%S"`

I am not sure why you are using @$ts?If i am not wrong $ts is enough.

But its working fine with following command
DATE=`date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S"`


Thanks
Naveen

druuna 08-17-2010 06:49 AM

Hi,

Your date version is a bit old and that could be the problem.

I have to use the @ in order to make it work (tried with the oldest date version I could find):
Code:

$ date --version
date (GNU coreutils) 5.96
Copyright (C) 2006 Free Software Foundation, Inc.

$ ts=1280353895
$ date -d @$ts +"%Y%m%d%H%M%S"
20100728235135

$ date -d $ts +"%Y%m%d%H%M%S"
date: invalid date `1280353895'

$ date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S"
20100728225135

As you can see the last command (date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S") also works. I guess you need to use that one in your case.

Hope this helps.

naveenese 08-17-2010 07:07 AM

Thanks a lot druuna.

I am using date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S" in my script.

Thanks
Naveen

druuna 08-17-2010 07:14 AM

You're welcome :)


All times are GMT -5. The time now is 09:05 PM.