├── .gitignore ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── EraCalculator │ ├── Configuration.swift │ ├── Const.swift │ ├── Era.swift │ ├── EraConvertable.swift │ ├── EraType+Comparable.swift │ ├── EraType+Date.swift │ └── EraType.swift └── Tests ├── EraCalculatorTests └── EraTypeTest.swift └── LinuxMain.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 hirose yudai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:4.2 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "EraCalculator", 8 | products: [ 9 | // Products define the executables and libraries produced by a package, and make them visible to other packages. 10 | .library( 11 | name: "EraCalculator", 12 | targets: ["EraCalculator"]), 13 | ], 14 | dependencies: [ 15 | // Dependencies declare other packages that this package depends on. 16 | // .package(url: /* package url */, from: "1.0.0"), 17 | ], 18 | targets: [ 19 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 20 | // Targets can depend on other targets in this package, and on products in packages which this package depends on. 21 | .target( 22 | name: "EraCalculator", 23 | dependencies: []), 24 | .testTarget( 25 | name: "EraCalculatorTests", 26 | dependencies: ["EraCalculator"]), 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EraCalculator 2 | Last library of 平成. 3 | So, in Japan change era. This library is my memorial library. 4 | 5 | ## Usage 6 | EraCalculator can convert other Era years. 7 | This example is Calculates what year of 大化 was when the 平成 was 大化. 8 | 9 | ```swift 10 | let expected = Era(eraType: .大化, year: 1345) 11 | let got = EraType.平成.convert(to: EraType.大化) 12 | XCTAssertEqual(got.eraType, expected.eraType) 13 | XCTAssertEqual(got.year, expected.year) 14 | XCTAssertEqual(got.description, "大化 1345年") 15 | ``` 16 | 17 | ## LICENSE 18 | EraCalculator is available under the MIT license. 19 | See the LICENSE file for more info. 20 | -------------------------------------------------------------------------------- /Sources/EraCalculator/Configuration.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Configuration.swift 3 | // EraCalculator 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import Foundation 9 | 10 | public struct Configuration { 11 | public var calendar: Calendar 12 | public var timeZone: TimeZone 13 | 14 | public init( 15 | calendar: Calendar = Calendar(identifier: .gregorian), 16 | timeZone: TimeZone = .current 17 | ) { 18 | self.calendar = calendar 19 | self.timeZone = timeZone 20 | } 21 | } 22 | 23 | var defaultConfiguration = Configuration() 24 | public func setConfiguration(_ configuration: Configuration) { 25 | defaultConfiguration = configuration 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Sources/EraCalculator/Const.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Const.swift 3 | // EraCalculator 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import Foundation 9 | 10 | typealias TimeIntervalType = Foundation.TimeInterval 11 | struct Const { 12 | struct TimeInterval { 13 | static let second: TimeIntervalType = 1 14 | static let minute: TimeIntervalType = second * 60 15 | static let hour: TimeIntervalType = minute * 60 16 | static let day: TimeIntervalType = hour * 24 17 | static let weekday: TimeIntervalType = day * 7 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Sources/EraCalculator/Era.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EraType+Function.swift 3 | // EraCalculator 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import Foundation 9 | 10 | 11 | public struct Era { 12 | public let eraType: EraType 13 | public let year: Int 14 | 15 | init( 16 | eraType: EraType, 17 | year: Int 18 | ) { 19 | self.eraType = eraType 20 | self.year = year 21 | } 22 | } 23 | 24 | extension Era: CustomStringConvertible { 25 | public var description: String { 26 | return "\(eraType.gengo) \(year)年" 27 | } 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Sources/EraCalculator/EraConvertable.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EraConvertable.swift 3 | // EraCalculator 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import Foundation 9 | 10 | public protocol EraConvertable { 11 | var compareYear: Int { get } 12 | } 13 | 14 | extension Era: EraConvertable { 15 | public var compareYear: Int { 16 | return eraType.startYear + year - 1 17 | } 18 | } 19 | 20 | extension Date: EraConvertable { 21 | public var compareYear: Int { 22 | return defaultConfiguration.calendar.dateComponents( 23 | in: defaultConfiguration.timeZone, 24 | from: self 25 | ) 26 | .year! 27 | } 28 | } 29 | 30 | extension EraType: EraConvertable { 31 | public var compareYear: Int { 32 | return startDate.compareYear 33 | } 34 | } 35 | 36 | extension EraConvertable { 37 | fileprivate func convert(with year: Int) -> EraType { 38 | return EraType 39 | .allCases 40 | .filter { $0.compareYear == year } 41 | .first! 42 | } 43 | public func convert(to eraType: EraConvertable) -> Era { 44 | let year = eraType.compareYear 45 | return Era( 46 | eraType: convert(with: year), 47 | year: compareYear - year + 1 48 | ) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Sources/EraCalculator/EraType+Comparable.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EraType+Comparable.swift 3 | // EraCalculator 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import Foundation 9 | 10 | extension EraType: Comparable { 11 | public static func < (lhs: EraType, rhs: EraType) -> Bool { 12 | return lhs.startDate < rhs.startDate 13 | } 14 | 15 | public static func <= (lhs: EraType, rhs: EraType) -> Bool { 16 | return lhs.startDate <= rhs.startDate 17 | } 18 | 19 | public static func >= (lhs: EraType, rhs: EraType) -> Bool { 20 | return lhs.startDate >= rhs.startDate 21 | } 22 | 23 | public static func > (lhs: EraType, rhs: EraType) -> Bool { 24 | return lhs.startDate >= rhs.startDate 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Sources/EraCalculator/EraType+Date.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EraType+Date.swift 3 | // EraCalculator 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import Foundation 9 | 10 | // MARK: - Start 11 | extension EraType { 12 | fileprivate var startDateComponents: DateComponents { 13 | return DateComponents(calendar: defaultConfiguration.calendar, era: 1, year: startYear, month: startMonth, day: startDay) 14 | } 15 | public var startDate: Date { 16 | return startDateComponents.date! 17 | } 18 | } 19 | 20 | // MARK: - End 21 | extension EraType { 22 | fileprivate var afterStartDate: Date? { 23 | guard let after = EraType(rawValue: rawValue + 1) else { 24 | return nil 25 | } 26 | return after.startDate 27 | } 28 | public var endDate: Date? { 29 | guard let afterStartDate = afterStartDate else { 30 | return nil 31 | } 32 | return Date(timeIntervalSince1970: afterStartDate.timeIntervalSince1970 - Const.TimeInterval.day) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Sources/EraCalculator/EraType.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EraType.swift 3 | // EraCalculator 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import Foundation 9 | 10 | public enum EraType: Int, CaseIterable { 11 | case 大化 12 | case 白雉 13 | case 朱鳥 14 | case 大宝 15 | case 慶雲 16 | case 和銅 17 | case 霊亀 18 | case 養老 19 | case 神亀 20 | case 天平 21 | case 天平感宝 22 | case 天平勝宝 23 | case 天平宝字 24 | case 天平神護 25 | case 神護景雲 26 | case 宝亀 27 | case 天応 28 | case 延暦 29 | case 大同 30 | case 弘仁 31 | case 天長 32 | case 承和 33 | case 嘉祥 34 | case 仁寿 35 | case 斉衡 36 | case 天安 37 | case 貞観 38 | case 元慶 39 | case 仁和 40 | case 寛平 41 | case 昌泰 42 | case 延喜 43 | case 延長 44 | case 承平 45 | case 天慶 46 | case 天暦 47 | case 天徳 48 | case 応和 49 | case 康保 50 | case 安和 51 | case 天禄 52 | case 天延 53 | case 貞元 54 | case 天元 55 | case 永観 56 | case 寛和 57 | case 永延 58 | case 永祚 59 | case 正暦 60 | case 長徳 61 | case 長保 62 | case 寛弘 63 | case 長和 64 | case 寛仁 65 | case 治安 66 | case 万寿 67 | case 長元 68 | case 長暦 69 | case 長久 70 | case 寛徳 71 | case 永承 72 | case 天喜 73 | case 康平 74 | case 治暦 75 | case 延久 76 | case 承保 77 | case 承暦 78 | case 永保 79 | case 応徳 80 | case 寛治 81 | case 嘉保 82 | case 永長 83 | case 承徳 84 | case 康和 85 | case 長治 86 | case 嘉承 87 | case 天仁 88 | case 天永 89 | case 永久 90 | case 元永 91 | case 保安 92 | case 天治 93 | case 大治 94 | case 天承 95 | case 長承 96 | case 保延 97 | case 永治 98 | case 康治 99 | case 天養 100 | case 久安 101 | case 仁平 102 | case 久寿 103 | case 保元 104 | case 平治 105 | case 永暦 106 | case 応保 107 | case 長寛 108 | case 永万 109 | case 仁安 110 | case 嘉応 111 | case 承安 112 | case 安元 113 | case 治承 114 | case 養和 115 | case 寿永 116 | case 元暦 117 | case 文治 118 | case 建久 119 | case 正治 120 | case 建仁 121 | case 元久 122 | case 建永 123 | case 承元 124 | case 建暦 125 | case 建保 126 | case 承久 127 | case 貞応 128 | case 元仁 129 | case 嘉禄 130 | case 安貞 131 | case 寛喜 132 | case 貞永 133 | case 天福 134 | case 文暦 135 | case 嘉禎 136 | case 暦仁 137 | case 延応 138 | case 仁治 139 | case 寛元 140 | case 宝治 141 | case 建長 142 | case 康元 143 | case 正嘉 144 | case 正元 145 | case 文応 146 | case 弘長 147 | case 文永 148 | case 建治 149 | case 弘安 150 | case 正応 151 | case 永仁 152 | case 正安 153 | case 乾元 154 | case 嘉元 155 | case 徳治 156 | case 延慶 157 | case 応長 158 | case 正和 159 | case 文保 160 | case 元応 161 | case 元亨 162 | case 正中 163 | case 嘉暦 164 | case 元徳 165 | case 元弘 166 | case 正慶 167 | case 建武 168 | case 延元 169 | case 興国 170 | case 正平 171 | case 建徳 172 | case 文中 173 | case 天授 174 | case 弘和 175 | case 元中 176 | case 暦応 177 | case 康永 178 | case 貞和 179 | case 観応 180 | case 文和 181 | case 延文 182 | case 康安 183 | case 貞治 184 | case 応安 185 | case 永和 186 | case 康暦 187 | case 永徳 188 | case 至徳 189 | case 嘉慶 190 | case 康応 191 | case 明徳 192 | case 応永 193 | case 正長 194 | case 永享 195 | case 嘉吉 196 | case 文安 197 | case 宝徳 198 | case 享徳 199 | case 康正 200 | case 長禄 201 | case 寛正 202 | case 文正 203 | case 応仁 204 | case 文明 205 | case 長享 206 | case 延徳 207 | case 明応 208 | case 文亀 209 | case 永正 210 | case 大永 211 | case 享禄 212 | case 天文 213 | case 弘治 214 | case 永禄 215 | case 元亀 216 | case 天正 217 | case 文禄 218 | case 慶長 219 | case 元和 220 | case 寛永 221 | case 正保 222 | case 慶安 223 | case 承応 224 | case 明暦 225 | case 万治 226 | case 寛文 227 | case 延宝 228 | case 天和 229 | case 貞享 230 | case 元禄 231 | case 宝永 232 | case 正徳 233 | case 享保 234 | case 元文 235 | case 寛保 236 | case 延享 237 | case 寛延 238 | case 宝暦 239 | case 明和 240 | case 安永 241 | case 天明 242 | case 寛政 243 | case 享和 244 | case 文化 245 | case 文政 246 | case 天保 247 | case 弘化 248 | case 嘉永 249 | case 安政 250 | case 万延 251 | case 文久 252 | case 元治 253 | case 慶応 254 | case 明治 255 | case 大正 256 | case 昭和 257 | case 平成 258 | case 令和 259 | } 260 | 261 | extension EraType { 262 | public var gengo: String { 263 | return "\(self)" 264 | } 265 | } 266 | 267 | extension EraType { 268 | public var startYear: Int { 269 | switch self { 270 | case .大化: return 645 271 | case .白雉: return 650 272 | case .朱鳥: return 686 273 | case .大宝: return 701 274 | case .慶雲: return 704 275 | case .和銅: return 708 276 | case .霊亀: return 715 277 | case .養老: return 717 278 | case .神亀: return 724 279 | case .天平: return 729 280 | case .天平感宝: return 749 281 | case .天平勝宝: return 749 282 | case .天平宝字: return 757 283 | case .天平神護: return 765 284 | case .神護景雲: return 767 285 | case .宝亀: return 770 286 | case .天応: return 781 287 | case .延暦: return 782 288 | case .大同: return 806 289 | case .弘仁: return 810 290 | case .天長: return 824 291 | case .承和: return 834 292 | case .嘉祥: return 848 293 | case .仁寿: return 851 294 | case .斉衡: return 854 295 | case .天安: return 857 296 | case .貞観: return 859 297 | case .元慶: return 877 298 | case .仁和: return 885 299 | case .寛平: return 889 300 | case .昌泰: return 898 301 | case .延喜: return 901 302 | case .延長: return 923 303 | case .承平: return 931 304 | case .天慶: return 938 305 | case .天暦: return 947 306 | case .天徳: return 957 307 | case .応和: return 961 308 | case .康保: return 964 309 | case .安和: return 968 310 | case .天禄: return 970 311 | case .天延: return 973 312 | case .貞元: return 976 313 | case .天元: return 978 314 | case .永観: return 983 315 | case .寛和: return 985 316 | case .永延: return 987 317 | case .永祚: return 989 318 | case .正暦: return 990 319 | case .長徳: return 995 320 | case .長保: return 999 321 | case .寛弘: return 1004 322 | case .長和: return 1012 323 | case .寛仁: return 1017 324 | case .治安: return 1021 325 | case .万寿: return 1024 326 | case .長元: return 1028 327 | case .長暦: return 1037 328 | case .長久: return 1040 329 | case .寛徳: return 1044 330 | case .永承: return 1046 331 | case .天喜: return 1053 332 | case .康平: return 1058 333 | case .治暦: return 1065 334 | case .延久: return 1069 335 | case .承保: return 1074 336 | case .承暦: return 1077 337 | case .永保: return 1081 338 | case .応徳: return 1084 339 | case .寛治: return 1087 340 | case .嘉保: return 1095 341 | case .永長: return 1097 342 | case .承徳: return 1097 343 | case .康和: return 1099 344 | case .長治: return 1104 345 | case .嘉承: return 1106 346 | case .天仁: return 1108 347 | case .天永: return 1110 348 | case .永久: return 1113 349 | case .元永: return 1118 350 | case .保安: return 1120 351 | case .天治: return 1124 352 | case .大治: return 1126 353 | case .天承: return 1131 354 | case .長承: return 1132 355 | case .保延: return 1135 356 | case .永治: return 1141 357 | case .康治: return 1142 358 | case .天養: return 1144 359 | case .久安: return 1145 360 | case .仁平: return 1151 361 | case .久寿: return 1154 362 | case .保元: return 1156 363 | case .平治: return 1159 364 | case .永暦: return 1160 365 | case .応保: return 1161 366 | case .長寛: return 1163 367 | case .永万: return 1165 368 | case .仁安: return 1166 369 | case .嘉応: return 1169 370 | case .承安: return 1171 371 | case .安元: return 1175 372 | case .治承: return 1177 373 | case .養和: return 1181 374 | case .寿永: return 1182 375 | case .元暦: return 1184 376 | case .文治: return 1185 377 | case .建久: return 1190 378 | case .正治: return 1199 379 | case .建仁: return 1201 380 | case .元久: return 1204 381 | case .建永: return 1206 382 | case .承元: return 1207 383 | case .建暦: return 1211 384 | case .建保: return 1214 385 | case .承久: return 1219 386 | case .貞応: return 1222 387 | case .元仁: return 1224 388 | case .嘉禄: return 1225 389 | case .安貞: return 1228 390 | case .寛喜: return 1229 391 | case .貞永: return 1232 392 | case .天福: return 1233 393 | case .文暦: return 1234 394 | case .嘉禎: return 1235 395 | case .暦仁: return 1238 396 | case .延応: return 1239 397 | case .仁治: return 1240 398 | case .寛元: return 1243 399 | case .宝治: return 1247 400 | case .建長: return 1249 401 | case .康元: return 1256 402 | case .正嘉: return 1257 403 | case .正元: return 1259 404 | case .文応: return 1260 405 | case .弘長: return 1261 406 | case .文永: return 1264 407 | case .建治: return 1275 408 | case .弘安: return 1278 409 | case .正応: return 1288 410 | case .永仁: return 1293 411 | case .正安: return 1299 412 | case .乾元: return 1302 413 | case .嘉元: return 1303 414 | case .徳治: return 1307 415 | case .延慶: return 1308 416 | case .応長: return 1311 417 | case .正和: return 1312 418 | case .文保: return 1317 419 | case .元応: return 1319 420 | case .元亨: return 1321 421 | case .正中: return 1324 422 | case .嘉暦: return 1326 423 | case .元徳: return 1329 424 | case .元弘: return 1331 425 | case .正慶: return 1332 426 | case .建武: return 1334 427 | case .延元: return 1336 428 | case .興国: return 1340 429 | case .正平: return 1347 430 | case .建徳: return 1370 431 | case .文中: return 1372 432 | case .天授: return 1375 433 | case .弘和: return 1381 434 | case .元中: return 1384 435 | case .暦応: return 1338 436 | case .康永: return 1342 437 | case .貞和: return 1345 438 | case .観応: return 1350 439 | case .文和: return 1352 440 | case .延文: return 1356 441 | case .康安: return 1361 442 | case .貞治: return 1362 443 | case .応安: return 1368 444 | case .永和: return 1375 445 | case .康暦: return 1379 446 | case .永徳: return 1381 447 | case .至徳: return 1384 448 | case .嘉慶: return 1387 449 | case .康応: return 1389 450 | case .明徳: return 1390 451 | case .応永: return 1394 452 | case .正長: return 1428 453 | case .永享: return 1429 454 | case .嘉吉: return 1441 455 | case .文安: return 1444 456 | case .宝徳: return 1449 457 | case .享徳: return 1452 458 | case .康正: return 1455 459 | case .長禄: return 1457 460 | case .寛正: return 1461 461 | case .文正: return 1466 462 | case .応仁: return 1467 463 | case .文明: return 1469 464 | case .長享: return 1487 465 | case .延徳: return 1489 466 | case .明応: return 1492 467 | case .文亀: return 1501 468 | case .永正: return 1504 469 | case .大永: return 1521 470 | case .享禄: return 1528 471 | case .天文: return 1532 472 | case .弘治: return 1555 473 | case .永禄: return 1558 474 | case .元亀: return 1570 475 | case .天正: return 1573 476 | case .文禄: return 1593 477 | case .慶長: return 1596 478 | case .元和: return 1615 479 | case .寛永: return 1624 480 | case .正保: return 1645 481 | case .慶安: return 1648 482 | case .承応: return 1652 483 | case .明暦: return 1655 484 | case .万治: return 1658 485 | case .寛文: return 1661 486 | case .延宝: return 1673 487 | case .天和: return 1681 488 | case .貞享: return 1684 489 | case .元禄: return 1688 490 | case .宝永: return 1704 491 | case .正徳: return 1711 492 | case .享保: return 1716 493 | case .元文: return 1736 494 | case .寛保: return 1741 495 | case .延享: return 1744 496 | case .寛延: return 1748 497 | case .宝暦: return 1751 498 | case .明和: return 1764 499 | case .安永: return 1772 500 | case .天明: return 1781 501 | case .寛政: return 1789 502 | case .享和: return 1801 503 | case .文化: return 1804 504 | case .文政: return 1818 505 | case .天保: return 1831 506 | case .弘化: return 1845 507 | case .嘉永: return 1848 508 | case .安政: return 1855 509 | case .万延: return 1860 510 | case .文久: return 1861 511 | case .元治: return 1864 512 | case .慶応: return 1865 513 | case .明治: return 1868 514 | case .大正: return 1912 515 | case .昭和: return 1926 516 | case .平成: return 1989 517 | case .令和: return 2019 518 | } 519 | } 520 | 521 | public var endYear: Int { 522 | switch self { 523 | case .大化: return 650 524 | case .白雉: return 686 525 | case .朱鳥: return 701 526 | case .大宝: return 704 527 | case .慶雲: return 708 528 | case .和銅: return 715 529 | case .霊亀: return 717 530 | case .養老: return 724 531 | case .神亀: return 729 532 | case .天平: return 749 533 | case .天平感宝: return 749 534 | case .天平勝宝: return 757 535 | case .天平宝字: return 765 536 | case .天平神護: return 767 537 | case .神護景雲: return 770 538 | case .宝亀: return 781 539 | case .天応: return 782 540 | case .延暦: return 806 541 | case .大同: return 810 542 | case .弘仁: return 824 543 | case .天長: return 834 544 | case .承和: return 848 545 | case .嘉祥: return 851 546 | case .仁寿: return 854 547 | case .斉衡: return 857 548 | case .天安: return 859 549 | case .貞観: return 877 550 | case .元慶: return 885 551 | case .仁和: return 889 552 | case .寛平: return 898 553 | case .昌泰: return 901 554 | case .延喜: return 923 555 | case .延長: return 931 556 | case .承平: return 938 557 | case .天慶: return 947 558 | case .天暦: return 957 559 | case .天徳: return 961 560 | case .応和: return 964 561 | case .康保: return 968 562 | case .安和: return 970 563 | case .天禄: return 973 564 | case .天延: return 976 565 | case .貞元: return 978 566 | case .天元: return 983 567 | case .永観: return 985 568 | case .寛和: return 987 569 | case .永延: return 989 570 | case .永祚: return 990 571 | case .正暦: return 995 572 | case .長徳: return 999 573 | case .長保: return 1004 574 | case .寛弘: return 1012 575 | case .長和: return 1017 576 | case .寛仁: return 1021 577 | case .治安: return 1024 578 | case .万寿: return 1028 579 | case .長元: return 1037 580 | case .長暦: return 1040 581 | case .長久: return 1044 582 | case .寛徳: return 1046 583 | case .永承: return 1053 584 | case .天喜: return 1058 585 | case .康平: return 1065 586 | case .治暦: return 1069 587 | case .延久: return 1074 588 | case .承保: return 1077 589 | case .承暦: return 1081 590 | case .永保: return 1084 591 | case .応徳: return 1087 592 | case .寛治: return 1095 593 | case .嘉保: return 1097 594 | case .永長: return 1097 595 | case .承徳: return 1099 596 | case .康和: return 1104 597 | case .長治: return 1106 598 | case .嘉承: return 1108 599 | case .天仁: return 1110 600 | case .天永: return 1113 601 | case .永久: return 1118 602 | case .元永: return 1120 603 | case .保安: return 1124 604 | case .天治: return 1126 605 | case .大治: return 1131 606 | case .天承: return 1132 607 | case .長承: return 1135 608 | case .保延: return 1141 609 | case .永治: return 1142 610 | case .康治: return 1144 611 | case .天養: return 1145 612 | case .久安: return 1151 613 | case .仁平: return 1154 614 | case .久寿: return 1156 615 | case .保元: return 1159 616 | case .平治: return 1160 617 | case .永暦: return 1161 618 | case .応保: return 1163 619 | case .長寛: return 1165 620 | case .永万: return 1166 621 | case .仁安: return 1169 622 | case .嘉応: return 1171 623 | case .承安: return 1175 624 | case .安元: return 1177 625 | case .治承: return 1181 626 | case .養和: return 1182 627 | case .寿永: return 1184 628 | case .元暦: return 1185 629 | case .文治: return 1190 630 | case .建久: return 1199 631 | case .正治: return 1201 632 | case .建仁: return 1204 633 | case .元久: return 1206 634 | case .建永: return 1207 635 | case .承元: return 1211 636 | case .建暦: return 1214 637 | case .建保: return 1219 638 | case .承久: return 1222 639 | case .貞応: return 1224 640 | case .元仁: return 1225 641 | case .嘉禄: return 1228 642 | case .安貞: return 1229 643 | case .寛喜: return 1232 644 | case .貞永: return 1233 645 | case .天福: return 1234 646 | case .文暦: return 1235 647 | case .嘉禎: return 1238 648 | case .暦仁: return 1239 649 | case .延応: return 1240 650 | case .仁治: return 1243 651 | case .寛元: return 1247 652 | case .宝治: return 1249 653 | case .建長: return 1256 654 | case .康元: return 1257 655 | case .正嘉: return 1259 656 | case .正元: return 1260 657 | case .文応: return 1261 658 | case .弘長: return 1264 659 | case .文永: return 1275 660 | case .建治: return 1278 661 | case .弘安: return 1288 662 | case .正応: return 1293 663 | case .永仁: return 1299 664 | case .正安: return 1302 665 | case .乾元: return 1303 666 | case .嘉元: return 1307 667 | case .徳治: return 1308 668 | case .延慶: return 1311 669 | case .応長: return 1312 670 | case .正和: return 1317 671 | case .文保: return 1319 672 | case .元応: return 1321 673 | case .元亨: return 1324 674 | case .正中: return 1326 675 | case .嘉暦: return 1329 676 | case .元徳: return 1331 677 | case .元弘: return 1332 678 | case .正慶: return 1334 679 | case .建武: return 1336 680 | case .延元: return 1340 681 | case .興国: return 1347 682 | case .正平: return 1370 683 | case .建徳: return 1372 684 | case .文中: return 1375 685 | case .天授: return 1381 686 | case .弘和: return 1384 687 | case .元中: return 1338 688 | case .暦応: return 1342 689 | case .康永: return 1345 690 | case .貞和: return 1350 691 | case .観応: return 1352 692 | case .文和: return 1356 693 | case .延文: return 1361 694 | case .康安: return 1362 695 | case .貞治: return 1368 696 | case .応安: return 1375 697 | case .永和: return 1379 698 | case .康暦: return 1381 699 | case .永徳: return 1384 700 | case .至徳: return 1387 701 | case .嘉慶: return 1389 702 | case .康応: return 1390 703 | case .明徳: return 1394 704 | case .応永: return 1428 705 | case .正長: return 1429 706 | case .永享: return 1441 707 | case .嘉吉: return 1444 708 | case .文安: return 1449 709 | case .宝徳: return 1452 710 | case .享徳: return 1455 711 | case .康正: return 1457 712 | case .長禄: return 1461 713 | case .寛正: return 1466 714 | case .文正: return 1467 715 | case .応仁: return 1469 716 | case .文明: return 1487 717 | case .長享: return 1489 718 | case .延徳: return 1492 719 | case .明応: return 1501 720 | case .文亀: return 1504 721 | case .永正: return 1521 722 | case .大永: return 1528 723 | case .享禄: return 1532 724 | case .天文: return 1555 725 | case .弘治: return 1558 726 | case .永禄: return 1570 727 | case .元亀: return 1573 728 | case .天正: return 1593 729 | case .文禄: return 1596 730 | case .慶長: return 1615 731 | case .元和: return 1624 732 | case .寛永: return 1645 733 | case .正保: return 1648 734 | case .慶安: return 1652 735 | case .承応: return 1655 736 | case .明暦: return 1658 737 | case .万治: return 1661 738 | case .寛文: return 1673 739 | case .延宝: return 1681 740 | case .天和: return 1684 741 | case .貞享: return 1688 742 | case .元禄: return 1704 743 | case .宝永: return 1711 744 | case .正徳: return 1716 745 | case .享保: return 1736 746 | case .元文: return 1741 747 | case .寛保: return 1744 748 | case .延享: return 1748 749 | case .寛延: return 1751 750 | case .宝暦: return 1764 751 | case .明和: return 1772 752 | case .安永: return 1781 753 | case .天明: return 1789 754 | case .寛政: return 1801 755 | case .享和: return 1804 756 | case .文化: return 1818 757 | case .文政: return 1831 758 | case .天保: return 1845 759 | case .弘化: return 1848 760 | case .嘉永: return 1855 761 | case .安政: return 1860 762 | case .万延: return 1861 763 | case .文久: return 1864 764 | case .元治: return 1865 765 | case .慶応: return 1868 766 | case .明治: return 1912 767 | case .大正: return 1926 768 | case .昭和: return 1989 769 | case .平成: return 2019 770 | case .令和: fatalError("Unknown end") 771 | } 772 | } 773 | 774 | } 775 | 776 | 777 | 778 | extension EraType { 779 | public var startMonth: Int { 780 | return startMonthAndDayComponent.month 781 | } 782 | public var startDay: Int { 783 | return startMonthAndDayComponent.day 784 | } 785 | fileprivate var startMonthAndDayComponent: (month: Int, day: Int) { 786 | switch self { 787 | case .大化: return (month: 7, day: 17) 788 | case .白雉: return (month: 3, day: 22) 789 | case .朱鳥: return (month: 8, day: 14) 790 | case .大宝: return (month: 5, day: 3) 791 | case .慶雲: return (month: 6, day: 16) 792 | case .和銅: return (month: 2, day: 7) 793 | case .霊亀: return (month: 10, day: 3) 794 | case .養老: return (month: 12, day: 24) 795 | case .神亀: return (month: 3, day: 3) 796 | case .天平: return (month: 9, day: 2) 797 | case .天平感宝: return (month: 5, day: 4) 798 | case .天平勝宝: return (month: 8, day: 19) 799 | case .天平宝字: return (month: 9, day: 6) 800 | case .天平神護: return (month: 2, day: 1) 801 | case .神護景雲: return (month: 9, day: 13) 802 | case .宝亀: return (month: 10, day: 23) 803 | case .天応: return (month: 1, day: 30) 804 | case .延暦: return (month: 9, day: 30) 805 | case .大同: return (month: 6, day: 8) 806 | case .弘仁: return (month: 10, day: 20) 807 | case .天長: return (month: 2, day: 8) 808 | case .承和: return (month: 2, day: 14) 809 | case .嘉祥: return (month: 7, day: 16) 810 | case .仁寿: return (month: 6, day: 1) 811 | case .斉衡: return (month: 12, day: 23) 812 | case .天安: return (month: 3, day: 20) 813 | case .貞観: return (month: 5, day: 20) 814 | case .元慶: return (month: 6, day: 1) 815 | case .仁和: return (month: 3, day: 11) 816 | case .寛平: return (month: 5, day: 30) 817 | case .昌泰: return (month: 5, day: 20) 818 | case .延喜: return (month: 8, day: 31) 819 | case .延長: return (month: 5, day: 29) 820 | case .承平: return (month: 5, day: 16) 821 | case .天慶: return (month: 6, day: 22) 822 | case .天暦: return (month: 5, day: 15) 823 | case .天徳: return (month: 11, day: 21) 824 | case .応和: return (month: 3, day: 5) 825 | case .康保: return (month: 8, day: 19) 826 | case .安和: return (month: 9, day: 8) 827 | case .天禄: return (month: 5, day: 3) 828 | case .天延: return (month: 1, day: 16) 829 | case .貞元: return (month: 8, day: 11) 830 | case .天元: return (month: 12, day: 31) 831 | case .永観: return (month: 5, day: 29) 832 | case .寛和: return (month: 5, day: 19) 833 | case .永延: return (month: 5, day: 5) 834 | case .永祚: return (month: 9, day: 10) 835 | case .正暦: return (month: 11, day: 26) 836 | case .長徳: return (month: 3, day: 25) 837 | case .長保: return (month: 2, day: 1) 838 | case .寛弘: return (month: 8, day: 8) 839 | case .長和: return (month: 2, day: 8) 840 | case .寛仁: return (month: 5, day: 21) 841 | case .治安: return (month: 3, day: 17) 842 | case .万寿: return (month: 8, day: 19) 843 | case .長元: return (month: 8, day: 18) 844 | case .長暦: return (month: 5, day: 9) 845 | case .長久: return (month: 12, day: 16) 846 | case .寛徳: return (month: 12, day: 16) 847 | case .永承: return (month: 5, day: 22) 848 | case .天喜: return (month: 2, day: 2) 849 | case .康平: return (month: 9, day: 19) 850 | case .治暦: return (month: 9, day: 4) 851 | case .延久: return (month: 5, day: 6) 852 | case .承保: return (month: 9, day: 16) 853 | case .承暦: return (month: 12, day: 5) 854 | case .永保: return (month: 3, day: 22) 855 | case .応徳: return (month: 3, day: 15) 856 | case .寛治: return (month: 5, day: 11) 857 | case .嘉保: return (month: 1, day: 23) 858 | case .永長: return (month: 1, day: 3) 859 | case .承徳: return (month: 12, day: 27) 860 | case .康和: return (month: 9, day: 15) 861 | case .長治: return (month: 3, day: 8) 862 | case .嘉承: return (month: 5, day: 13) 863 | case .天仁: return (month: 9, day: 9) 864 | case .天永: return (month: 7, day: 31) 865 | case .永久: return (month: 8, day: 25) 866 | case .元永: return (month: 4, day: 25) 867 | case .保安: return (month: 5, day: 9) 868 | case .天治: return (month: 5, day: 18) 869 | case .大治: return (month: 2, day: 15) 870 | case .天承: return (month: 2, day: 28) 871 | case .長承: return (month: 9, day: 21) 872 | case .保延: return (month: 6, day: 10) 873 | case .永治: return (month: 8, day: 13) 874 | case .康治: return (month: 5, day: 25) 875 | case .天養: return (month: 3, day: 28) 876 | case .久安: return (month: 8, day: 12) 877 | case .仁平: return (month: 2, day: 14) 878 | case .久寿: return (month: 12, day: 4) 879 | case .保元: return (month: 5, day: 18) 880 | case .平治: return (month: 5, day: 9) 881 | case .永暦: return (month: 2, day: 18) 882 | case .応保: return (month: 9, day: 24) 883 | case .長寛: return (month: 5, day: 4) 884 | case .永万: return (month: 7, day: 14) 885 | case .仁安: return (month: 9, day: 23) 886 | case .嘉応: return (month: 5, day: 6) 887 | case .承安: return (month: 5, day: 27) 888 | case .安元: return (month: 8, day: 16) 889 | case .治承: return (month: 8, day: 29) 890 | case .養和: return (month: 8, day: 25) 891 | case .寿永: return (month: 6, day: 29) 892 | case .元暦: return (month: 5, day: 27) 893 | case .文治: return (month: 9, day: 9) 894 | case .建久: return (month: 5, day: 16) 895 | case .正治: return (month: 5, day: 23) 896 | case .建仁: return (month: 3, day: 19) 897 | case .元久: return (month: 3, day: 23) 898 | case .建永: return (month: 6, day: 5) 899 | case .承元: return (month: 11, day: 16) 900 | case .建暦: return (month: 4, day: 23) 901 | case .建保: return (month: 1, day: 18) 902 | case .承久: return (month: 5, day: 27) 903 | case .貞応: return (month: 5, day: 25) 904 | case .元仁: return (month: 12, day: 31) 905 | case .嘉禄: return (month: 5, day: 28) 906 | case .安貞: return (month: 1, day: 18) 907 | case .寛喜: return (month: 3, day: 31) 908 | case .貞永: return (month: 4, day: 23) 909 | case .天福: return (month: 5, day: 25) 910 | case .文暦: return (month: 11, day: 27) 911 | case .嘉禎: return (month: 11, day: 1) 912 | case .暦仁: return (month: 12, day: 30) 913 | case .延応: return (month: 3, day: 13) 914 | case .仁治: return (month: 8, day: 5) 915 | case .寛元: return (month: 3, day: 18) 916 | case .宝治: return (month: 4, day: 5) 917 | case .建長: return (month: 5, day: 2) 918 | case .康元: return (month: 10, day: 24) 919 | case .正嘉: return (month: 3, day: 31) 920 | case .正元: return (month: 4, day: 20) 921 | case .文応: return (month: 5, day: 24) 922 | case .弘長: return (month: 3, day: 22) 923 | case .文永: return (month: 3, day: 27) 924 | case .建治: return (month: 5, day: 22) 925 | case .弘安: return (month: 3, day: 23) 926 | case .正応: return (month: 5, day: 29) 927 | case .永仁: return (month: 9, day: 6) 928 | case .正安: return (month: 5, day: 25) 929 | case .乾元: return (month: 12, day: 10) 930 | case .嘉元: return (month: 9, day: 16) 931 | case .徳治: return (month: 1, day: 18) 932 | case .延慶: return (month: 11, day: 22) 933 | case .応長: return (month: 5, day: 17) 934 | case .正和: return (month: 4, day: 27) 935 | case .文保: return (month: 3, day: 16) 936 | case .元応: return (month: 5, day: 18) 937 | case .元亨: return (month: 3, day: 22) 938 | case .正中: return (month: 12, day: 25) 939 | case .嘉暦: return (month: 5, day: 28) 940 | case .元徳: return (month: 9, day: 22) 941 | case .元弘: return (month: 9, day: 11) 942 | case .正慶: return (month: 5, day: 23) 943 | case .建武: return (month: 3, day: 5) 944 | case .延元: return (month: 4, day: 11) 945 | case .興国: return (month: 5, day: 25) 946 | case .正平: return (month: 1, day: 20) 947 | case .建徳: return (month: 8, day: 16) 948 | case .文中: return (month: 5, day: 1) // unknown day 949 | case .天授: return (month: 6, day: 26) 950 | case .弘和: return (month: 3, day: 6) 951 | case .元中: return (month: 5, day: 18) 952 | case .暦応: return (month: 10, day: 11) 953 | case .康永: return (month: 6, day: 1) 954 | case .貞和: return (month: 11, day: 15) 955 | case .観応: return (month: 4, day: 4) 956 | case .文和: return (month: 11, day: 4) 957 | case .延文: return (month: 4, day: 29) 958 | case .康安: return (month: 5, day: 4) 959 | case .貞治: return (month: 10, day: 11) 960 | case .応安: return (month: 3, day: 7) 961 | case .永和: return (month: 3, day: 29) 962 | case .康暦: return (month: 4, day: 9) 963 | case .永徳: return (month: 3, day: 20) 964 | case .至徳: return (month: 3, day: 19) 965 | case .嘉慶: return (month: 10, day: 5) 966 | case .康応: return (month: 3, day: 7) 967 | case .明徳: return (month: 4, day: 12) 968 | case .応永: return (month: 8, day: 2) 969 | case .正長: return (month: 6, day: 10) 970 | case .永享: return (month: 10, day: 3) 971 | case .嘉吉: return (month: 3, day: 10) 972 | case .文安: return (month: 2, day: 23) 973 | case .宝徳: return (month: 8, day: 16) 974 | case .享徳: return (month: 8, day: 10) 975 | case .康正: return (month: 9, day: 6) 976 | case .長禄: return (month: 10, day: 16) 977 | case .寛正: return (month: 2, day: 1) 978 | case .文正: return (month: 3, day: 14) 979 | case .応仁: return (month: 4, day: 9) 980 | case .文明: return (month: 6, day: 8) 981 | case .長享: return (month: 8, day: 9) 982 | case .延徳: return (month: 9, day: 16) 983 | case .明応: return (month: 8, day: 12) 984 | case .文亀: return (month: 3, day: 18) 985 | case .永正: return (month: 3, day: 16) 986 | case .大永: return (month: 9, day: 23) 987 | case .享禄: return (month: 9, day: 3) 988 | case .天文: return (month: 8, day: 29) 989 | case .弘治: return (month: 11, day: 7) 990 | case .永禄: return (month: 3, day: 18) 991 | case .元亀: return (month: 5, day: 27) 992 | case .天正: return (month: 8, day: 25) 993 | case .文禄: return (month: 1, day: 10) 994 | case .慶長: return (month: 12, day: 16) 995 | case .元和: return (month: 9, day: 5) 996 | case .寛永: return (month: 4, day: 17) 997 | case .正保: return (month: 1, day: 13) 998 | case .慶安: return (month: 4, day: 7) 999 | case .承応: return (month: 10, day: 20) 1000 | case .明暦: return (month: 5, day: 18) 1001 | case .万治: return (month: 8, day: 21) 1002 | case .寛文: return (month: 5, day: 23) 1003 | case .延宝: return (month: 10, day: 30) 1004 | case .天和: return (month: 11, day: 9) 1005 | case .貞享: return (month: 4, day: 5) 1006 | case .元禄: return (month: 10, day: 23) 1007 | case .宝永: return (month: 4, day: 16) 1008 | case .正徳: return (month: 6, day: 11) 1009 | case .享保: return (month: 8, day: 9) 1010 | case .元文: return (month: 6, day: 7) 1011 | case .寛保: return (month: 4, day: 12) 1012 | case .延享: return (month: 4, day: 3) 1013 | case .寛延: return (month: 8, day: 5) 1014 | case .宝暦: return (month: 12, day: 14) 1015 | case .明和: return (month: 6, day: 30) 1016 | case .安永: return (month: 12, day: 10) 1017 | case .天明: return (month: 4, day: 25) 1018 | case .寛政: return (month: 2, day: 19) 1019 | case .享和: return (month: 3, day: 19) 1020 | case .文化: return (month: 3, day: 22) 1021 | case .文政: return (month: 5, day: 26) 1022 | case .天保: return (month: 1, day: 23) 1023 | case .弘化: return (month: 1, day: 9) 1024 | case .嘉永: return (month: 4, day: 1) 1025 | case .安政: return (month: 1, day: 15) 1026 | case .万延: return (month: 4, day: 8) 1027 | case .文久: return (month: 3, day: 29) 1028 | case .元治: return (month: 3, day: 27) 1029 | case .慶応: return (month: 5, day: 1) 1030 | case .明治: return (month: 1, day: 25) 1031 | case .大正: return (month: 7, day: 30) 1032 | case .昭和: return (month: 12, day: 25) 1033 | case .平成: return (month: 1, day: 8) 1034 | case .令和: return (month: 5, day: 1) 1035 | } 1036 | } 1037 | } 1038 | -------------------------------------------------------------------------------- /Tests/EraCalculatorTests/EraTypeTest.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EraTypeTest.swift 3 | // EraCalculatorTests 4 | // 5 | // Created by Yudai.Hirose on 2019/04/30. 6 | // 7 | 8 | import XCTest 9 | @testable import EraCalculator 10 | 11 | class EraTypeTest: XCTestCase { 12 | 13 | override func setUp() { 14 | // Put setup code here. This method is called before the invocation of each test method in the class. 15 | } 16 | 17 | override func tearDown() { 18 | // Put teardown code here. This method is called after the invocation of each test method in the class. 19 | } 20 | 21 | func testBaseProperities() { 22 | XCTAssertEqual(EraType.平成.compareYear, 1989) 23 | } 24 | 25 | func testConvert() { 26 | do { 27 | let sender = Era(eraType: .平成, year: 31) 28 | let expected = Era(eraType: .大化, year: 1375) 29 | let got = sender.convert(to: EraType.大化) 30 | XCTAssertEqual(got.eraType, expected.eraType) 31 | XCTAssertEqual(got.year, expected.year) 32 | XCTAssertEqual(got.description, "大化 1375年") 33 | } 34 | 35 | do { 36 | let expected = Era(eraType: .大化, year: 1345) 37 | let got = EraType.平成.convert(to: EraType.大化) 38 | XCTAssertEqual(got.eraType, expected.eraType) 39 | XCTAssertEqual(got.year, expected.year) 40 | XCTAssertEqual(got.description, "大化 1345年") 41 | } 42 | } 43 | 44 | func testPerformanceExample() { 45 | // This is an example of a performance test case. 46 | self.measure { 47 | // Put the code you want to measure the time of here. 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import EraCalculatorTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += EraCalculatorTests.allTests() 7 | XCTMain(tests) --------------------------------------------------------------------------------