Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to drop the OPTIONAL arguments in "date_and_time"?

7 views
Skip to first unread message

qqu...@hotmail.com

unread,
Jan 3, 2009, 9:30:46 PM1/3/09
to
Dear Everyone:

I have a book "Programmer's Guide to Fortran 90" (3rd ed.) by
Brainerd, Goldberg and Adams. On page 388, it gives information about
the subroutine "date_and_time" in the following fashion:

date_and_time(date, time, zone, values)
Optional date, time, zone, values

I can get everything correctly when I put all the arguments---"date",
"time", "zone" and "values"---there. However, all I actually need is
"values". But if I include ONLY this argument as follows

call date_and_time(values)

it won't work.

Since it says that all the arguments are OPTIONAL, how can I correctly
drop the other arguments in the "call" statement?

Thank you for reading and replying!

--Roland

George

unread,
Jan 3, 2009, 9:57:52 PM1/3/09
to

Roland,

I've seen a lot of errors with this routine lately, most of them mine.


implicit none

real :: u, sum1
integer :: i, j

sum1 = 0.0
do i = 1, 10
call init_seed()
call random_number(u)
print *, " u is ", u
sum1 = sum1 + u

! I need a pause here of 30 milliseconds
do j = 1, 1000000
call random_number(u)
end do

end do

print *, "sum over ", (i - 1), " trials is ", sum1

contains
subroutine init_seed()
integer :: n, ival(8), v(3)
integer, allocatable :: seed(:)

call date_and_time(values=ival)

v(1) = ival(8) + 2048*ival(7)
v(2) = ival(6) + 64*ival(5) ! value(4) isn't really 'random'
v(3) = ival(3) + 32*ival(2) + 32*8*ival(1)

call random_seed(size=n)

allocate(seed(n))

call random_seed() ! Give the seed an implementation-dependent kick

call random_seed(get=seed)

do i=1, n
seed(i) = seed(i) + v(mod(i-1, 3) + 1)
enddo

print *, "seed is ", seed

call random_seed(put=seed)

deallocate(seed)

end subroutine

endprogram


! g95 -Wall duke5.f03 -o y.exe

I think this shows date_and_time used portably to seed fortran's
pseudorandom intrinsics.

Merry tenth day of Christams,
--
George

America must not ignore the threat gathering against us. Facing clear
evidence of peril, we cannot wait for the final proof, the smoking gun that
could come in the form of a mushroom cloud.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/

qqu...@hotmail.com

unread,
Jan 3, 2009, 11:14:16 PM1/3/09
to
George: Thank you for your sample code. I realized that I should treat
DATE, TIME, ZONE and VALUES as intrinsic keywords, and thus I would
write

character(len=8):: D0
character(len=10):: T0
call date_and_time(DATE=D0)
call date_and_time(TIME=T0)

--Roland

Tim Prince

unread,
Jan 4, 2009, 12:48:28 AM1/4/09
to
qqu...@hotmail.com wrote:

> I have a book "Programmer's Guide to Fortran 90" (3rd ed.) by
> Brainerd, Goldberg and Adams. On page 388, it gives information about
> the subroutine "date_and_time" in the following fashion:
>
> date_and_time(date, time, zone, values)
> Optional date, time, zone, values
>

Here at home, I have only the current edition of Adams, Brainerd et al.
where I don't see any reference like this (at least, not in the index).
Anyway, the usage of named keyword arguments is like

integer myvalues(8)
call date_and_time(values=myvalues)

Adams, Brainerd should have a section on keyword arguments, as they do in
the editions I own. This applies to intrinsic functions as well as to
user written functions. What did you expect the compiler to do with the
values results, if you didn't declare and specify an array to receive them?

A few f77 compilers implemented date_and_time without keyword arguments,
as you may have noted in today's other thread on date_and_time.

Richard Maine

unread,
Jan 4, 2009, 2:35:57 AM1/4/09
to
<qqu...@hotmail.com> wrote:

> call date_and_time(values)
>
> it won't work.
>
> Since it says that all the arguments are OPTIONAL, how can I correctly
> drop the other arguments in the "call" statement?

I see that you found the specific answer for this one in other replies.
But you should be aware that this is how optional arguments work in
general. It isn't anything particular about date_and_time. You can't
just randomly omit optional arguments and expect the compiler to
magically know which arguments go with the ones you did supply. (And it
isn't going to key off of your variable names, which don't have to have
any relation to th eargument names, either). You can freely omit
optional arguments from the *END* of the argument list. But if you omit
them other than at the end, you need to use the keyword form to tell the
compiler which arguments you are specifying.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain

George

unread,
Jan 4, 2009, 8:26:42 PM1/4/09
to
On Sat, 3 Jan 2009 20:14:16 -0800 (PST), qqu...@hotmail.com wrote:

> George: Thank you for your sample code. I realized that I should treat
> DATE, TIME, ZONE and VALUES as intrinsic keywords, and thus I would
> write
>
> character(len=8):: D0
> character(len=10):: T0
> call date_and_time(DATE=D0)
> call date_and_time(TIME=T0)
>
> --Roland

You're welcome. I should credit Andy Vaught for the part that seeds the
pseudorandom, although that doesn't seem to be the part you want.

This might show you other ways to call:

contains
SUBROUTINE init_g95_seed()
INTEGER :: i, n
INTEGER, DIMENSION(:), ALLOCATABLE :: seed

INTEGER date_time(8)
CHARACTER(LEN=10) big_ben(3)

CALL DATE_AND_TIME(big_ben(1), big_ben(2), big_ben(3), date_time)
PRINT *,'date_time array values:'
PRINT *,'year=',date_time(1)
PRINT *,'month_of_year=',date_time(2)
PRINT *,'day_of_month=',date_time(3)
PRINT *,'time difference in minutes=',date_time(4)
PRINT *,'hour of day=',date_time(5)
PRINT *,'minutes of hour=',date_time(6)
PRINT *,'seconds of minute=',date_time(7)
PRINT *,'milliseconds of second=',date_time(8)
PRINT *, 'DATE=',big_ben(1)
PRINT *, 'TIME=',big_ben(2)
PRINT *, 'ZONE=',big_ben(3)

CALL RANDOM_SEED(size = n)
print *, "n=", n


ALLOCATE(seed(n))

do i = 1, n
seed(i) = date_time(9-i)
end do

print *, seed


! seed = date_time(8) + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)
print *, "seed= ", seed

DEALLOCATE(seed)
END SUBROUTINE

The above is not a robust means to seed a PRNG.
--
George

Some have argued that confronting the threat from Iraq could detract from
the war against terror. To the contrary, confronting the threat posed by
Iraq is crucial to winning the war on terror.

George

unread,
Jan 4, 2009, 8:42:56 PM1/4/09
to
On Sat, 03 Jan 2009 21:48:28 -0800, Tim Prince wrote:

> qqu...@hotmail.com wrote:
>
>> I have a book "Programmer's Guide to Fortran 90" (3rd ed.) by
>> Brainerd, Goldberg and Adams. On page 388, it gives information about
>> the subroutine "date_and_time" in the following fashion:
>>
>> date_and_time(date, time, zone, values)
>> Optional date, time, zone, values
>>
>
> Here at home, I have only the current edition of Adams, Brainerd et al.
> where I don't see any reference like this (at least, not in the index).
> Anyway, the usage of named keyword arguments is like
>
> integer myvalues(8)
> call date_and_time(values=myvalues)
>
> Adams, Brainerd should have a section on keyword arguments, as they do in
> the editions I own. This applies to intrinsic functions as well as to
> user written functions. What did you expect the compiler to do with the
> values results, if you didn't declare and specify an array to receive them?

Tim,

I couldn't figure out what you meant by this until I looked this up in the
standard.
13.7.28 DATE AND TIME ([DATE, TIME, ZONE, VALUES])

Of course the brackets indicate optional arguments. I didn't reliaze that
you could call them *without* keywords:

10 INTEGER DATE_TIME (8)
11 CHARACTER (LEN = 10) BIG_BEN (3)
12 CALL DATE_AND_TIME (BIG_BEN (1), BIG_BEN (2), &
13 BIG_BEN (3), DATE_TIME)

> Adams, Brainerd should have a section on keyword arguments, as they do in
> the editions I own.

Look at the index of examples instead, which cites pg 475.
--
George

Use power to help people. For we are given power not to advance our own
purposes nor to make a great show in the world, nor a name. There is but
one just use of power and it is to serve people.

0 new messages