gtool5 Fortran 90/95 ライブラリ 1.0.0-rc5
English
Loading...
Searching...
No Matches
dccaldateparsestr.f90
Go to the documentation of this file.
1! -*- mode: f90; coding: utf-8 -*-
2!-----------------------------------------------------------------------
3! Copyright (c) 2000-2026 Gtool Development Group. All rights reserved.
4!-----------------------------------------------------------------------
5!>
6!> @author Yasuhiro MORIKAWA
7!> @copyright Copyright (C) GFD Dennou Club, 2009-2026. All rights reserved. <br/>
8!> License is BSD-2-Clause. see [COPYRIGHT](@ref COPYRIGHT) in detail
9!> @en
10!> @brief Parse strings of date
11!> @details
12!> Procedures described in this file are provided from "dc_calendar" module.
13!> @enden
14!>
15!> @ja
16!> @brief 日時の文字列の解釈
17!> @details
18!> このファイルに記載される手続き群は dc_calendar モジュールから提供されます.
19!> @endja
20!>
21
22!> @en
23!> @brief Parse date string
24!> @details
25!> Parse strings of date (conformed to gtool4 netCDF Convention
26!> "5.5 Expression of date and time") specified as `date_str`,
27!> and return `year` -- `zone`.
28!>
29!> @param[in] date_str Strings that express date and time.
30!> See gtool4 netCDF Convention 5.5 Expression of date and time for details.
31!> @param[out] year Year
32!> @param[out] month Month
33!> @param[out] day Day
34!> @param[out] hour Hour
35!> @param[out] min Minute
36!> @param[out] sec Second
37!> @param[out] zone Time-zone (difference from UTC)
38!> @param[out] err Exception handling flag.
39!> By default, when error occur in this procedure, the program aborts.
40!> If this `err` argument is given, .true. is substituted to `err` and
41!> the program does not abort.
42!> @enden
43!>
44!> @ja
45!> @brief 日時文字列の解釈
46!> @details
47!> `date_str` で与えられる日時形式
48!> (gtool4 netCDF 規約「5.5 日時形式」に準拠) を解釈し,
49!> `year` 〜 `zone` に返します.
50!>
51!> @param[in] date_str 日時情報を表す文字列.
52!> 表示形式については gtool4 netCDF 規約 5.5 日時形式を参照のこと.
53!> @param[out] year 年
54!> @param[out] month 月
55!> @param[out] day 日
56!> @param[out] hour 時
57!> @param[out] min 分
58!> @param[out] sec 秒
59!> @param[out] zone UTC からの時差
60!> @param[out] err 例外処理用フラグ.
61!> デフォルトでは, この手続き内でエラーが生じた場合,
62!> プログラムは強制終了します.
63!> 引数 `err` が与えられる場合, プログラムは強制終了せず,
64!> 代わりに `err` に .true. が代入されます.
65!> @endja
66subroutine dccaldateparsestr1( date_str, &
67 & year, month, day, hour, min, sec, zone, &
68 & err )
69
70 use dc_regex, only: match
71 use dc_message, only: messagenotify
72 use dc_string, only: lchar, stoi, stod
75 use dc_types, only: string, dp, token
76 implicit none
77 character(*), intent(in):: date_str
78 integer, intent(out):: year
79 integer, intent(out):: month
80 integer, intent(out):: day
81 integer, intent(out):: hour
82 integer, intent(out):: min
83 real(DP), intent(out):: sec
84 character(*), intent(out):: zone
85 logical, intent(out), optional:: err
86
87 ! 作業変数
88 ! Work variables
89 !
90 integer:: start, length
91 character(STRING):: str1, str2
92 character(TOKEN):: zone_pm, zone_hrs, zone_min
93 integer:: stat
94 character(STRING):: cause_c
95 character(*), parameter:: subname = 'DCCalDateParseStr1'
96continue
97 call beginsub( subname )
98 stat = dc_noerr
99 cause_c = ''
100
101 ! 与えられた文字列が日時表現として有効かどうかをチェック
102 ! Check validation of strings as an expression of date
103 !
104 call match( '[-]*#d+-#d+-#d+[#w#s]+#d+:#d+:#d+', date_str, & ! (in)
105 & start, length ) ! (out)
106
107 if ( length > 0 ) then
108 str1 = date_str(start:)
109 else
110 stat = dc_ebaddate
111 call messagenotify('W', subname, &
112 & 'date_str=<%c> is invalid expression as date.', &
113 & c1 = trim(date_str) )
114 goto 999
115 end if
116
117 ! 年の解釈
118 ! Parse year
119 !
120 call match( '^[-]*#d+-', str1, & ! (in)
121 & start, length ) ! (out)
122 str2 = str1(start:start+length-2)
123 str1 = str1(start+length:)
124 year = stoi(str2)
125
126 ! 月の解釈
127 ! Parse month
128 !
129 call match( '^#d+-', str1, & ! (in)
130 & start, length ) ! (out)
131 str2 = str1(start:start+length-2)
132 str1 = str1(start+length:)
133 month = stoi(str2)
134
135 ! 日の解釈
136 ! Parse day
137 !
138 call match( '^#d+[#w#s]', str1, & ! (in)
139 & start, length ) ! (out)
140 str2 = str1(start:start+length-2)
141 str1 = str1(start+length:)
142 day = stoi(str2)
143
144 ! 時の解釈
145 ! Parse hour
146 !
147 call match( '#d+:', str1, & ! (in)
148 & start, length ) ! (out)
149 str2 = str1(start:start+length-2)
150 str1 = str1(start+length:)
151 hour = stoi(str2)
152
153 ! 分の解釈
154 ! Parse minute
155 !
156 call match( '#d+:', str1, & ! (in)
157 & start, length ) ! (out)
158 str2 = str1(start:start+length-2)
159 str1 = str1(start+length:)
160 min = stoi(str2)
161
162 ! 秒の解釈
163 ! Parse min
164 !
165 call match( '#d+', str1, & ! (in)
166 & start, length ) ! (out)
167 str2 = str1(start:start+length-1)
168 str1 = str1(start+length:)
169
170 call match( '^#.#d+', str1, & ! (in)
171 & start, length ) ! (out)
172
173 if ( length > 0 ) then
174 str2 = trim(str2) // str1(start:start+length-1)
175 str1 = str1(start+length:)
176 end if
177 sec = stod(str2)
178
179 ! UTC からの時差の解釈
180 ! Parse time-zone difference
181 !
182 call match( '[#+-]#d+:#d+', str1, & ! (in)
183 & start, length ) ! (out)
184 if ( length > 0 ) then
185 zone_pm = str1(start:start)
186 str1 = str1(start+1:start+length-1)
187
188 call match( '^#d+:', str1, & ! (in)
189 & start, length ) ! (out)
190 zone_hrs = str1(start:start+length-2)
191 zone_min = str1(start+length:)
192 zone = trim(zone_pm) // trim(zone_hrs) // ':' // trim(zone_min)
193 else
194 zone = ''
195 end if
196
197 call dbgmessage('year=<%d> month=<%d> day=<%d> hour=<%d> min=<%d> sec=<%f>' // &
198 & ' zone=<%c>', &
199 & i = (/year, month, day, hour, min/), d = (/sec/), &
200 & c1 = trim(zone) )
201
202 ! 終了処理, 例外処理
203 ! Termination and Exception handling
204 !
205999 continue
206 call storeerror( stat, subname, err, cause_c )
207 call endsub( subname )
208end subroutine dccaldateparsestr1
subroutine dccaldateparsestr1(date_str, year, month, day, hour, min, sec, zone, err)
日時の文字列の解釈
エラー処理用モジュール
Definition dc_error.f90:454
subroutine, public storeerror(number, where, err, cause_c, cause_i)
Definition dc_error.f90:891
integer, parameter, public dc_noerr
エラー等を保持
Definition dc_error.f90:468
integer, parameter, public dc_ebaddate
Definition dc_error.f90:552
メッセージの出力
シンプルな正規表現関数 'match' を提供します.
Definition dc_regex.f90:62
subroutine, public match(pattern, text, start, length)
Definition dc_regex.f90:469
文字型変数の操作
Definition dc_string.f90:83
デバッグ時の追跡用モジュール
Definition dc_trace.f90:150
subroutine, public dbgmessage(fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:661
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
Definition dc_trace.f90:457
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:580
種別型パラメタを提供します。
Definition dc_types.f90:55
integer, parameter, public token
単語やキーワードを保持する文字型変数の種別型パラメタ
Definition dc_types.f90:128
integer, parameter, public string
文字列を保持する 文字型変数の種別型パラメタ
Definition dc_types.f90:137
integer, parameter, public dp
倍精度実数型変数
Definition dc_types.f90:92