主题:[讨论]请教mltx老师,还是关于fortran提取系统时间的问题
chary85
[专家分:0] 发布于 2012-07-19 10:07:00
请问我如何提取系统时间的前两个小时的时间呢,同时在时间节点自动实现日期的转换?比如当前系统时间是2012年1月1号01时05分,那么我如何得到前两个小时的时间呢?即2011年12月31号23时05分。
因为在程序中我要提取系统前两小时的时间数据进行连续多年的循环处理,所以还请老师指教,谢谢~
回复列表 (共12个回复)
沙发
臭石头雪球 [专家分:23030] 发布于 2012-07-19 13:59:00
提取时间以后,减去两个小时就行了。
板凳
chary85 [专家分:0] 发布于 2012-07-19 22:53:00
我用的是call date_and_time(date=t,time=t),系统提取的时间是字符型,比如t=223831.531,这样的字符是不是不能加减2呢?我试过time-2和t-2都报错了。
3 楼
cgl_lgs [专家分:21040] 发布于 2012-07-20 07:18:00
date_and_time返回的是数组吧。
另mltx很久不来了。
4 楼
chary85 [专家分:0] 发布于 2012-07-20 08:23:00
按我以上的写法,返回的是一个日期字符串d,和一个时间字符串t。那您有什么好的方法么?谢谢~
5 楼
臭石头雪球 [专家分:23030] 发布于 2012-07-20 08:54:00
是的,你应该仔细看 date_and_time 的说明,参数应该是数组才对。
6 楼
chary85 [专家分:0] 发布于 2012-07-20 09:54:00
[quote]是的,你应该仔细看 date_and_time 的说明,参数应该是数组才对。[/quote]
date_and_time 的最后一个参数values才是一个八位数的数值,这个函数也可以指定不定参数的用法的,比如只提取时间和时区就可以这样date_and_time(TIME=t, ZONE=z)
http://gcc.gnu.org/onlinedocs/gfortran/DATE_005fAND_005fTIME.html
7 楼
臭石头雪球 [专家分:23030] 发布于 2012-07-20 12:04:00
你为啥非要用前两个参数呢?那俩就是字符型的。
而最后一个整型的输出,你却丢弃了。
这样不就可以了。
Program M
INTEGER DATE_TIME (8)
CALL DATE_AND_TIME ( values = DATE_TIME)
write(*,*) date_time
end
date_time 这个数组就分别是年月日时分秒
8 楼
chary85 [专家分:0] 发布于 2012-07-20 21:18:00
[quote]你为啥非要用前两个参数呢?那俩就是字符型的。
而最后一个整型的输出,你却丢弃了。
这样不就可以了。
Program M
INTEGER DATE_TIME (8)
CALL DATE_AND_TIME ( values = DATE_TIME)
write(*,*) date_time
end
date_time 这个数组就分别是年月日时分秒[/quote]
这个是没问题,可是我想要的就是字符型的时间,要肿么样实现我的目的呢?取得系统时间的前两个小时,并且在时间节点上年月日也一并转换呢?
9 楼
臭石头雪球 [专家分:23030] 发布于 2012-07-20 21:52:00
字符型的时间,你想减去两个小时。这比较难的。
你先在整型的时间上面减去,然后再自己转成字符型的。
我觉得你有必要补充一下基础知识。
10 楼
davyzhou9999 [专家分:30] 发布于 2012-07-21 17:02:00
用一个子程序判断就行了:
subroutine getdatetime(dates,times,date_time)
implicit none
integer(2) :: yy,mm,dd,hh,min,ss !6位整数
integer :: stephh=-2 !小时增量
integer,dimension(8) :: dtvalue
character(20) :: dates,times,date_time
character(8) :: yyc,mmc,ddc,hhc,minc,ssc
call date_and_time(date=dates,time=times,values=dtvalue)
write(date_time,*) dates(1:4),"-",dates(5:6),"-",dates(7:8),"_",times(1:2),":",times(3:4),":",times(5:6)
! read(dates(1:4),*) yy !这部分内容与下面6句是一样的效果,如果不想用values=dtvalue数组就用这6句
! read(dates(5:6),*) mm
! read(dates(7:8),*) dd
! read(times(1:2),*) hh
! read(times(3:4),*) min
! read(times(5:6),*) ss
yy=dtvalue(1)
mm=dtvalue(2)
dd=dtvalue(3)
hh=dtvalue(5)
min=dtvalue(6)
ss=dtvalue(7)
write(*,*) "修改前的系统时间为:",date_time
hh=hh+stephh !这里的stephh就是要变化的小时数
if(hh<0) then
hh=hh+24
dd=dd-1
end if
if(hh>=24) then
hh=hh-24
dd=dd+1
end if
if(dd<=0) then
if(mm==1.or.5.or.7.or.10.or.12) then !当mm为大月(不含3,8),则mm-1为小月
dd=dd+30
mm=mm-1
else if(mm==3) then
dd=dd+28 !此处取28天,没有考虑润年的情况
mm=mm-1
else
dd=dd+31 !当mm为小月(8月例外),则mm-1为大月
mm=mm-1
end if
else
if(mm==2) then
if(dd>28) then
dd=dd-28 !此处取28天,没有考虑29天的情况
mm=mm+1
end if
else if(mm==1.or.3.or.5.or.7.or.8.or.10.or.12) then !当mm为大月(不含1),则当月为31天
if(dd>31) then
dd=dd-31
mm=mm+1
end if
else
if(dd>30) then
dd=dd-30 !当mm为小月,则当月为30天
mm=mm+1
end if
end if
end if
if(mm<=0) then
mm=mm+12
yy=yy-1
end if
if(mm>12) then
mm=mm-12
yy=yy+1
end if
write(yyc,*) yy
write(mmc,*) mm
write(ddc,*) dd
write(hhc,*) hh
write(minc,*) min
write(ssc,*) ss
write(date_time,*) yyc(4:7),"-",mmc(6:7),"-",ddc(6:7),"_",hhc(6:7),":",minc(6:7),":",ssc(6:7)
write(*,*) "修改后的系统时间为:" ,date_time
end subroutine getdatetime
我来回复