LinuxQuestions.org
Visit Jeremy's Blog.
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 08-01-2008, 03:46 PM   #1
hockeyman_102
Member
 
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Rep: Reputation: 15
Lightbulb shell date with +%j


Hi, so a program was prebuilt before my time here that names files (I assume) with the 'date +%j' because they all correspond to the current year, but in a 001-365 format. "Filename.212.gz" for example. I need to be able to grab one of these files and parse through it (done) but now they want it pretty with the actual date i.e. "July 28". Is there any quick way to use the linux 'date' cmd to my advantage? I would love to use something in place instead of taking up memory... I don't mind writing it out, but just thought I would ask. Thanks!

I've found a TON of programs on google about the gregorian calendar and conversions, but if there is an easier way - I'm all for it.
 
Old 08-01-2008, 05:01 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Here's your shell script. You set yday (day of year) and year (since 1900)

Code:
$ yday=212
$ year=108
$ perl -M'POSIX(strftime)' -e "print strftime( \"%B %d\", 0, 0, 0, 0, 0, $year, -1, $yday );"      
July 31
 
Old 08-01-2008, 05:53 PM   #3
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Ah nice!

I've always done this by passing epoch time of the # of days given, adjusted by timezone through localtime and grab the appropriate field in the resulting array.

This certainly saves some effort, thanks for the tip Mr C
 
Old 08-01-2008, 06:03 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
You're welcome.

You can add a final parameter for specifying daylight saving time. isdst value of 0 or 1. I figured the OP wouldn't care about this since we're assuming midnight here.
 
Old 08-02-2008, 08:44 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
There is a little known function in the date command to add an offset to a date string.

date -d "1 jan 2008 212 days"
Thu Jul 31 00:00:00 GMT+4 2008

Of course you can specify the exact date format for output:
date -d "1 jan 2008 212 days" +"%b %d"
Jul 31

Any offset can be added, but you have to tell what the number means, e.g. sec or days


jlinkels
 
Old 08-02-2008, 11:36 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Nice jlinkels.
 
Old 08-02-2008, 12:51 PM   #7
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Bonzer jlinkels!
 
Old 08-02-2008, 03:10 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can also go backward:
Code:
# date -d "20080711 67 days ago"
Mon May  5 00:00:00 CEST 2008
Furthermore, you can combine different time units:
Code:
# date -d "20080711 67 days ago 2 hours ago"
Sun May  4 22:00:00 CEST 2008
 
Old 08-04-2008, 06:24 PM   #9
hockeyman_102
Member
 
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 15
WOW! Very nice! Thank you!!!
 
Old 08-05-2008, 12:36 PM   #10
hockeyman_102
Member
 
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 15
Okay, I have another problem... This worked GREAT on my Suse and Ubuntu boxes, but I have a FREEBSD machine that the "date -d" is all screwed up... I found the man pages, but if anyone can help do the same thing as above, but with the freebsd version i would GREATLY appreciate it.

http://nixdoc.net/man-pages/FreeBSD/date.1.html
 
Old 08-05-2008, 12:42 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
According to the man page, the -d option has a totally different meaning on FreeBSD. On Unix machines, where the -d option did not exist, I used a custom Fortran code which accepted a date in yyyymmdd format and a number of days to add or subtract. If you are experienced in programming you can try to write a C or a Fortran code to do the required operations. Or eventually you can write some function in python or in another scripting language that can manage dates.

Or eventually install the GNU date on FreeBSD, but sincerely I don't know if it is possible or if there is a precompiled package that provides such functionality.

Last edited by colucix; 08-05-2008 at 12:44 PM.
 
Old 08-05-2008, 01:54 PM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Does the perl script I gave you now work?

You've run into the standard cross-platform utilities problem. This is an important requirement to mention at the beginning of your problem. Otherwise, as you found out, solutions won't work.

I generally try to keep portability in mind when I propose solutions.
 
Old 08-05-2008, 02:06 PM   #13
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Hmmm, never knew that BSD was so much different from Linux.

I have found python-parsedatetime which is a module able to parse a human expression date/time. Maybe it is easier than writing from scratch.

jlinkels
 
Old 08-06-2008, 01:21 PM   #14
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Quote:
Originally Posted by colucix View Post
Or eventually install the GNU date on FreeBSD, but sincerely I don't know if it is possible or if there is a precompiled package that provides such functionality.
I would highly recommend this route. `date` is tried and true and will take things into account such as leap years, etc. You can install the GNU utilities on damn near everything including Solaris.
 
  


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
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Shell Script for Unix Date ??? ajeetraina Linux - Newbie 11 02-08-2008 12:58 AM
shell scripting and 'date' matttail Programming 8 05-02-2006 10:56 PM
Shell Script.. Date format handling C-RAF. Programming 2 02-14-2006 08:34 AM
date manipulation in shell peal_ss Programming 4 03-29-2004 04:43 AM

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

All times are GMT -5. The time now is 04:51 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