gtool5 Fortran 90/95 Library 1.0.0-rc5
日本語
Loading...
Searching...
No Matches
dcdatetimezone.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, 2000-2026. All rights reserved. <br/>
8!> License is BSD-2-Clause. see [COPYRIGHT](@ref COPYRIGHT) in detail
9!> @en
10!> @brief Procedures for timezone handling
11!> @details
12!> Procedures described in this file are provided from "dc_date" module.
13!> @enden
14!>
15!> @ja
16!> @brief タイムゾーンに関する手続き
17!> @details
18!> このファイルで提供される手続き群は dc_date モジュールにて提供されます。
19!> @endja
20!>
21
22!> @en
23!> @brief Set the timezone of a datetime
24!> @details
25!> Change the timezone of the argument `time` to `zone`.
26!> The actual datetime is not changed.
27!>
28!> If an invalid value is given to the argument `zone`,
29!> an error is raised.
30!> If the argument `err` is given, .true. is returned to `err`
31!> and the program continues.
32!>
33!> @param[inout] time Datetime to change timezone
34!> @param[in] zone Timezone to set (e.g., "+09:00")
35!> @param[out] err Exception handling flag
36!> @enden
37!>
38!> @ja
39!> @brief 日時のタイムゾーンを設定
40!> @details
41!> 引数 `time` のタイムゾーンを `zone` へと変更します.
42!> 実質的な日時は変更しません.
43!>
44!> 引数 `zone` に不適切な値が与えられた場合,
45!> エラーを発生させます.
46!> 引数 `err` を与えている場合には `err` に .true. が返り,
47!> プログラムは続行します.
48!>
49!> @param[inout] time タイムゾーンを変更する日時
50!> @param[in] zone 設定するタイムゾーン (例: "+09:00")
51!> @param[out] err 例外処理用フラグ
52!> @endja
53subroutine dcdatetimesetzone(time, zone, err)
54
55 use dc_types, only: string
58 & operator(-), operator(+)
60 use dc_message, only: messagenotify
61 use dc_trace, only: beginsub, endsub
62 use dc_string, only: stoi
63 implicit none
64 type(dc_datetime), intent(inout):: time
65 character(*), intent(in):: zone
66 logical, intent(out), optional:: err
67 type(dc_difftime):: diff, diff_in
68 integer :: stat
69 character(STRING) :: zone_in, cause_c
70 character(*), parameter :: subname = 'DCDateTimeSetZone'
71continue
72 call beginsub(subname, 'time=%c, zone=%c', &
73 & c1=trim(tochar(time)), c2=trim(zone))
74 stat = dc_noerr
75 cause_c = ''
76
77 if (.not. validzone(zone)) then
78 stat = dc_ebadtimezone
79 cause_c = zone
80 if (present(err)) then
81 call messagenotify('W', subname, &
82 & 'zone=<%c> is invalid.', &
83 & c1=trim(zone))
84 else
85 goto 999
86 end if
87 end if
88
89 call eval(time, zone = zone_in)
90 diff_in = zonetodiff(zone_in)
91 diff = zonetodiff(zone)
92
93 time = time + (diff_in - diff)
94 time % zone = zone
95
96999 continue
97 call storeerror(stat, subname, err, cause_c)
98 call endsub(subname, 'time=%c', &
99 & c1=trim(tochar(time)))
100end subroutine dcdatetimesetzone
101
102!> @en
103!> @brief Convert timezone string to DC_DIFFTIME
104!> @details
105!> Convert the given timezone to a dc_date_types#DC_DIFFTIME variable
106!> and return it. If the timezone notation is invalid, it is
107!> interpreted as if '+00:00' was given.
108!>
109!> @param[in] zone Timezone string (e.g., "+09:00")
110!> @return Time difference corresponding to the timezone
111!> @enden
112!>
113!> @ja
114!> @brief タイムゾーン文字列を DC_DIFFTIME に変換
115!> @details
116!> 与えられるタイムゾーンを dc_date_types#DC_DIFFTIME 変数へと
117!> 変換して返します. タイムゾーンの表記が無効な場合は '+00:00'
118!> が与えられたと解釈します.
119!>
120!> @param[in] zone タイムゾーン文字列 (例: "+09:00")
121!> @return タイムゾーンに対応する時間差
122!> @endja
123function dcdatetimezonetodiff(zone) result(diff)
124
125 use dc_date_types, only: dc_difftime
127 use dc_string, only: stoi
128 implicit none
129 type(dc_difftime):: diff
130 character(*), intent(in):: zone
131 integer:: hour, min, sgn
132continue
133 if (.not. validzone(zone)) then
134 call dcdifftimecreate(diff)
135 else
136 if (zone(1:1) == '-') then
137 sgn = 1
138 else
139 sgn = -1
140 end if
141 hour = stoi(zone(2:3))
142 min = stoi(zone(5:6))
143 call dcdifftimecreate(diff, hour = hour * sgn, min = min * sgn)
144 end if
145end function dcdatetimezonetodiff
146
147!> @en
148!> @brief Check if timezone notation is valid
149!> @details
150!> Returns .true. if the given timezone notation is valid,
151!> .false. otherwise.
152!>
153!> The timezone notation should be like '+09:00', where the 1st character
154!> is '+' or '-', 2nd-3rd and 5th-6th characters are digits,
155!> and 4th character is ':'.
156!>
157!> @param[in] zone Timezone string to check
158!> @return .true. if valid, .false. otherwise
159!> @enden
160!>
161!> @ja
162!> @brief タイムゾーン表記が有効かどうかをチェック
163!> @details
164!> 与えられるタイムゾーンの表記が有効であれば
165!> .true. を, それ以外の場合は .false. を返します.
166!>
167!> タイムゾーンの表記は '+09:00' のように, 1 文字目が '+' または '-',
168!> 2〜3, 5〜6 文字目が数値で, 4 文字目が ':' となります.
169!>
170!> @param[in] zone チェックするタイムゾーン文字列
171!> @return 有効な場合は .true., それ以外は .false.
172!> @endja
173function dcdatetimevalidzone(zone) result(result)
174
175 implicit none
176 character(*), intent(in):: zone
177 logical:: result
178continue
179 result = .false.
180 if (len(zone) < 6) return
181 if (verify(zone(1:1), '+-') /= 0) return
182 if (verify(zone(2:3), '1234567890') /= 0) return
183 if (verify(zone(5:6), '1234567890') /= 0) return
184 if (zone(4:4) /= ':') return
185 result = .true.
186end function dcdatetimevalidzone
type(dc_difftime) function dcdatetimezonetodiff(zone)
logical function dcdatetimevalidzone(zone)
subroutine dcdatetimesetzone(time, zone, err)
Procedures for timezone handling.
Interface declarations for procedures provided from dc_date.
Derived types and parameters for date and time.
Error handling module.
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
Error storage variables
Definition dc_error.f90:468
integer, parameter, public dc_ebadtimezone
Definition dc_error.f90:538
Message output module.
Handling character types.
Definition dc_string.f90:83
Debug tracing module.
Definition dc_trace.f90:150
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
Provides kind type parameter values.
Definition dc_types.f90:55
integer, parameter, public string
Character length for string
Definition dc_types.f90:137