Loading...
Searching...
No Matches
dccaldateparsestr.f90
Go to the documentation of this file.
1!= 日時の文字列の解釈
2!= Parse strings of date
3!
4! Authors:: Yasuhiro MORIKAWA
5! Version:: $Id: dccaldateparsestr.f90,v 1.3 2010-09-24 07:07:31 morikawa Exp $
6! Tag Name:: $Name: $
7! Copyright:: Copyright (C) GFD Dennou Club, 2009-. All rights reserved.
8! License:: See COPYRIGHT[link:../../COPYRIGHT]
9!
10! このファイルに記載される手続き群は dc_calendar モジュールから提供されます.
11!
12! Procedures described in this file are provided from "dc_calendar" module.
13!
14subroutine dccaldateparsestr1( date_str, &
15 & year, month, day, hour, min, sec, zone, &
16 & err )
17 ! *date_str* で与えられる日時形式
18 ! (gtool4 netCDF 規約「5.5 日時形式」に準拠) を解釈し,
19 ! *year* 〜 *zone* に返します.
20 !
21 ! Parse strings of date (conformed to gtool4 netCDF Convention
22 ! "5.5 Expression of date and time") specified as *date_str*,
23 ! and return *year* -- *zone*.
24 !
25
26 use dc_regex, only: match
27 use dc_message, only: messagenotify
28 use dc_string, only: lchar, stoi, stod
29 use dc_trace, only: beginsub, endsub, dbgmessage
31 use dc_types, only: string, dp, token
32 implicit none
33 character(*), intent(in):: date_str
34 ! 日時情報を表す文字列.
35 ! 表示形式については gtool4 netCDF 規約
36 ! 5.5 日時形式を参照のこと.
37 !
38 ! Strings that express date and time.
39 ! See gtool4 netCDF Convention
40 ! 5.5 Expression of date and time for details.
41 integer, intent(out):: year ! 年. Year.
42 integer, intent(out):: month ! 月. Month.
43 integer, intent(out):: day ! 日. Day.
44 integer, intent(out):: hour ! 時. Hour.
45 integer, intent(out):: min ! 分. Minute.
46 real(DP), intent(out):: sec ! 秒. Second.
47 character(*), intent(out):: zone
48 ! UTC からの時差. Time-zone.
49 logical, intent(out), optional:: err
50 ! 例外処理用フラグ.
51 ! デフォルトでは, この手続き内でエラーが
52 ! 生じた場合, プログラムは強制終了します.
53 ! 引数 *err* が与えられる場合,
54 ! プログラムは強制終了せず, 代わりに
55 ! *err* に .true. が代入されます.
56 !
57 ! Exception handling flag.
58 ! By default, when error occur in
59 ! this procedure, the program aborts.
60 ! If this *err* argument is given,
61 ! .true. is substituted to *err* and
62 ! the program does not abort.
63
64
65 ! 作業変数
66 ! Work variables
67 !
68 integer:: start, length
69 character(STRING):: str1, str2
70 character(TOKEN):: zone_pm, zone_hrs, zone_min
71 integer:: stat
72 character(STRING):: cause_c
73 character(*), parameter:: subname = 'DCCalDateParseStr1'
74continue
75 call beginsub( subname )
76 stat = dc_noerr
77 cause_c = ''
78
79 ! 与えられた文字列が日時表現として有効かどうかをチェック
80 ! Check validation of strings as an expression of date
81 !
82 call match( '[-]*#d+-#d+-#d+[#w#s]+#d+:#d+:#d+', date_str, & ! (in)
83 & start, length ) ! (out)
84
85 if ( length > 0 ) then
86 str1 = date_str(start:)
87 else
88 stat = dc_ebaddate
89 call messagenotify('W', subname, &
90 & 'date_str=<%c> is invalid expression as date.', &
91 & c1 = trim(date_str) )
92 goto 999
93 end if
94
95 ! 年の解釈
96 ! Parse year
97 !
98 call match( '^[-]*#d+-', str1, & ! (in)
99 & start, length ) ! (out)
100 str2 = str1(start:start+length-2)
101 str1 = str1(start+length:)
102 year = stoi(str2)
103
104 ! 月の解釈
105 ! Parse month
106 !
107 call match( '^#d+-', str1, & ! (in)
108 & start, length ) ! (out)
109 str2 = str1(start:start+length-2)
110 str1 = str1(start+length:)
111 month = stoi(str2)
112
113 ! 日の解釈
114 ! Parse day
115 !
116 call match( '^#d+[#w#s]', str1, & ! (in)
117 & start, length ) ! (out)
118 str2 = str1(start:start+length-2)
119 str1 = str1(start+length:)
120 day = stoi(str2)
121
122 ! 時の解釈
123 ! Parse hour
124 !
125 call match( '#d+:', str1, & ! (in)
126 & start, length ) ! (out)
127 str2 = str1(start:start+length-2)
128 str1 = str1(start+length:)
129 hour = stoi(str2)
130
131 ! 分の解釈
132 ! Parse minute
133 !
134 call match( '#d+:', str1, & ! (in)
135 & start, length ) ! (out)
136 str2 = str1(start:start+length-2)
137 str1 = str1(start+length:)
138 min = stoi(str2)
139
140 ! 秒の解釈
141 ! Parse min
142 !
143 call match( '#d+', str1, & ! (in)
144 & start, length ) ! (out)
145 str2 = str1(start:start+length-1)
146 str1 = str1(start+length:)
147
148 call match( '^#.#d+', str1, & ! (in)
149 & start, length ) ! (out)
150
151 if ( length > 0 ) then
152 str2 = trim(str2) // str1(start:start+length-1)
153 str1 = str1(start+length:)
154 end if
155 sec = stod(str2)
156
157 ! UTC からの時差の解釈
158 ! Parse time-zone difference
159 !
160 call match( '[#+-]#d+:#d+', str1, & ! (in)
161 & start, length ) ! (out)
162 if ( length > 0 ) then
163 zone_pm = str1(start:start)
164 str1 = str1(start+1:start+length-1)
165
166 call match( '^#d+:', str1, & ! (in)
167 & start, length ) ! (out)
168 zone_hrs = str1(start:start+length-2)
169 zone_min = str1(start+length:)
170 zone = trim(zone_pm) // trim(zone_hrs) // ':' // trim(zone_min)
171 else
172 zone = ''
173 end if
174
175 call dbgmessage('year=<%d> month=<%d> day=<%d> hour=<%d> min=<%d> sec=<%f>' // &
176 & ' zone=<%c>', &
177 & i = (/year, month, day, hour, min/), d = (/sec/), &
178 & c1 = trim(zone) )
179
180 ! 終了処理, 例外処理
181 ! Termination and Exception handling
182 !
183999 continue
184 call storeerror( stat, subname, err, cause_c )
185 call endsub( subname )
186end subroutine dccaldateparsestr1
187
subroutine dccaldateparsestr1(date_str, year, month, day, hour, min, sec, zone, err)
subroutine, public storeerror(number, where, err, cause_c, cause_i)
Definition dc_error.f90:830
integer, parameter, public dc_ealreadyinit
Definition dc_error.f90:558
integer, parameter, public dc_noerr
Definition dc_error.f90:509
integer, parameter, public dc_ebaddate
Definition dc_error.f90:575
Provide simple regular expression subroutine: 'match' .
Definition dc_regex.f90:16
subroutine, public match(pattern, text, start, length)
Definition dc_regex.f90:267
Provides kind type parameter values.
Definition dc_types.f90:49
integer, parameter, public token
Character length for word, token
Definition dc_types.f90:109
integer, parameter, public string
Character length for string
Definition dc_types.f90:118
integer, parameter, public dp
Double Precision Real number
Definition dc_types.f90:83