image
image

Go Back   macosx.com > Mac Help Forums > Unix & X11

Reply
 
LinkBack Thread Tools
  #1  
Old February 1st, 2012, 08:29 PM
Registered User
 
Join Date: Dec 2000
Location: Gaithersburg, Maryland
Posts: 103
Thanks: 3
Thanked 0 Times in 0 Posts
DominikHoffmann is on a distinguished road
How to convert between date formats on the command line

How would I convert a string, such as
Code:
2012-01-31 18:54:55 +0000
into the format of the
Code:
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
i.e., Epoch time?
Reply With Quote
  #2  
Old February 3rd, 2012, 10:48 AM
Viro's Avatar
Registered User
 
Join Date: Nov 2003
Location: Oxford, UK
Posts: 2,497
Thanks: 0
Thanked 2 Times in 2 Posts
Viro will become famous soon enoughViro will become famous soon enough
Try this:
Code:
date --date='2012-01-31 18:54:55 +0000' +'%a %b %d %T %Z %Y'
That code produces "Tue Jan 31 18:54:55 GMT 2012" which is presumably what you're after.
Reply With Quote
  #3  
Old February 3rd, 2012, 11:09 AM
Registered User
 
Join Date: Dec 2000
Location: Gaithersburg, Maryland
Posts: 103
Thanks: 3
Thanked 0 Times in 0 Posts
DominikHoffmann is on a distinguished road
That command produces the output
Code:
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
Reply With Quote
  #4  
Old February 3rd, 2012, 03:42 PM
Viro's Avatar
Registered User
 
Join Date: Nov 2003
Location: Oxford, UK
Posts: 2,497
Thanks: 0
Thanked 2 Times in 2 Posts
Viro will become famous soon enoughViro will become famous soon enough
Hmm... it looks like the shell utilities that are shipped with OS X aren't standard. Sorry I can't help more as I don't have my Mac with me.
Reply With Quote
  #5  
Old February 3rd, 2012, 03:44 PM
Registered User
 
Join Date: Dec 2000
Location: Gaithersburg, Maryland
Posts: 103
Thanks: 3
Thanked 0 Times in 0 Posts
DominikHoffmann is on a distinguished road
Quote:
Originally Posted by Viro View Post
Try this:
[CODE]
That code produces "Tue Jan 31 18:54:55 GMT 2012" which is presumably what you're after.
I'm looking for the time in seconds since Jan. 1, 1970.
Reply With Quote
  #6  
Old February 3rd, 2012, 05:03 PM
BjarneDM's Avatar
Registered User
 
Join Date: Oct 2005
Location: København ; Danmark ; Europa
Posts: 213
Thanks: 0
Thanked 9 Times in 9 Posts
BjarneDM is a jewel in the roughBjarneDM is a jewel in the roughBjarneDM is a jewel in the rough
You'll have to combine the information from man 1 date and man 3 strftime

Code:
declare myDate="2012-01-31 18:54:55 +0000"
date -j -f "%Y-%m-%d %T %z" "${myDate}" "+%s"
__________________
Bjarne D Mathiesen
København ; Danmark ; Europa

Last edited by BjarneDM; February 3rd, 2012 at 05:08 PM.
Reply With Quote
The Following User Says Thank You to BjarneDM For This Useful Post:
DominikHoffmann (February 3rd, 2012)
Reply

Bookmarks

Tags
conversion, date, format, seconds, time

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -5. The time now is 10:17 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Copyright 2000-2010 DigitalCrowd, Inc.