LinuxQuestions.org
Help answer threads with 0 replies.
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 02-17-2012, 05:11 PM   #1
Skaperen
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
Blog Entries: 31

Rep: Reputation: 180Reputation: 180
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.
 
Old 02-17-2012, 05:43 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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"
 
Old 02-18-2012, 10:15 AM   #3
Skaperen
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
Blog Entries: 31

Rep: Reputation: 180Reputation: 180
How widely implemented is this undocumented feature?
 
Old 02-18-2012, 10:24 AM   #4
lithos
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

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by Skaperen View Post
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.
 
Old 02-18-2012, 10:53 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 ))
 
Old 02-19-2012, 03:17 AM   #6
Skaperen
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
Blog Entries: 31

Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by David the H. View Post
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. View Post
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.
 
Old 04-12-2012, 03:09 AM   #7
hroptatyr
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to calculate the context switches and disk reads that my C program makes Joohi Linux - Newbie 6 02-11-2012 11:41 AM
What other scripts and files are used by a program blueumbrella Linux - Software 1 11-22-2010 09:19 PM
how to calculate program excecute time. UltraSoul Programming 1 04-16-2008 09:41 PM
need a program of c made in linux to calculate a raised to the power b using pow fun mona_iitr Programming 5 03-28-2007 09:56 AM

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

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