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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-17-2012, 05:11 PM
|
#1
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,849
Rep: 
|
program to calculate dates for scripts
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.
|
|
|
02-17-2012, 05:43 PM
|
#2
|
Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852
|
You could use good old date:
time difference
Code:
date1="1995-12-20"
date2="2010-10-15"
echo "($(date -d "$date2" "+%s") - $(date -d "$date1" "+%s")) / 86400"|bc
adding days
Code:
date1="1995-12-20"
datediff="25"
date -d "$date1 + $datediff days" "+%F"
|
|
|
02-18-2012, 10:15 AM
|
#3
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,849
Original Poster
Rep: 
|
How widely implemented is this undocumented feature?
|
|
|
02-18-2012, 10:24 AM
|
#4
|
Senior Member
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144
|
Quote:
Originally Posted by Skaperen
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
Last edited by lithos; 02-18-2012 at 10:25 AM.
|
|
|
02-18-2012, 10:53 AM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
Code:
date1="1995-12-20"
date2="2010-10-15"
echo $(( ( $( date -d "$date2" "+%s" ) - $( date -d "$date1" "+%s" ) ) / 86400 ))
|
|
|
02-19-2012, 03:17 AM
|
#6
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,849
Original Poster
Rep: 
|
Quote:
Originally Posted by David the H.
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.
Code:
date1="1995-12-20"
date2="2010-10-15"
echo $(( ( $( date -d "$date2" "+%s" ) - $( date -d "$date1" "+%s" ) ) / 86400 ))
|
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.
|
|
|
04-12-2012, 03:09 AM
|
#7
|
LQ Newbie
Registered: Apr 2012
Posts: 7
Rep: 
|
I wrote a bunch of tools called dateutils. It's a fairly new project however and I'm not aware of any ports or packages.
Question 1:
Code:
ddiff 1995-12-20 2010-10-15
=>
5413
Question 2:
Code:
dadd 1995-12-20 25
=>
1996-01-14
|
|
|
All times are GMT -5. The time now is 04:43 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|