-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ca6a08b
Basic JSDate implementation with a simple test
MaxDesiatov 6546f3a
Fix tests, address PR feedback
MaxDesiatov e134515
Fix UTC properties, refine test expectations
MaxDesiatov cbea311
Implement Equatable and Comparable on JSDate
MaxDesiatov 3bc0294
Update Sources/JavaScriptKit/BasicObjects/JSDate.swift
MaxDesiatov 08a0b78
Update Sources/JavaScriptKit/BasicObjects/JSDate.swift
MaxDesiatov 6c142eb
Update Sources/JavaScriptKit/BasicObjects/JSDate.swift
MaxDesiatov 25356e8
Merge branch 'master' into jsdate
MaxDesiatov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.