Skip to content

Commit

Permalink
Merge pull request #37 from mannylopez/manny-lopez--timeTestHelper
Browse files Browse the repository at this point in the history
Move time date and timezone helpers to TimeTestHelper
  • Loading branch information
mannylopez authored Jul 23, 2024
2 parents 8705319 + 3a0736d commit 64ccd1a
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 194 deletions.
44 changes: 0 additions & 44 deletions Sources/TinyMoon/TinyMoon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import Foundation

public enum TinyMoon {

// MARK: Public

/// The `Moon` object for a specific date, prioritizing major phases (new moon, first quarter, full moon, last quarter) .
///
/// Use this object when you need a general understanding of the moon's phase for a day, especially when emphasizing the major phases is important for your application's context.
Expand Down Expand Up @@ -31,46 +29,4 @@ public enum TinyMoon {
return ExactMoon(date: date, phaseFraction: moon.phaseFraction)
}

// MARK: Internal

enum TimeZoneOption {
case utc
case pacific
case tokyo

static func createTimeZone(timeZone: TimeZoneOption) -> TimeZone {
switch timeZone {
case .utc:
TimeZone(identifier: "UTC")!
case .pacific:
TimeZone(identifier: "America/Los_Angeles")!
case .tokyo:
TimeZone(identifier: "Asia/Tokyo")!
}
}
}

/// Creates a Date from the given arguments. Default is in UTC timezone.
static func formatDate(
year: Int,
month: Int,
day: Int,
hour: Int = 00,
minute: Int = 00,
second: Int = 00,
timeZone: TimeZone = TimeZoneOption.createTimeZone(timeZone: .utc))
-> Date
{
var components = DateComponents()
components.year = year
components.month = month
components.day = day
components.hour = hour
components.minute = minute
components.second = second
components.timeZone = timeZone

return Calendar.current.date(from: components)!
}

}
30 changes: 15 additions & 15 deletions Tests/TinyMoonTests/AstronomicalConstantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ final class AstronomicalConstantTests: XCTestCase {
}

