Skip to content

Add JSDate implementation with tests #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,27 @@ try test("TypedArray_Mutation") {
}
try expectEqual(toString(array.jsValue().object!), jsStringify(Array(0..<100)))
}

try test("Date") {
let date1 = JSDate()
print(date1.toISOString())
let date2 = JSDate(millisecondsSinceEpoch: date1.now())

try expectEqual(date1.now(), date2.now())
try expectEqual(date1.fullYear, date2.fullYear)
try expectEqual(date1.month, date2.month)
try expectEqual(date1.date, date2.date)
try expectEqual(date1.day, date2.day)
try expectEqual(date1.hours, date2.hours)
try expectEqual(date1.minutes, date2.minutes)
try expectEqual(date1.seconds, date2.seconds)
try expectEqual(date1.milliseconds, date2.milliseconds)
try expectEqual(date1.utcFullYear, date2.utcFullYear)
try expectEqual(date1.utcMonth, date2.utcMonth)
try expectEqual(date1.utcDate, date2.utcDate)
try expectEqual(date1.utcDay, date2.utcDay)
try expectEqual(date1.utcHours, date2.utcHours)
try expectEqual(date1.utcMinutes, date2.utcMinutes)
try expectEqual(date1.utcSeconds, date2.utcSeconds)
try expectEqual(date1.utcMilliseconds, date2.utcMilliseconds)
}
203 changes: 203 additions & 0 deletions Sources/JavaScriptKit/BasicObjects/JSDate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
public final class JSDate {
private static let constructor = JSObject.global.Date.function!
private let ref: JSObject

public init(millisecondsSinceEpoch: Int? = nil) {
if let milliseconds = millisecondsSinceEpoch {
ref = Self.constructor.new(milliseconds)
} else {
ref = Self.constructor.new()
}
}

/** According to the standard, `monthIndex` is zero-indexed, where `11` is December. `day`
represents a day of the month starting at `1`.
*/
public init(
year: Int,
monthIndex: Int,
day: Int = 1,
hours: Int = 0,
minutes: Int = 0,
seconds: Int = 0,
milliseconds: Int = 0
) {
ref = Self.constructor.new(year, monthIndex, day, hours, minutes, seconds, milliseconds)
}

/// Year of this date in local time zone.
public var fullYear: Int {
get {
Int(ref.getFullYear.function!().number!)
}
set {
ref.setFullYear.function!(newValue)
}
}

/// Month of this date in `0–11` range in local time zone
public var month: Int {
get {
Int(ref.getMonth.function!().number!)
}
set {
ref.setMonth.function!(newValue)
}
}

/// The day of the month in `1..31` range in local time zone.
public var date: Int {
get {
Int(ref.getDate.function!().number!)
}
set {
ref.setDate.function!(newValue)
}
}

/// The day of the week in `0..6` range in local time zone.
public var day: Int {
Int(ref.getDay.function!().number!)
}

/// The amount of hours in this day from `0..23` range in local time zone.
public var hours: Int {
get {
Int(ref.getHours.function!().number!)
}
set {
ref.setHours.function!(newValue)
}
}

/// The amount of minutes in this hours from `0..59` range in local time zone.
public var minutes: Int {
get {
Int(ref.getMinutes.function!().number!)
}
set {
ref.setMinutes.function!(newValue)
}
}

/// The amount of seconds in this minute from `0..59` range in local time zone.
public var seconds: Int {
get {
Int(ref.getSeconds.function!().number!)
}
set {
ref.setSeconds.function!(newValue)
}
}

/// The amount of milliseconds in this second `0..999` range in local time zone.
public var milliseconds: Int {
get {
Int(ref.getMilliseconds.function!().number!)
}
set {
ref.setMilliseconds.function!(newValue)
}
}

/// Year of this date in the UTC time zone
public var utcFullYear: Int {
get {
Int(ref.getFullYear.function!().number!)
}
set {
ref.setFullYear.function!(newValue)
}
}

/// Month of this date in `0–11` range in the UTC time zone
public var utcMonth: Int {
get {
Int(ref.getMonth.function!().number!)
}
set {
ref.setMonth.function!(newValue)
}
}

/// The day of the month in `1..31` range in the UTC time zone
public var utcDate: Int {
get {
Int(ref.getDate.function!().number!)
}
set {
ref.setDate.function!(newValue)
}
}

/// The day of the week in `0..6` range in the UTC time zone
public var utcDay: Int {
Int(ref.getDay.function!().number!)
}

/// The amount of hours in this day from `0..23` range in the UTC time zone
public var utcHours: Int {
get {
Int(ref.getHours.function!().number!)
}
set {
ref.setHours.function!(newValue)
}
}

/// The amount of minutes in this hours from `0..59` range in the UTC time zone
public var utcMinutes: Int {
get {
Int(ref.getMinutes.function!().number!)
}
set {
ref.setMinutes.function!(newValue)
}
}

/// The amount of seconds in this minute from `0..59` range in the UTC time zone
public var utcSeconds: Int {
get {
Int(ref.getSeconds.function!().number!)
}
set {
ref.setSeconds.function!(newValue)
}
}

/// The amount of milliseconds in this second `0..999` range in the UTC time zone
public var utcMilliseconds: Int {
get {
Int(ref.getMilliseconds.function!().number!)
}
set {
ref.setMilliseconds.function!(newValue)
}
}

/// Offset in minutes between the local time zone and UTC
public var timezoneOffset: Int {
Int(ref.getTimezoneOffset.function!().number!)
}

public func toISOString() -> String {
ref.toISOString.function!().string!
}

public func toLocaleDateString() -> String {
ref.toLocaleDateString.function!().string!
}

public func toLocaleTimeString() -> String {
ref.toLocaleTimeString.function!().string!
}

public func toUTCString() -> String {
ref.toUTCString.function!().string!
}

/// Number of seconds since epoch ignoring leap seconds
public func now() -> Int {
Int(ref.now.function!().number!)
}
}