0

I'm attempting to get a time stamp with DATE_AND_TIME(), but Fortran returns an excessive amount of spaces, and I can't seem to trim these spaces because functions like TRIM() and ADJUST() work on strings only. Fortran appears to not have a function to convert integers to strings or at least I haven't been able to find one so far. I'd like to use the date-time stamp in a file name, so the included spaces are a problem. Can anyone show me how to remove these additional spaces?

Unfortunately I have to use Fortran, although I believe other versions of Fortran are acceptable.

Code:

program

character(8) :: date
character(10) :: time
character(5) :: zone
integer, dimension(8) :: values

zone = "+01"
call date_and_time(ZONE=zone,VALUES=values)
print *, "values(1)", values(1), "-"

Returns (including text before and after values(1) to show spacing):

values(1)        2017 -

As shown above there seem to be about 6 spaces before the number.

I'm compiling this in gfortran, version is: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)

Alium Britt
  • 1,246
  • 4
  • 13
  • 25
  • Please use tag [tag:fortran] for all Fortran questions. – Vladimir F Героям слава Dec 28 '17 at 15:17
  • If you have a link to a post about trimming spaces on an integer I'd love to see it. – Alium Britt Dec 28 '17 at 15:23
  • Basically, it has nothing to do with date_and_time, any integer would be printed that way with list-directed format (the `*`). You can use Fortran format to change the printing behaviour. For example the `I0` descriptor. – Vladimir F Героям слава Dec 28 '17 at 15:28
  • Are you saying that this is just a product of the PRINT formatting, and those spaces are not actually in the integer? If so, why can I print out other integers that are declared the same way without these additional spaces? – Alium Britt Dec 28 '17 at 15:32
  • Isn't it more direct to do `call date_and_time( date=date, time=time )` and use `date` and `time` for creating a filename ? (e.g., `filename = "myfile_year" // date(1:4) // "_month" // trim(date(5:6)) // "_day" // trim(date(7:8)) // ".dat"` etc) – roygvib Dec 28 '17 at 15:33
  • I didn't realize DATE and TIME were outputs -- but yes, this is what I'm trying to do, but I was testing the variables before I tried to concatenate them. – Alium Britt Dec 28 '17 at 15:38
  • Then, one approach may be first to do some numerical tests using `values` (e.g., `values(1) >= 2000`) and then use substrings of `date` and `time` (both of which are usual characters) for making filenames...? (If the desired filename format is given in the question, I guess we can make it somehow.) – roygvib Dec 28 '17 at 15:43
  • [Sorry, though I added `trim()` to `date(5:6)` and `date(7:8)` in the above comment, it may be not necessary... (they seem to be zero-padded automatically).] – roygvib Dec 28 '17 at 15:49
  • Yes, character type variables seem to not be padded by default. – Alium Britt Dec 28 '17 at 15:53
  • Ok, so the VALUES are intended for numeric purposes then, and the DATE and TIME are for string purposes? – Alium Britt Dec 28 '17 at 15:54
  • Looking at some manual of date_and_time() on the net, I guess so. (FYI, I've never used date_and_time() up to now. Today I used it for the first time :) – roygvib Dec 28 '17 at 15:55
  • Ok, thanks so much for your help! – Alium Britt Dec 28 '17 at 15:56
  • Yes, that is exactly what I was saying, the spaces were only artefacts of the `print` statement. – Vladimir F Героям слава Dec 28 '17 at 16:47

0 Answers0