func test_astronomicalConstant_julianDay() {
let pacificTimeZone = TinyMoon.TimeZoneOption.createTimeZone(timeZone: .pacific)
let pacificTimeZone = TimeTestHelper.TimeZoneOption.createTimeZone(timeZone: .pacific)

// January 6, 2000 @ 00:00:00
var date = TinyMoon.formatDate(year: 2000, month: 01, day: 06)
var date = TimeTestHelper.formatDate(year: 2000, month: 01, day: 06)
var julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
XCTAssertEqual(julianDay, 2451549.5000)

// January 6, 2000 @ 00:00: Pacific
// January 6, 2000 @ 08:00:00 UTC
date = TinyMoon.formatDate(
date = TimeTestHelper.formatDate(
year: 2000,
month: 01,
day: 06,
Expand All @@ -42,7 +42,7 @@ final class AstronomicalConstantTests: XCTestCase {

// January 5, 2000 @ 16:00: Pacific
// January 6, 2000 @ 00:00:00 UTC
date = TinyMoon.formatDate(
date = TimeTestHelper.formatDate(
year: 2000,
month: 01,
day: 5,
Expand All @@ -52,33 +52,33 @@ final class AstronomicalConstantTests: XCTestCase {
XCTAssertEqual(julianDay, 2451549.5000)

// January 6, 2000 @ 20:00:00
date = TinyMoon.formatDate(year: 2000, month: 01, day: 06, hour: 20, minute: 00)
date = TimeTestHelper.formatDate(year: 2000, month: 01, day: 06, hour: 20, minute: 00)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
XCTAssertEqual(julianDay, 2451550.3333333335)

// August 22, 2022 @ 00:00:00
date = TinyMoon.formatDate(year: 2022, month: 08, day: 22, hour: 00, minute: 00)
date = TimeTestHelper.formatDate(year: 2022, month: 08, day: 22, hour: 00, minute: 00)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
XCTAssertEqual(julianDay, 2459813.5000)

// August 22, 2022 @ 04:05:00
date = TinyMoon.formatDate(year: 2022, month: 08, day: 22, hour: 04, minute: 05)
date = TimeTestHelper.formatDate(year: 2022, month: 08, day: 22, hour: 04, minute: 05)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
XCTAssertEqual(julianDay, 2459813.670138889)

// August 22, 2022 @ 14:05:00
date = TinyMoon.formatDate(year: 2022, month: 08, day: 22, hour: 14, minute: 05)
date = TimeTestHelper.formatDate(year: 2022, month: 08, day: 22, hour: 14, minute: 05)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
XCTAssertEqual(julianDay, 2459814.0868055555)

date = TinyMoon.formatDate(year: 2022, month: 08, day: 22, hour: 23, minute: 59)
date = TimeTestHelper.formatDate(year: 2022, month: 08, day: 22, hour: 23, minute: 59)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
XCTAssertEqual(julianDay, 2459814.4993055556)
}

func test_astronomicalConstant_getMoonPhase_moonDetail() {
// Full moon
var date = TinyMoon.formatDate(year: 2024, month: 06, day: 22)
var date = TimeTestHelper.formatDate(year: 2024, month: 06, day: 22)
var julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
var moonDetail = TinyMoon.AstronomicalConstant.getMoonPhase(julianDay: julianDay)

Expand All @@ -92,7 +92,7 @@ final class AstronomicalConstantTests: XCTestCase {
XCTAssertEqual(moonDetail.phase, 0.49835304181785745)

// New moon
date = TinyMoon.formatDate(year: 2024, month: 07, day: 06, hour: 12, minute: 37)
date = TimeTestHelper.formatDate(year: 2024, month: 07, day: 06, hour: 12, minute: 37)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
moonDetail = TinyMoon.AstronomicalConstant.getMoonPhase(julianDay: julianDay)

Expand All @@ -106,7 +106,7 @@ final class AstronomicalConstantTests: XCTestCase {
XCTAssertEqual(moonDetail.phase, 0.019184351732275336)

// First quarter
date = TinyMoon.formatDate(year: 2024, month: 08, day: 12, hour: 15, minute: 18)
date = TimeTestHelper.formatDate(year: 2024, month: 08, day: 12, hour: 15, minute: 18)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
moonDetail = TinyMoon.AstronomicalConstant.getMoonPhase(julianDay: julianDay)

Expand All @@ -120,7 +120,7 @@ final class AstronomicalConstantTests: XCTestCase {
XCTAssertEqual(moonDetail.phase, 0.2505449551679033)

// Last quarter
date = TinyMoon.formatDate(year: 2024, month: 08, day: 26, hour: 09, minute: 25)
date = TimeTestHelper.formatDate(year: 2024, month: 08, day: 26, hour: 09, minute: 25)
julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
moonDetail = TinyMoon.AstronomicalConstant.getMoonPhase(julianDay: julianDay)

Expand All @@ -136,8 +136,8 @@ final class AstronomicalConstantTests: XCTestCase {

func test_moontool() {
// Test taken from https://www.fourmilab.ch/moontoolw/
let utcTimeZone = TinyMoon.TimeZoneOption.createTimeZone(timeZone: .utc)
let date = TinyMoon.formatDate(year: 1999, month: 07, day: 20, hour: 20, minute: 17, second: 40, timeZone: utcTimeZone)
let utcTimeZone = TimeTestHelper.TimeZoneOption.createTimeZone(timeZone: .utc)
let date = TimeTestHelper.formatDate(year: 1999, month: 07, day: 20, hour: 20, minute: 17, second: 40, timeZone: utcTimeZone)
let julianDay = TinyMoon.AstronomicalConstant.julianDay(date)
let moonDetail = TinyMoon.AstronomicalConstant.getMoonPhase(julianDay: julianDay)
XCTAssertEqual(moonDetail.julianDay, 2451380.345601852)
Expand Down
2 changes: 1 addition & 1 deletion Tests/TinyMoonTests/Helpers/MoonTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum MoonTestHelper {
timeZone: TimeZone = utcTimeZone)
-> TinyMoon.Moon
{
let date = TinyMoon.formatDate(year: year, month: month, day: day, hour: hour, minute: minute, timeZone: timeZone)
let date = TimeTestHelper.formatDate(year: year, month: month, day: day, hour: hour, minute: minute, timeZone: timeZone)
return TinyMoon.calculateMoonPhase(date, timeZone: timeZone)
}

Expand Down
46 changes: 46 additions & 0 deletions Tests/TinyMoonTests/Helpers/TimeTestHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Created by manny_lopez on 7/22/24.

import Foundation

enum TimeTestHelper {

enum TimeZoneOption {
case utc
case pacific
case tokyo

static func createTimeZone(timeZone: TimeZoneOption) -> TimeZone {
switch timeZone {
case .utc:
TimeZone(identifier: "UTC")!
case .pacific:
TimeZone(identifier: "America/Los_Angeles")!
case .tokyo:
TimeZone(identifier: "Asia/Tokyo")!
}
}
}

/// Creates a Date from the given arguments. Default is in UTC timezone.
static func formatDate(
year: Int,
month: Int,
day: Int,
hour: Int = 00,
minute: Int = 00,
second: Int = 00,
timeZone: TimeZone = TimeZoneOption.createTimeZone(timeZone: .utc))
-> Date
{
var components = DateComponents()
components.year = year
components.month = month
components.day = day
components.hour = hour
components.minute = minute
components.second = second
components.timeZone = timeZone

return Calendar.current.date(from: components)!
}
}
Loading

0 comments on commit 64ccd1a

Please sign in to comment.