Fortran中的日期时间差 [英] Date Time Difference in Fortran

查看:526
本文介绍了Fortran中的日期时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使fortran返回两次作为字符串传入的时间差,这与VBA的TimeDiff非常相似。我已经在fortran中处理日期和时间已有相当长的时间了,但是在这种情况下找不到我需要的东西。
fortran的第一个问题是如何将字符串转换为另一个可计算的时间变量。例如,time_and_dates ctime将时间转换为字符串,但在我的情况下所需的恰好相反。

My goal is to make fortran return the difference between two times that were passed in as character strings, very simular to VBA's TimeDiff. I've been going through the handling of date and time in fortran for quite a while now, but couldn't find what I need in this perticular case. The first problem in fortran is how to turn a string into a further computable time variable. time_and_dates ctime for example converts the time into a character string, but whats needed in my case is the exact opposite.

由于fortran可以很容易地计算出诸如点手表之类的内容,以完成程序所需的时间,甚至可以从系统本身上花费时间,因此它显然能够计算出这些时间如果到目前为止我的想法是正确的,那是一种东西。

Since fortran easily calculates things like a spot watch for the time needed for a programm to finish and even dislays the time from the system itself, it obviously is able to calculate these kind of stuff if my thoughts are right so far.

但是如何将我的字符串 YYYYMMDD HHMMSS传递给时间格式,如何计算它以及如何用短语表达输出呢?

But how to pass my String "YYYYMMDD HHMMSS" into the time format, how to calculate with it and how to phrase the output?

下面是一个示例:

date1 = as.date("20130512 091519") !Stringinput
date2 = as.date("20131116 120418")


result = time.difference(date1,date2,outputunit="seconds")

print*, 'time diff in sec: ', result !returns something like 4646345 as Integer

由于fortrans能够在其他情况下(秒表等)进行此类计算,因此我非常感谢一个不涉及任何外部扩展或冒险性手工编码的解决方案(365 。 .leap year ... / 400 ..依此类推)。

Due to fortrans ability to do such calculations in other context (stop watch etc) I would really appreciate a solution that does not involve any extern extensions or adventurous manual coding (365 ...leap year... /400..and so on).

有没有办法使用Fortran和系统的内部实现方式(win64 )出于我的目的?

Is there a way to use the intern possibilies of fortran and the system (win64) for my purpose?

如果没有,以哪种方式将系统的时间信息传递到fortran中,并且可以模仿(因此,源不是 system

If not, on which way passes the system its time information into fortran and can this possibly be imitated (so the source isn't "system" but "character string" instead)?

编辑:到目前为止,感谢您的输入,但是如上所述,我ld最好将fortrans实习生功能(如在秒表中使用的)用于工作,而不是扩展或手动计算。我发现很难相信日期和时间只能转换为字符,而不能转换为字符...但是无论如何还是要谢谢您。

Thanks for the input so far, but as mentioned above I would preferably use fortrans intern abilities (as it happens in stop watch) for the job instead of extensions or manual calculations. I find it hard to belief that date&time can only be converted to character but not the other way round... but thank you anyway.

推荐答案

我经常在Fortran中处理日期和时间,所以最终为它编写了一个库:

I often work with dates and times in Fortran, so I ended up writing a library for it:

http://github.com/milancurcic/datetime-fortran

它支持基本 datetime timedelta 对象的数学算术和比较运算符(在Python的datetime之后松散地建模),并且还提供C的接口 strftime strptime 等。

It supports basic arithmetic and comparison operators for datetime and timedelta objects (loosely modeled after Python's datetime), and also provides interfaces to C strftime and strptime, and more.

从0.3版开始.0, strftime strptime 接口被定义为直接在 datetime 实例,因此无需摆弄tm_structs。

Since version 0.3.0, strftime and strptime interfaces are defined to operate directly on datetime instances, so there is no need to fiddle with tm_structs.

这里有一个简单的程序可以处理您的情况:

Here is a simple program that will handle your case:

USE datetime_module

TYPE(datetime)  :: date1,date2
TYPE(timedelta) :: timediff

CHARACTER(LEN=15) :: str1 = "20130512 091519"
CHARACTER(LEN=15) :: str2 = "20131116 120418"

date1 = strptime(str1,"%Y%m%d %H%M%S")
date2 = strptime(str2,"%Y%m%d %H%M%S")

timediff = date2-date1

WRITE(*,*)timediff
WRITE(*,*)timediff % total_seconds()

END

输出:

     188           2          48          59           0
  16253339.000000000 

在我的计算机上。

这篇关于Fortran中的日期时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