gtool5 Fortran 90/95 Library 1.0.0-rc5
日本語
Loading...
Searching...
No Matches
dcdatetimetochar.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, 2006-2026. All rights reserved. <br/>
8!> License is BSD-2-Clause. see [COPYRIGHT](@ref COPYRIGHT) in detail
9!> @en
10!> @brief Convert DC_DATETIME, DC_DIFFTIME to character strings
11!> @details
12!> Procedures described in this file are provided from "dc_date" module.
13!> @enden
14!>
15!> @ja
16!> @brief dc_date_types#DC_DATETIME, dc_date_types#DC_DIFFTIME 型変数の文字変換
17!> @details
18!> このファイルで提供される手続き群は dc_date モジュールにて提供されます。
19!> @endja
20!>
21
22!> @en
23!> @brief Convert DC_DATETIME to character string
24!> @details
25!> Convert a dc_date_types#DC_DATETIME variable to a character variable
26!> and return it.
27!> The format is a complete notation according to JIS X 0301 as follows:
28!>
29!> YYYY-MM-DDThh:mm:ss.sTZD
30!>
31!> YYYY is year, MM is month, DD is day, hh is hour, mm is minute,
32!> ss.s is seconds, and TZD is timezone.
33!>
34!> @param[in] time Datetime to convert
35!> @return Character representation of the datetime
36!> @enden
37!>
38!> @ja
39!> @brief DC_DATETIME を文字列に変換
40!> @details
41!> dc_date_types#DC_DATETIME 型変数を文字型変数へ変換して返します.
42!> 書式は下記のように JIS X 0301 の完全表記です.
43!>
44!> YYYY-MM-DDThh:mm:ss.sTZD
45!>
46!> YYYY は年, MM は月, DD は日, hh は時, mm は分, ss.s は秒,
47!> TZD はタイムゾーンを表します.
48!>
49!> @param[in] time 変換する日時
50!> @return 日時の文字列表現
51!> @endja
52function dcdatetimetochar(time) result(result)
53
54 use dc_types, only: string, token, dp
55 use dc_string, only: tochar, cprintf, stoa
56 use dc_date_generic, only: eval
57 use dc_date_types, only: dc_datetime
58 use dc_message, only: messagenotify
59 implicit none
60 character(STRING):: result
61 type(dc_datetime), intent(in):: time
62
63 integer :: year, mon, day, hour, min, csec_len
64 real(dp):: sec
65 character(TOKEN) :: zone
66 character(STRING) :: csec
67continue
68
69 call eval(time, &
70 & year=year, mon=mon, day=day, hour=hour, min=min, sec=sec, zone=zone)
71
72 csec = tochar(sec)
73 if ( trim(csec) == '-0.' ) csec = '0.'
74 do while ( index('123456789.', csec(len_trim(csec):len_trim(csec)) ) == 0 )
75 if ( len_trim(csec) < 2 ) exit
76 csec = csec(1:len_trim(csec)-1)
77 end do
78 if (int(sec) > -1 .and. int(sec) < 10) then
79 if (len_trim(csec) >= len(csec)) then
80 csec = '0' // trim(csec(1:len(csec) - 1))
81 else
82 csec = '0' // trim(csec)
83 end if
84 end if
85 csec_len = len(trim(adjustl(csec)))
86 if (csec(csec_len:csec_len) == '.') csec = csec(1:csec_len-1)
87
88 result = cprintf('%04d-%02d-%02dT%02d:%02d:%c%c', &
89 & i=(/year, mon, day, hour, min/), &
90 & c1=trim(csec), c2=trim(zone))
91
92end function dcdatetimetochar
93
94
95!> @en
96!> @brief Convert DC_DIFFTIME to character string
97!> @details
98!> Convert a dc_date_types#DC_DIFFTIME variable to a character variable
99!> and return it.
100!> The format is as follows:
101!>
102!> +YYYY-MM-DDThh:mm:ss.s
103!> -YYYY-MM-DDThh:mm:ss.s
104!>
105!> YYYY is year, MM is month, DD is day, hh is hour, mm is minute,
106!> ss.s is seconds.
107!> Note that DD may exceed 2 digits.
108!> (dc_date_types#DC_DIFFTIME is a data type for expressing "X months later",
109!> "X days before", etc., so it does not carry up days to months or
110!> carry down months to days. It also does not hold "year" information.
111!> The number of days in a year or month is determined by the
112!> dc_date_types#DC_DATETIME side).
113!>
114!> If the unit was set to '1' in DCDiffTimeCreate, it is treated as
115!> non-dimensional time, so the format becomes:
116!>
117!> ss.s
118!>
119!> @param[in] diff Time difference to convert
120!> @return Character representation of the time difference
121!> @enden
122!>
123!> @ja
124!> @brief DC_DIFFTIME を文字列に変換
125!> @details
126!> dc_date_types#DC_DIFFTIME 型変数を文字型変数へ変換して返します.
127!> 書式は以下のようになります.
128!>
129!> +YYYY-MM-DDThh:mm:ss.s
130!> -YYYY-MM-DDThh:mm:ss.s
131!>
132!> YYYY は年, MM は月, DD は日, hh は時, mm は分, ss.s は秒を表します.
133!> ただし, DD は 2 桁を超える場合があります.
134!> (dc_date_types#DC_DIFFTIME は X ヶ月後, X 日前, などを表現するため
135!> のデータ型なので, 日を月に繰り上げたり, 月を日に繰り下げることを
136!> しません. また「年」の情報も持ちません. 1 年の日数や 1 月の日数は
137!> dc_date_types#DC_DATETIME 側で決まります).
138!>
139!> なお, DCDiffTimeCreate において, 単位を '1' とした場合は無時限時間と
140!> 扱うため, 以下のような書式となります.
141!>
142!> ss.s
143!>
144!> @param[in] diff 変換する日時差
145!> @return 日時差の文字列表現
146!> @endja
147function dcdifftimetochar(diff) result(result)
148
149 use dc_types, only: string, dp
150 use dc_string, only: tochar, cprintf, stoa
151 use dc_date_generic, only: eval
152 use dc_date_types, only: dc_difftime
153 use dc_scaledsec, only: assignment(=)
154 implicit none
155 character(STRING):: result
156 type(dc_difftime), intent(in):: diff
157
158 integer :: year, mon, day, hour, min, csec_len
159 real(dp):: sec
160 character(STRING) :: csec
161 character(1) :: pm
162continue
163
164 if ( .not. diff % nondim_flag ) then
165 call eval(diff, year=year, mon=mon, day=day, hour=hour, min=min, sec=sec)
166
167 if ( year < 0 .or. mon < 0 .or. day < 0 .or. &
168 & hour < 0 .or. min < 0 .or. sec < 0 ) then
169 year=abs(year) ; mon=abs(mon) ; day=abs(day)
170 hour=abs(hour) ; min=abs(min) ; sec=abs(sec)
171 pm = '-'
172 else
173 pm = '+'
174 end if
175
176 csec = tochar(sec)
177 if ( trim(csec) == '-0.' ) csec = '0.'
178 do while ( index('123456789.', csec(len_trim(csec):len_trim(csec)) ) == 0 )
179 if ( len_trim(csec) < 2 ) exit
180 csec = csec(1:len_trim(csec)-1)
181 end do
182 if (int(sec) > -1 .and. int(sec) < 10) then
183 if (len_trim(csec) >= len(csec)) then
184 csec = '0' // trim(csec(1:len(csec) - 1))
185 else
186 csec = '0' // trim(csec)
187 end if
188 end if
189 csec_len = len(trim(adjustl(csec)))
190 if (csec(csec_len:csec_len) == '.') csec = csec(1:csec_len-1)
191
192 result = cprintf('%c%04d-%02d-%02dT%02d:%02d:%c', &
193 & i=(/year, mon, day, hour, min/), &
194 & c1=pm, c2=trim(csec))
195 else
196 sec = diff % sec
197 result = tochar( sec )
198 end if
199
200end function dcdifftimetochar
201
202!> @en
203!> @brief Convert calendar type of DC_DATETIME to character string
204!> @details
205!> Return the calendar type of a dc_date_types#DC_DATETIME variable as
206!> a character string.
207!> Currently supported calendars are as follows.
208!> Left is the integer variable representing the calendar,
209!> right is the returned string.
210!> If .true. is given to `upcase`, the string is returned in uppercase.
211!>
212!> | Calendar type constant | Returned string |
213!> |------------------------------|-----------------|
214!> | dc_date_types#CAL_CYCLIC | cyclic |
215!> | dc_date_types#CAL_NOLEAP | noleap |
216!> | dc_date_types#CAL_JULIAN | julian |
217!> | dc_date_types#CAL_GREGORIAN | gregorian |
218!>
219!> @param[in] time Datetime whose calendar type to convert
220!> @param[in] upcase If .true., return uppercase string
221!> @return Calendar type as character string
222!> @enden
223!>
224!> @ja
225!> @brief DC_DATETIME の暦タイプを文字列に変換
226!> @details
227!> dc_date_types#DC_DATETIME 型変数の暦を文字型にして返します.
228!> 現在サポートされている暦は以下の通りです.
229!> 左が暦を示す整数型変数, 右が返る文字列です.
230!> `upcase` に .true. を与えた場合には, 大文字となって返ります.
231!>
232!> | 暦タイプ定数 | 返る文字列 |
233!> |------------------------------|------------|
234!> | dc_date_types#CAL_CYCLIC | cyclic |
235!> | dc_date_types#CAL_NOLEAP | noleap |
236!> | dc_date_types#CAL_JULIAN | julian |
237!> | dc_date_types#CAL_GREGORIAN | gregorian |
238!>
239!> @param[in] time 暦タイプを変換する日時
240!> @param[in] upcase .true. なら大文字で返す
241!> @return 暦タイプの文字列表現
242!> @endja
243function dcdatetimetocharcal(time, upcase) result(result)
244
245 use dc_types, only: token
247 use dc_date_types, only: dc_datetime, &
249 use dc_string, only: toupper
250 character(TOKEN) :: result
251 type(dc_datetime), intent(in):: time
252 logical, intent(in), optional:: upcase
253continue
254 select case( time % caltype )
255 case(cal_cyclic)
256 result = 'cyclic'
257 case(cal_noleap)
258 result = 'noleap'
259 case(cal_julian)
260 result = 'julian'
261 case(cal_gregorian)
262 result = 'gregorian'
263 case default
264 result = 'none'
265 end select
266
267 if ( present_and_true(upcase) ) then
268 call toupper(result) ! (inout)
269 end if
270
271end function dcdatetimetocharcal
character(token) function dcdatetimetocharcal(time, upcase)
character(string) function dcdatetimetochar(time)
Convert DC_DATETIME, DC_DIFFTIME to character strings.
character(string) function dcdifftimetochar(diff)
Interface declarations for procedures provided from dc_date.
Derived types and parameters for date and time.
integer, parameter, public cal_noleap
Calendar without leap years (365 days per year)
integer, parameter, public cal_cyclic
Cyclic calendar (30.6 days per month)
integer, parameter, public cal_gregorian
Gregorian calendar
integer, parameter, public cal_julian
Julian calendar
Message output module.
Judge optional control parameters.
logical function, public present_and_true(arg)
Scaled seconds module for precise time operations.
Handling character types.
Definition dc_string.f90:83
Provides kind type parameter values.
Definition dc_types.f90:55
integer, parameter, public token
Character length for word, token
Definition dc_types.f90:128
integer, parameter, public string
Character length for string
Definition dc_types.f90:137
integer, parameter, public dp
Double Precision Real number
Definition dc_types.f90:92