perl_manipulating_date_strings
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| perl_manipulating_date_strings [2025/03/20 19:29] – removed - external edit (Unknown date) 127.0.0.1 | perl_manipulating_date_strings [2025/03/20 19:30] (current) – ↷ Page name changed from manipulating_date_strings to perl_manipulating_date_strings juckins | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== Perl Manipulating Date Strings ==== | ||
| + | Print file modification times: | ||
| + | < | ||
| + | $filename = " | ||
| + | $mtime = (stat $filename)[9]; | ||
| + | chomp($mtime); | ||
| + | $mtime_date = `/bin/date --date=' | ||
| + | |||
| + | Output is: Thursday, 21 October 2010 22:21:49 UTC | ||
| + | </ | ||
| + | |||
| + | Querying a specific timezone to determine DST: | ||
| + | < | ||
| + | [cjuckins@lnxopc2: | ||
| + | America/ | ||
| + | |||
| + | [cjuckins@lnxopc2: | ||
| + | America/ | ||
| + | </ | ||
| + | |||
| + | Manipulating date/time from NWS text products: | ||
| + | < | ||
| + | If you take the date/time line from a current text product, such as: | ||
| + | |||
| + | 806 PM EST TUE 3 JAN 2012 | ||
| + | |||
| + | Add a colon to the hour/ | ||
| + | |||
| + | 8:06 PM EST TUE 3 JAN 2012 | ||
| + | |||
| + | You can feed that string directly into the linux date command and | ||
| + | manipulate it forward and backward by any amount of time and then print | ||
| + | it out in the same format. | ||
| + | |||
| + | Forward 6 hours: | ||
| + | $ date --date=" | ||
| + | %Y" | sed ' | ||
| + | 206 AM EST WED 4 JAN 2012 | ||
| + | |||
| + | Forward 30 minutes: | ||
| + | date --date=" | ||
| + | %b %Y" | sed ' | ||
| + | 836 PM EST TUE 3 JAN 2012 | ||
| + | |||
| + | Backward 12 hours: | ||
| + | date --date=" | ||
| + | %Y" | sed ' | ||
| + | 806 AM EST TUE 3 JAN 2012 | ||
| + | |||
| + | The 1st sed strips any leading zeros at the beginning of the line, the | ||
| + | 2nd sed strips any other leading zeros, and the final sed makes it all | ||
| + | UPPER. | ||
| + | </ | ||
| + | |||
| + | Other file stats info: | ||
| + | [[http:// | ||
| + | |||
| + | Using Linux ' | ||
| + | < | ||
| + | [juckins@lightning: | ||
| + | Sat Nov 19 23:00:00 EST 2011 | ||
| + | </ | ||
| + | |||
| + | More examples of ' | ||
| + | < | ||
| + | [juckins@lightning: | ||
| + | Sat Nov 19 18:00:00 EST 2011 | ||
| + | |||
| + | [juckins@lightning: | ||
| + | Sat Nov 19 19:01:00 EST 2011 | ||
| + | |||
| + | [juckins@lightning: | ||
| + | Sat Nov 19 16:59:00 EST 2011 | ||
| + | |||
| + | $date --date=" | ||
| + | 121002/0800 | ||
| + | |||
| + | $ date --date=" | ||
| + | 121003/1800 | ||
| + | |||
| + | $ date --date=" | ||
| + | 121002/1200 | ||
| + | |||
| + | $ date -d @1446390000 | ||
| + | Sun Nov 1 15:00:00 UTC 2015 | ||
| + | </ | ||
| + | |||
| + | Converting from seconds and a different timezone: | ||
| + | TZ=": | ||
| + | TZ=": | ||
| + | TZ=": | ||
