LinuxQuestions.org
Visit Jeremy's Blog.
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 10-09-2009, 06:48 AM   #1
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Rep: Reputation: 21
Smile Simple Linux script to convert datetime format to UTC format


Hi, just wondering is there a simple script to convert datetime to UTC format. I have been searching different forums but most answers are for converting UTC to datetime. For example what is a simple command/script to convert todays datetime to UTC format i.e. '2009-10-09 11:47:59'. Hope someone can help!
 
Old 10-09-2009, 06:58 AM   #2
mathew_p_a
LQ Newbie
 
Registered: Oct 2009
Posts: 7
Blog Entries: 1

Rep: Reputation: 1
May be this might help.

date "+%F %T"


Eg:

Quote:
# date
Fri Oct 9 16:12:21 IST 2009

# date "+%F %T"
2009-10-09 16:12:24
 
Old 10-09-2009, 07:07 AM   #3
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Original Poster
Rep: Reputation: 21
Sorry I should be more specific, converting for example '2009-10-09 11:58:59' to an integer like 1254304212 (UTC time in seconds). So the command would echo or print the integers like 1254304212 for example.
 
Old 10-09-2009, 07:07 AM   #4
cardy
Member
 
Registered: Jan 2005
Location: Shropshire, England, UK
Distribution: RedHat, Fedora, CentOS..........
Posts: 121

Rep: Reputation: 19
For the current time you can do this with date

Quote:
date "+%Y-%m-%d %H:%M:%S %Z"
will print the current date (with the addition of the time zone you can remove this by removing the %Z in your local time zone)

e.g.
Quote:
date "+%Y-%m-%d %H:%M:%S %Z"
2009-10-09 12:05:15 BST
Adding -u tells it to print UTC time

Quote:
date -u "+%Y-%m-%d %H:%M:%S %Z"
2009-10-09 11:05:51 UTC
To print the current UTC time in the format you described

Quote:
date -u "+%Y-%m-%d %H:%M:%S"
2009-10-09 11:07:06

Last edited by cardy; 10-09-2009 at 07:12 AM.
 
Old 10-09-2009, 07:13 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by shayno90 View Post
Sorry I should be more specific, converting for example '2009-10-09 11:58:59' to an integer like 1254304212 (UTC time in seconds). So the command would echo or print the integers like 1254304212 for example.
"man date" for info on how to use date. You can do what you want with date.
 
Old 10-09-2009, 07:16 AM   #6
cardy
Member
 
Registered: Jan 2005
Location: Shropshire, England, UK
Distribution: RedHat, Fedora, CentOS..........
Posts: 121

Rep: Reputation: 19
try

Quote:
date -u +%s
 
Old 10-09-2009, 07:26 AM   #7
Lordandmaker
Member
 
Registered: Sep 2005
Location: London, UK
Distribution: Debian
Posts: 258

Rep: Reputation: 38
Code:
date -d "2009-10-09 11:58:59" +%s
will echo the timestamp of the time 2009-10-09 11:58:59

To get a bash script to do it, you'd want something like
Code:
/bin/bash
date -d "$1" +%s
Where the first argument is the date in the above format (in quotes to preserve the space). $1 always contains the first argument to the script in bash, $2 the second and so on.
 
Old 10-09-2009, 07:27 AM   #8
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Original Poster
Rep: Reputation: 21
Thanks cardy I tried the command for example

echo 2009-10-09 08:39:59| date -u +%s

and the output I got was

1255087420

Is there any way to verify if this correct, that the nano seconds equals that specified time?
 
Old 10-09-2009, 07:34 AM   #9
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Original Poster
Rep: Reputation: 21
thanks lordandmaker, i tried the bash script with the previous datetime but the integers were different from the output of the previous command for example

date -d "2009-10-09 08:39:59" +%s

1255073999

but

echo 2009-10-09 08:39:59| date -u +%s

1255087420

any ideas why this is so? and which is the correct one?
 
Old 10-09-2009, 07:41 AM   #10
Lordandmaker
Member
 
Registered: Sep 2005
Location: London, UK
Distribution: Debian
Posts: 258

Rep: Reputation: 38
I'm guessing it's only getting the date portion, and not the time portion.

Try giving $1 and $2 to the date command in the script, or play with quote marks. I _always_ have to refer to the Internet when I write a bash script.
 
Old 10-09-2009, 09:19 AM   #11
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Original Poster
Rep: Reputation: 21
Thanks guys, the first bash script works perfect for getting the UTC time output as integers. I just verified doing a quick calculation in a spreadsheet versus the other bash command.
 
  


Reply

Tags
convert, date, utc


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
A script to convert maildir to mailbox format i_s Linux - General 7 12-14-2009 04:20 PM
How to convert rm format music files to mp3 format me4linux Linux - Software 2 05-15-2007 02:45 PM
script to convert dos to unix format kapilcool Linux - Software 3 06-15-2006 12:50 PM
comparing datetime format in php mohtasham1983 Programming 2 01-26-2006 08:54 AM
use vi to convert linux format to windows format intolinux Linux - Software 2 12-10-2003 11:20 PM


All times are GMT -5. The time now is 07:24 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration