北京时间减8小时就是世界时。 8小时=8*60*60=28800秒。 把下面的代码复制成get_new_time.f
然后编译:pgf90 -o get_new_time get_new_time.f
比如运行: get_new_time 2008071620 -28800 会输出2008071612_00:00
get_new_time.f:
program new_time
!yuanbing, 2006
character*16 ndate, odate, idt
character*19 odate1
character*256 cm,cn
real m,n
integer idts,method
odate=' '
numarg = iargc()
if(numarg<2) call help
if(numarg==2) method = 1
if(numarg==3) then
method = 2
endif
if(numarg==4) then
call getarg(4,cn)
read(cn,*) n
if(n>0) method = 3
if(n==0 ) call help
if(n<0 ) method = 4
endif
call getarg(1,odate1)
if(method == 3 .or. method == 4) then
odate(1:4)=odate1(1:4)
odate(5:6)=odate1(6:7)
odate(7:8)=odate1(9:10)
if(len(trim(odate1))>=13) odate(9:10)=odate1(12:13)
if(len(trim(odate1))>=16) then
odate(11:11)='_'
odate(12:13)=odate1(15:16)
endif
if(len(trim(odate1))>=19) then
odate(14:14)=':'
odate(15:16)=odate1(18:19)
endif
else
odate=odate1(1:16)
endif
call getarg(2,idt)
read(idt,*) idts
! print*,odate,method
call geth_newdate (ndate, odate, idts)
if(method == 1 .or. method == 4) then
write(*,"(a16)") ndate
else
write(*,"(a19)") ndate(1:4)//'-'//ndate(5:6)//'-'//ndate(7:8)//
- '_'//ndate(9:10)//':'//ndate(12:13)//':'//ndate(15:16)
endif
end
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine geth_newdate (ndate, odate, idts)
implicit none
!**********************************************************************
!
! purpose - from old date ('YYYYMMDDHH_MM:SS') and time in
! seconds, compute the new date.
!
! on entry - odate - the old hdate.
! idts - the change in time in seconds.
!
! on exit - ndate - the new hdate.
! idts - the change in time in seconds.
!
!**********************************************************************
integer idts
character*(*) ndate, odate
integer nlen, olen
!
! Local Variables
!
! yrold - indicates the year associated with "odate"
! moold - indicates the month associated with "odate"
! dyold - indicates the day associated with "odate"
! hrold - indicates the hour associated with "odate"
! miold - indicates the minute associated with "odate"
! scold - indicates the second associated with "odate"
!
! yrnew - indicates the year associated with "ndate"
! monew - indicates the month associated with "ndate"
! dynew - indicates the day associated with "ndate"
! hrnew - indicates the hour associated with "ndate"
! minew - indicates the minute associated with "ndate"
! scnew - indicates the second associated with "ndate"
!
! mday - a list assigning the number of days in each month
! dth - the number of hours represented by "idts"
! i - loop counter
! nday - the integer number of days represented by "idts"
! nhour - the integer number of hours in "idts" after taking out
! all the whole days
! nmin - the integer number of minutes in "idts" after taking out
! all the whole days and whole hours.
! nsec - the integer number of minutes in "idts" after taking out
! all the whole days, whole hours, and whole minutes.
!
integer yrnew, monew, dynew, hrnew, minew, scnew
integer yrold, moold, dyold, hrold, miold, scold
integer mday(12), nday, nhour, nmin, nsec, i
real dth
logical opass
!************************* Subroutine Begin *************************
!
! Assign the number of days in a months
!
mday( 1) = 31
mday( 2) = 28
mday( 3) = 31
mday( 4) = 30
mday( 5) = 31
mday( 6) = 30
mday( 7) = 31
mday( 8) = 31
mday( 9) = 30
mday(10) = 31
mday(11) = 30
mday(12) = 31
!
! Break down old hdate into parts
!
hrold = 0
miold = 0
scold = 0
olen = len(odate)
read(odate(1:4), '(I4)') yrold
read(odate(5:6), '(I2)') moold
read(odate(7:8), '(I2)') dyold
if (olen.ge.10) then
read(odate(9:10),'(I2)') hrold
if (olen.ge.13) then
read(odate(12:13),'(I2)') miold
if (olen.ge.16) then
read(odate(15:16),'(I2)') scold
endif
endif
endif
!
! Set the number of days in February for that year.
!
mday(2) = 28
if (mod(yrold,4).eq.0) then
mday(2) = 29
if (mod(yrold,100).eq.0) then
mday(2) = 28
if (mod(yrold,400).eq.0) then
mday(2) = 29
endif
endif
endif
!
! Check that ODATE makes sense.
!
opass = .TRUE.
! Check that the month of ODATE makes sense.
if ((moold.gt.12).or.(moold.lt.1)) then
print*, 'GETH_NEWDATE: Month of ODATE = ', moold
opass = .FALSE.
endif
! Check that the day of ODATE makes sense.
if ((dyold.gt.mday(moold)).or.(dyold.lt.1)) then
print*, 'GET_NEWDATE: Day of ODATE = ', dyold
opass = .FALSE.
endif
! Check that the hour of ODATE makes sense.
if ((hrold.gt.23).or.(hrold.lt.0)) then
print*, 'GET_NEWDATE: Hour of ODATE = ', hrold
opass = .FALSE.
endif
! Check that the minute of ODATE makes sense.
if ((miold.gt.59).or.(miold.lt.0)) then
print*, 'GET_NEWDATE: Minute of ODATE = ', miold
opass = .FALSE.
endif
! Check that the second of ODATE makes sense.
if ((scold.gt.59).or.(scold.lt.0)) then
print*, 'GET_NEWDATE: Second of ODATE = ', scold
opass = .FALSE.
endif
if (.not.opass) then
print*, 'Crazy ODATE: ', odate(1:olen), olen
STOP 'Error_odate'
! stop
endif
!
! Date Checks are completed. Continue.
!
!
! Compute the number of days, hours, minutes, and seconds in idts
!
if(idts>=0) then
nday = idts/86400 ! Integer number of days in delta-time
nhour = mod(idts,86400)/3600
nmin = mod(idts,3600)/60
nsec = mod(idts,60)
scnew = scold + nsec
if (scnew .ge. 60) then
scnew = scnew - 60
nmin = nmin + 1
end if
minew = miold + nmin
if (minew .ge. 60) then
minew = minew - 60
nhour = nhour + 1
end if
hrnew = hrold + nhour
if (hrnew .ge. 24) then
hrnew = hrnew - 24
nday = nday + 1
end if
dynew = dyold
monew = moold
yrnew = yrold
do i = 1, nday
dynew = dynew + 1
if (dynew.gt.mday(monew)) then
dynew = dynew - mday(monew)
monew = monew + 1
if (monew .gt. 12) then
monew = 1
yrnew = yrnew + 1
mday(2) = 28
if (mod(yrnew,4).eq.0) then
mday(2) = 29
if (mod(yrnew,100).eq.0) then
mday(2) = 28
if (mod(yrnew,400).eq.0) then
mday(2) = 29
endif
endif
endif
end if
endif
enddo
else
nday = -idts/86400
nhour = -mod(idts,86400)/3600
nmin = -mod(idts,3600)/60
nsec = -mod(idts,60)
scnew = scold - nsec
if (scnew .lt. 0) then
scnew = 60 + scnew
nmin = nmin + 1
end if
minew = miold - nmin
if (minew .lt. 0) then
minew = 60 + minew
nhour = nhour + 1
endif
hrnew = hrold - nhour
if (hrnew .lt. 0) then
hrnew = 24 + hrnew
nday = nday + 1
end if
dynew = dyold
monew = moold
yrnew = yrold
do i = 1, nday
dynew = dynew - 1
if (dynew.lt.1) then
monew = monew - 1
dynew = mday(monew) - abs(dynew)
if (monew .lt. 1) then
monew = 12
dynew = mday(monew) - abs(dynew)
yrnew = yrnew - 1
mday(2) = 28
if (mod(yrnew,4).eq.0) then
mday(2) = 29
if (mod(yrnew,100).eq.0) then
mday(2) = 28
if (mod(yrnew,400).eq.0) then
mday(2) = 29
endif
endif
endif
endif
endif
enddo
endif
!
! Now construct the new mdate
!
nlen = len(ndate)
if (nlen.ge.16) then
write(ndate,19) yrnew, monew, dynew, hrnew, minew, scnew
19 format(I4,I2.2,I2.2,I2.2,'_',I2.2,':',I2.2)
else if (nlen.eq.13) then
write(ndate,16) yrnew, monew, dynew, hrnew, minew
16 format(I4,I2.2,I2.2,I2.2,'_',I2.2)
else if (nlen.eq.10) then
write(ndate,13) yrnew, monew, dynew, hrnew
13 format(I4,I2.2,I2.2,I2.2)
else if (nlen.eq.8) then
write(ndate,10) yrnew, monew, dynew
10 format(I4,I2.2,I2.2)
endif
!************************** Subroutine End **************************
end
subroutine help
print*
print*,'usage: '
print*,' get_new_time.exe date_time idts [m] [n]'
print*
print*,'if m,n not given, date_time in YYYYMMDDHH_MM:SS'
print*,' ouput in YYYYMMDDHH_MM:SS'
print*
print*,'if m given,n not, date_time in YYYYMMDDHH_MM:SS'
print*,' ouput in YYYY-MM-DD_HH:MM:SS'
print*
print*,'if m,n given,n>0, date_time in YYYY-MM-DD_HH:MM:SS'
print*,' ouput in YYYY-MM-DD_HH:MM:SS'
print*
print*,'if m,n given,n<0, date_time in YYYY-MM-DD_HH:MM:SS'
print*,' ouput in YYYYMMDDHH_MM:SS'
print*
print*,'Here, idts means the time inteval in seconds. '
print*,' idts may be postive or negtive'
stop ' '
end