LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2010, 08:32 AM   #1
naveenese
LQ Newbie
 
Registered: Aug 2010
Location: Inida,chennai
Posts: 7

Rep: Reputation: 0
Smile 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.
 
Old 08-13-2010, 08:39 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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

Hope this helps.
 
1 members found this post helpful.
Old 08-13-2010, 08:41 AM   #3
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 113Reputation: 113
Try this

Code:
date --date=@1280353895
 
Old 08-13-2010, 08:43 AM   #4
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 113Reputation: 113
druuna you beat me


@ OP

as your require format you can use

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

Last edited by sem007; 08-13-2010 at 08:47 AM.
 
1 members found this post helpful.
Old 08-16-2010, 07:34 AM   #5
naveenese
LQ Newbie
 
Registered: Aug 2010
Location: Inida,chennai
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 08-16-2010, 07:44 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
2 members found this post helpful.
Old 08-16-2010, 11:41 PM   #7
naveenese
LQ Newbie
 
Registered: Aug 2010
Location: Inida,chennai
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 08-17-2010, 12:56 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 08-17-2010, 01:41 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by naveenese View Post
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.
 
1 members found this post helpful.
Old 08-17-2010, 04:35 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I have to agree with konsolebox on this issue.

What does date --version tell you?

Hope this helps.
 
Old 08-17-2010, 06:38 AM   #11
naveenese
LQ Newbie
 
Registered: Aug 2010
Location: Inida,chennai
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

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
 
Old 08-17-2010, 06:49 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
1 members found this post helpful.
Old 08-17-2010, 07:07 AM   #13
naveenese
LQ Newbie
 
Registered: Aug 2010
Location: Inida,chennai
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

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
 
Old 08-17-2010, 07:14 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
  


Reply

Tags
conversion, date, seconds


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting help, time/date manipulation enine Linux - Software 5 02-26-2009 08:04 PM
Using sed in a shell script to add date and time seefor Programming 3 02-25-2009 01:21 PM
Shell Script - Date/Time runnerpaul Programming 9 08-20-2008 07:45 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration