ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I wrote a program to do date calculation. I use it in my scripts, now. But now I have a script I want to make publicly available, yet I don't want to require others to install my date calculation program (more complicated because it also depends on a big library).
So, what I am looking for is if there is any widely available program to do date calculations for the Gregorian calendar. What I need to do is:
1. Given 2 dates in a YYYY-MM-DD format, how many days between them?
2. Given 1 date in YYYY-MM-DD format, and a number of days to add, what is the resulting date?
I could certainly re-arrange YYYY and MM and DD to meet another format. But there must be no ambiguity. A program that is trying to guess whether I am using MM/DD/YYYY vs DD/MM/YYYY is not acceptable. But I prefer the ISO format (YYYY-MM-DD).
Output needs to be simple for use in script backtick output capture, and not need to parse the date out. If an option is required on the command like to get this kind of output, or a formatting like the "date" command has, that would work. It just needs to be simple and reliable in a script.
If the program can also do this with date+time combinations and work with days, hours, minutes, and seconds, that would be a plus for the future. If it only does conversion in both directions between a date and a day number, I could work with that, but would prefer something more direct.
I prefer something normally built in to Linux (and BSD if possible). Requiring the install of a package is to be avoided. If it's really a script in a language like Perl or Python, that would probably work. But the important part is that it already be there somewhere.
Just what would such a program be named? The one I wrote was named "datecalc". There was no such name before, in Debian, Slackware or Ubuntu. Otherwise, I don't think I could guess what name someone else would come up with for it.
How widely implemented is this undocumented feature?
Although "undocumented" this is a basic BASH command line operations with date (like numbers etc.)
so it's not an "extra" unsupported feature (as far as I know)
Maybe the date format could also be converted to Epoch time and then you can do the math operations on it ...(I do, because of different date/time format used DD.MM.YYY) - my other example
It should be pointed out that the -d option is a feature of the gnu version of date. The implementations found on other unix-like systems likely do not have it. On those systems it's probably better to switch to something like perl. If you search the web, you're likely to find all kinds of scripts for calculating date differences.
BTW, there's no need to use bc when most shells have integer arithmetic built-in.
It should be pointed out that the -d option is a feature of the gnu version of date. The implementations found on other unix-like systems likely do not have it. On those systems it's probably better to switch to something like perl. If you search the web, you're likely to find all kinds of scripts for calculating date differences.
I just checked BSD date, and -d is in fact used for something else (setting kernel date). I'm looking for something simple to call from a script that works everywhere.
If I make a switch, it will be to C. I already have code done in C to do this.
Quote:
Originally Posted by David the H.
BTW, there's no need to use bc when most shells have integer arithmetic built-in.
I'm not even sure how widely that is available. But I do use that arithmetic method a lot these days. I never did use bc for shell arithmetic. If the arithmetic got heavy, I switched to C.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.