[SOLVED] Simple Linux script to convert datetime format to UTC format
Linux - NewbieThis 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.
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 140
Rep:
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!
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 140
Original Poster
Rep:
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.
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.
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.
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 140
Original Poster
Rep:
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?
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 140
Original Poster
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.