LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2011, 11:36 PM   #1
geetalicious
LQ Newbie
 
Registered: May 2011
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 1

Rep: Reputation: 0
SOLVED: awk: d/m/yyyy h:mm to yyyy-mm-dd hh:mm:ss


Hi,

I'm trying to process a field
'd/m/yyyy h:mm' to 'yyyy-mm-dd hh:mm:ss' for sql inserts using awk.

I have figured out how to split date only using the following, but am a little confused on include the time part.

Code:
split(b,a,"/")
sprintf("%s-%02d-%02d",a[3],a[2],a[1])
Thanks in advance

Last edited by geetalicious; 05-07-2011 at 08:33 PM.
 
Old 05-05-2011, 02:41 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,

Do you have to use awk?

You could use the date command to change the format to just about anything you like:
Code:
$ ORGDATE="3/11/2010 6:59"
$ date --date="$ORGDATE" '+%Y-%m-%d %H:%M:%S'
2010-03-11 06:59:00
Have a look at the date man page to check out all the other % options.

Hope this helps.
 
1 members found this post helpful.
Old 05-05-2011, 03:18 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by geetalicious View Post
Hi,

I'm trying to process a field
'd/m/yyyy h:mm' to 'yyyy-mm-dd hh:mm:ss' for sql inserts using awk.
Assume you have the field in variable timestamp. First, split it to e.g. timefield array, setting the seconds part to zero if not specified:
Code:
timefields = split(timestamp, timefield, /[\t\n\v\f\r \/:]+/)
if (timefields < 6)
    timefield[6] = 0
Note that it would be a good idea to check you do have at least five timefields, otherwise the timestamp is garbled. Also, you could check the century, unless you are sure your timestamps have the full year.

Finally, reformat the fields the way you prefer. Note that I use %.0f instead of %d for seconds, for proper rounding in case it has a fractional part.
Code:
formattedtime = sprintf("%04d-%02d-%02d %02d:%02d:%02.0f",
                        timefield[3], timefield[2], timefield[1],
                        timefield[4], timefield[5], timefield[6])
Hope this helps,

Last edited by Nominal Animal; 05-08-2011 at 01:36 PM. Reason: fixed timefields[6] -> timefield[6]
 
1 members found this post helpful.
  


Reply



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
[SOLVED] datetime format from MM-DD-YYYY HH:MM:SS to YYYY-MM-DD HH:MM:SS hattori.hanzo Programming 4 10-13-2010 07:04 PM
[SOLVED] dd/mm/yyyy date format Ian D Linux - Newbie 4 01-11-2010 03:24 PM
cron, run at yyyy-mm-dd hh:ii:SS? pungsnurr Linux - Newbie 14 11-26-2009 05:12 PM
logrotate mishandles files with YYYY-MM-DD extension dthacker Linux - General 0 08-20-2004 09:53 AM
Open Office Sorting by HH:MM:SS DD MMMM YYYY toddydog Linux - Software 2 08-10-2003 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

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