-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Initial NSDateFormatter tests #274
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c13196c
very basic NSDateFormatter test and implementation change
tfrank64 341c874
created initial .ShortStyle NSDateFormatter tests
tfrank64 5c9c18d
all NSDateFormatter tests using default dateStyles are in place
tfrank64 8b7dd2a
added .timeStyle to date formatter tests
tfrank64 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
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,178 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
|
||
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux) | ||
import Foundation | ||
import XCTest | ||
#else | ||
import SwiftFoundation | ||
import SwiftXCTest | ||
#endif | ||
|
||
class TestNSDateFormatter: XCTestCase { | ||
|
||
let DEFAULT_LOCALE = "en_US" | ||
let DEFAULT_TIMEZONE = "GMT" | ||
|
||
static var allTests : [(String, TestNSDateFormatter -> () throws -> Void)] { | ||
return [ | ||
("test_BasicConstruction", test_BasicConstruction), | ||
// ("test_customDateFormat", test_customDateFormat), | ||
("test_dateStyleShort", test_dateStyleShort), | ||
("test_dateStyleMedium", test_dateStyleMedium), | ||
("test_dateStyleLong", test_dateStyleLong), | ||
("test_dateStyleFull", test_dateStyleFull) | ||
] | ||
} | ||
|
||
func test_BasicConstruction() { | ||
let f = NSDateFormatter() | ||
XCTAssertNotNil(f) | ||
} | ||
|
||
func test_customDateFormat() { | ||
let dateFormatter = NSDateFormatter() | ||
dateFormatter.dateFormat = String("dd-MM-yyyy") | ||
let dateStr = dateFormatter.stringFromDate(NSDate()) | ||
|
||
print("With dateFormat '\(dateFormatter.dateFormat)': '\(dateStr)'") | ||
|
||
} | ||
|
||
// ShortStyle | ||
// locale stringFromDate example | ||
// ------ -------------- -------- | ||
// en_US M/d/yy 12/25/15 | ||
func test_dateStyleShort() { | ||
|
||
let timestamps = [ | ||
-31536000 : "1/1/69, 12:00 AM" , 0.0 : "1/1/70, 12:00 AM", 31536000 : "1/1/71, 12:00 AM", | ||
2145916800 : "1/1/38, 12:00 AM", 1456272000 : "2/24/16, 12:00 AM", 1456358399 : "2/24/16, 11:59 PM", | ||
1452574638 : "1/12/16, 4:57 AM", 1455685038 : "2/17/16, 4:57 AM", 1458622638 : "3/22/16, 4:57 AM", | ||
1459745838 : "4/4/16, 4:57 AM", 1462597038 : "5/7/16, 4:57 AM", 1465534638 : "6/10/16, 4:57 AM", | ||
1469854638 : "7/30/16, 4:57 AM", 1470718638 : "8/9/16, 4:57 AM", 1473915438 : "9/15/16, 4:57 AM", | ||
1477285038 : "10/24/16, 4:57 AM", 1478062638 : "11/2/16, 4:57 AM", 1482641838 : "12/25/16, 4:57 AM" | ||
] | ||
|
||
let f = NSDateFormatter() | ||
f.dateStyle = .ShortStyle | ||
f.timeStyle = .ShortStyle | ||
|
||
// ensure tests give consistent results by setting specific timeZone and locale | ||
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) | ||
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) | ||
|
||
for (timestamp, stringResult) in timestamps { | ||
|
||
let testDate = NSDate(timeIntervalSince1970: timestamp) | ||
let sf = f.stringFromDate(testDate) | ||
|
||
XCTAssertEqual(sf, stringResult) | ||
} | ||
|
||
} | ||
|
||
// MediumStyle | ||
// locale stringFromDate example | ||
// ------ -------------- ------------ | ||
// en_US MMM d, y Dec 25, 2015 | ||
func test_dateStyleMedium() { | ||
|
||
let timestamps = [ | ||
-31536000 : "Jan 1, 1969, 12:00:00 AM" , 0.0 : "Jan 1, 1970, 12:00:00 AM", 31536000 : "Jan 1, 1971, 12:00:00 AM", | ||
2145916800 : "Jan 1, 2038, 12:00:00 AM", 1456272000 : "Feb 24, 2016, 12:00:00 AM", 1456358399 : "Feb 24, 2016, 11:59:59 PM", | ||
1452574638 : "Jan 12, 2016, 4:57:18 AM", 1455685038 : "Feb 17, 2016, 4:57:18 AM", 1458622638 : "Mar 22, 2016, 4:57:18 AM", | ||
1459745838 : "Apr 4, 2016, 4:57:18 AM", 1462597038 : "May 7, 2016, 4:57:18 AM", 1465534638 : "Jun 10, 2016, 4:57:18 AM", | ||
1469854638 : "Jul 30, 2016, 4:57:18 AM", 1470718638 : "Aug 9, 2016, 4:57:18 AM", 1473915438 : "Sep 15, 2016, 4:57:18 AM", | ||
1477285038 : "Oct 24, 2016, 4:57:18 AM", 1478062638 : "Nov 2, 2016, 4:57:18 AM", 1482641838 : "Dec 25, 2016, 4:57:18 AM" | ||
] | ||
|
||
let f = NSDateFormatter() | ||
f.dateStyle = .MediumStyle | ||
f.timeStyle = .MediumStyle | ||
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) | ||
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) | ||
|
||
for (timestamp, stringResult) in timestamps { | ||
|
||
let testDate = NSDate(timeIntervalSince1970: timestamp) | ||
let sf = f.stringFromDate(testDate) | ||
|
||
XCTAssertEqual(sf, stringResult) | ||
} | ||
|
||
} | ||
|
||
|
||
// LongStyle | ||
// locale stringFromDate example | ||
// ------ -------------- ----------------- | ||
// en_US MMMM d, y December 25, 2015 | ||
func test_dateStyleLong() { | ||
|
||
let timestamps = [ | ||
-31536000 : "January 1, 1969 at 12:00:00 AM GMT" , 0.0 : "January 1, 1970 at 12:00:00 AM GMT", 31536000 : "January 1, 1971 at 12:00:00 AM GMT", | ||
2145916800 : "January 1, 2038 at 12:00:00 AM GMT", 1456272000 : "February 24, 2016 at 12:00:00 AM GMT", 1456358399 : "February 24, 2016 at 11:59:59 PM GMT", | ||
1452574638 : "January 12, 2016 at 4:57:18 AM GMT", 1455685038 : "February 17, 2016 at 4:57:18 AM GMT", 1458622638 : "March 22, 2016 at 4:57:18 AM GMT", | ||
1459745838 : "April 4, 2016 at 4:57:18 AM GMT", 1462597038 : "May 7, 2016 at 4:57:18 AM GMT", 1465534638 : "June 10, 2016 at 4:57:18 AM GMT", | ||
1469854638 : "July 30, 2016 at 4:57:18 AM GMT", 1470718638 : "August 9, 2016 at 4:57:18 AM GMT", 1473915438 : "September 15, 2016 at 4:57:18 AM GMT", | ||
1477285038 : "October 24, 2016 at 4:57:18 AM GMT", 1478062638 : "November 2, 2016 at 4:57:18 AM GMT", 1482641838 : "December 25, 2016 at 4:57:18 AM GMT" | ||
] | ||
|
||
let f = NSDateFormatter() | ||
f.dateStyle = .LongStyle | ||
f.timeStyle = .LongStyle | ||
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) | ||
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) | ||
|
||
for (timestamp, stringResult) in timestamps { | ||
|
||
let testDate = NSDate(timeIntervalSince1970: timestamp) | ||
let sf = f.stringFromDate(testDate) | ||
|
||
XCTAssertEqual(sf, stringResult) | ||
} | ||
|
||
} | ||
|
||
// FullStyle | ||
// locale stringFromDate example | ||
// ------ -------------- ------------------------- | ||
// en_US EEEE, MMMM d, y Friday, December 25, 2015 | ||
func test_dateStyleFull() { | ||
|
||
let timestamps = [ | ||
-31536000 : "Wednesday, January 1, 1969 at 12:00:00 AM GMT" , 0.0 : "Thursday, January 1, 1970 at 12:00:00 AM GMT", | ||
31536000 : "Friday, January 1, 1971 at 12:00:00 AM GMT", 2145916800 : "Friday, January 1, 2038 at 12:00:00 AM GMT", | ||
1456272000 : "Wednesday, February 24, 2016 at 12:00:00 AM GMT", 1456358399 : "Wednesday, February 24, 2016 at 11:59:59 PM GMT", | ||
1452574638 : "Tuesday, January 12, 2016 at 4:57:18 AM GMT", 1455685038 : "Wednesday, February 17, 2016 at 4:57:18 AM GMT", | ||
1458622638 : "Tuesday, March 22, 2016 at 4:57:18 AM GMT", 1459745838 : "Monday, April 4, 2016 at 4:57:18 AM GMT", | ||
1462597038 : "Saturday, May 7, 2016 at 4:57:18 AM GMT", 1465534638 : "Friday, June 10, 2016 at 4:57:18 AM GMT", | ||
1469854638 : "Saturday, July 30, 2016 at 4:57:18 AM GMT", 1470718638 : "Tuesday, August 9, 2016 at 4:57:18 AM GMT", | ||
1473915438 : "Thursday, September 15, 2016 at 4:57:18 AM GMT", 1477285038 : "Monday, October 24, 2016 at 4:57:18 AM GMT", | ||
1478062638 : "Wednesday, November 2, 2016 at 4:57:18 AM GMT", 1482641838 : "Sunday, December 25, 2016 at 4:57:18 AM GMT" | ||
] | ||
|
||
let f = NSDateFormatter() | ||
f.dateStyle = .FullStyle | ||
f.timeStyle = .FullStyle | ||
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) | ||
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) | ||
|
||
for (timestamp, stringResult) in timestamps { | ||
|
||
let testDate = NSDate(timeIntervalSince1970: timestamp) | ||
let sf = f.stringFromDate(testDate) | ||
|
||
XCTAssertEqual(sf, stringResult) | ||
} | ||
|
||
} | ||
|
||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the correct value be
Greenwich Mean Time
instead ofGMT
?That's what ICU spec says (http://userguide.icu-project.org/formatparse/datetime)
Also, that's the behaviour of
f.timeStyle = .full
on macOS.