0

I am using the date command in a shell script to increment startdate by a given number of days (1 day for example):

EndDateTime=$(date -d "$StartDateTime 1 days" "+%Y-%m-%d %H:%M:%S");

The problem is that i get wrong result for somes cases:

StartDateTime       | EndDateTime        
2016-02-04 15:57:21 | 2016-02-05 15:57:21

2016-02-04 16:01:18 | 2016-02-05 15:59:41

The first result is exact but the second is wrong and they are computed on same computer by same script in same time.

I am working on Ubuntu with intel 64 family 6 model 23 stepping 10 processor.

Please advice me and thanks in advance.

hash
  • 11
  • 3
  • 1
    Could you provide the original source code? Is it bash? Otherwise it's hard to answer. – Tombart Feb 04 '16 at 23:27
  • 1
    Possible duplicate of [How to increment a date in a bash script](http://stackoverflow.com/questions/18706823/how-to-increment-a-date-in-a-bash-script) – Tombart Feb 04 '16 at 23:29
  • __and they are computed on same computer by same script in **same time**__. But the start time of the second one is 4 minutes late. It could be some processes between the 2 that caused some delays to compute the start and the end. – alvits Feb 04 '16 at 23:33
  • the startdatetime is passed as variable with a given value as you can see in command above. thanks for your cooperation – hash Feb 04 '16 at 23:35
  • If you manually set the start date/time to the second value and run the calculation again, do you still get the different result? What is the exact format of the string in `$StartDateTime`? How do you set it? I cannot reproduce, but I've not yet tried on Ubuntu (I'm using an archaic RHEL 5). Which version of Ubuntu are you using? – Jonathan Leffler Feb 04 '16 at 23:54
  • when i set it manually and execute on terminal i get correct result. '$StarDateTime' format is the same as in example i get it by reformating datetime string 20160204160118 using below command: 'StartDateTime="${String:0:4}-${String:4:2}-${String:6:2} ${String:8:2}:${String:10:2}:${String:12:2}"' thanks – hash Feb 05 '16 at 00:09
  • I am using Ubuntu 14.04 – hash Feb 05 '16 at 00:18
  • Cannot reproduce: `String=20160204160118; StartDateTime="${String:0:4}-${String:4:2}-${String:6:2} ${String:8:2}:${String:10:2}:${String:12:2}"; EndDateTime=$(date -d "$StartDateTime 1 days" "+%Y-%m-%d %H:%M:%S"); echo "$String $StartDateTime $EndDateTime"` results in `20160204160118 2016-02-04 16:01:18 2016-02-05 16:01:18` -- you will have to show *exactly* how you get that result. – glenn jackman Feb 05 '16 at 01:18
  • Yes, when we execute this suit of command on terminal it get accurate resulat. but i am using theses command in a shell script wich read StartDateTime from a file and make EndDateTime calculation. My problem is that this script return almost of times an accurate result but sometimes it return a wrong result as in first post the script is continually running to get any uploaded file in a given directory those file contains StartDateTime – hash Feb 05 '16 at 09:40

0 Answers0