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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
08-13-2010, 08:32 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2010
Location: Inida,chennai
Posts: 7
Rep:
|
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.
|
|
|
|
08-13-2010, 08:39 AM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,703
|
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.
|
08-13-2010, 08:41 AM
|
#3
|
|
Member
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638
Rep: 
|
Try this
Code:
date --date=@1280353895
|
|
|
|
08-13-2010, 08:43 AM
|
#4
|
|
Member
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638
Rep: 
|
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.
|
08-16-2010, 07:34 AM
|
#5
|
|
LQ Newbie
Registered: Aug 2010
Location: Inida,chennai
Posts: 7
Original Poster
Rep:
|
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
|
|
|
|
08-16-2010, 07:44 AM
|
#6
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,703
|
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.
|
08-16-2010, 11:41 PM
|
#7
|
|
LQ Newbie
Registered: Aug 2010
Location: Inida,chennai
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
08-17-2010, 12:56 AM
|
#8
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,703
|
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.
|
|
|
|
08-17-2010, 01:41 AM
|
#9
|
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,046
Rep: 
|
Quote:
Originally Posted by naveenese
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.
|
08-17-2010, 04:35 AM
|
#10
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,703
|
Hi,
I have to agree with konsolebox on this issue.
What does date --version tell you?
Hope this helps.
|
|
|
|
08-17-2010, 06:38 AM
|
#11
|
|
LQ Newbie
Registered: Aug 2010
Location: Inida,chennai
Posts: 7
Original Poster
Rep:
|
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
|
|
|
|
08-17-2010, 06:49 AM
|
#12
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,703
|
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.
|
08-17-2010, 07:07 AM
|
#13
|
|
LQ Newbie
Registered: Aug 2010
Location: Inida,chennai
Posts: 7
Original Poster
Rep:
|
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
|
|
|
|
08-17-2010, 07:14 AM
|
#14
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,703
|
You're welcome 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:12 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|