|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | + |
| 10 | +#if DEPLOYMENT_RUNTIME_OBJC || os(Linux) |
| 11 | + import Foundation |
| 12 | + import XCTest |
| 13 | +#else |
| 14 | + import SwiftFoundation |
| 15 | + import SwiftXCTest |
| 16 | +#endif |
| 17 | + |
| 18 | +class TestNSDateFormatter: XCTestCase { |
| 19 | + |
| 20 | + let DEFAULT_LOCALE = "en_US" |
| 21 | + let DEFAULT_TIMEZONE = "GMT" |
| 22 | + |
| 23 | + static var allTests : [(String, TestNSDateFormatter -> () throws -> Void)] { |
| 24 | + return [ |
| 25 | + ("test_BasicConstruction", test_BasicConstruction), |
| 26 | +// ("test_customDateFormat", test_customDateFormat), |
| 27 | + ("test_dateStyleShort", test_dateStyleShort), |
| 28 | + ("test_dateStyleMedium", test_dateStyleMedium), |
| 29 | + ("test_dateStyleLong", test_dateStyleLong), |
| 30 | + ("test_dateStyleFull", test_dateStyleFull) |
| 31 | + ] |
| 32 | + } |
| 33 | + |
| 34 | + func test_BasicConstruction() { |
| 35 | + let f = NSDateFormatter() |
| 36 | + XCTAssertNotNil(f) |
| 37 | + } |
| 38 | + |
| 39 | + func test_customDateFormat() { |
| 40 | + let dateFormatter = NSDateFormatter() |
| 41 | + dateFormatter.dateFormat = String("dd-MM-yyyy") |
| 42 | + let dateStr = dateFormatter.stringFromDate(NSDate()) |
| 43 | + |
| 44 | + print("With dateFormat '\(dateFormatter.dateFormat)': '\(dateStr)'") |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + // ShortStyle |
| 49 | + // locale stringFromDate example |
| 50 | + // ------ -------------- -------- |
| 51 | + // en_US M/d/yy 12/25/15 |
| 52 | + func test_dateStyleShort() { |
| 53 | + |
| 54 | + let timestamps = [ |
| 55 | + -31536000 : "1/1/69, 12:00 AM" , 0.0 : "1/1/70, 12:00 AM", 31536000 : "1/1/71, 12:00 AM", |
| 56 | + 2145916800 : "1/1/38, 12:00 AM", 1456272000 : "2/24/16, 12:00 AM", 1456358399 : "2/24/16, 11:59 PM", |
| 57 | + 1452574638 : "1/12/16, 4:57 AM", 1455685038 : "2/17/16, 4:57 AM", 1458622638 : "3/22/16, 4:57 AM", |
| 58 | + 1459745838 : "4/4/16, 4:57 AM", 1462597038 : "5/7/16, 4:57 AM", 1465534638 : "6/10/16, 4:57 AM", |
| 59 | + 1469854638 : "7/30/16, 4:57 AM", 1470718638 : "8/9/16, 4:57 AM", 1473915438 : "9/15/16, 4:57 AM", |
| 60 | + 1477285038 : "10/24/16, 4:57 AM", 1478062638 : "11/2/16, 4:57 AM", 1482641838 : "12/25/16, 4:57 AM" |
| 61 | + ] |
| 62 | + |
| 63 | + let f = NSDateFormatter() |
| 64 | + f.dateStyle = .ShortStyle |
| 65 | + f.timeStyle = .ShortStyle |
| 66 | + |
| 67 | + // ensure tests give consistent results by setting specific timeZone and locale |
| 68 | + f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) |
| 69 | + f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) |
| 70 | + |
| 71 | + for (timestamp, stringResult) in timestamps { |
| 72 | + |
| 73 | + let testDate = NSDate(timeIntervalSince1970: timestamp) |
| 74 | + let sf = f.stringFromDate(testDate) |
| 75 | + |
| 76 | + XCTAssertEqual(sf, stringResult) |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + // MediumStyle |
| 82 | + // locale stringFromDate example |
| 83 | + // ------ -------------- ------------ |
| 84 | + // en_US MMM d, y Dec 25, 2015 |
| 85 | + func test_dateStyleMedium() { |
| 86 | + |
| 87 | + let timestamps = [ |
| 88 | + -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", |
| 89 | + 2145916800 : "Jan 1, 2038, 12:00:00 AM", 1456272000 : "Feb 24, 2016, 12:00:00 AM", 1456358399 : "Feb 24, 2016, 11:59:59 PM", |
| 90 | + 1452574638 : "Jan 12, 2016, 4:57:18 AM", 1455685038 : "Feb 17, 2016, 4:57:18 AM", 1458622638 : "Mar 22, 2016, 4:57:18 AM", |
| 91 | + 1459745838 : "Apr 4, 2016, 4:57:18 AM", 1462597038 : "May 7, 2016, 4:57:18 AM", 1465534638 : "Jun 10, 2016, 4:57:18 AM", |
| 92 | + 1469854638 : "Jul 30, 2016, 4:57:18 AM", 1470718638 : "Aug 9, 2016, 4:57:18 AM", 1473915438 : "Sep 15, 2016, 4:57:18 AM", |
| 93 | + 1477285038 : "Oct 24, 2016, 4:57:18 AM", 1478062638 : "Nov 2, 2016, 4:57:18 AM", 1482641838 : "Dec 25, 2016, 4:57:18 AM" |
| 94 | + ] |
| 95 | + |
| 96 | + let f = NSDateFormatter() |
| 97 | + f.dateStyle = .MediumStyle |
| 98 | + f.timeStyle = .MediumStyle |
| 99 | + f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) |
| 100 | + f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) |
| 101 | + |
| 102 | + for (timestamp, stringResult) in timestamps { |
| 103 | + |
| 104 | + let testDate = NSDate(timeIntervalSince1970: timestamp) |
| 105 | + let sf = f.stringFromDate(testDate) |
| 106 | + |
| 107 | + XCTAssertEqual(sf, stringResult) |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + // LongStyle |
| 114 | + // locale stringFromDate example |
| 115 | + // ------ -------------- ----------------- |
| 116 | + // en_US MMMM d, y December 25, 2015 |
| 117 | + func test_dateStyleLong() { |
| 118 | + |
| 119 | + let timestamps = [ |
| 120 | + -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", |
| 121 | + 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", |
| 122 | + 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", |
| 123 | + 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", |
| 124 | + 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", |
| 125 | + 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" |
| 126 | + ] |
| 127 | + |
| 128 | + let f = NSDateFormatter() |
| 129 | + f.dateStyle = .LongStyle |
| 130 | + f.timeStyle = .LongStyle |
| 131 | + f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) |
| 132 | + f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) |
| 133 | + |
| 134 | + for (timestamp, stringResult) in timestamps { |
| 135 | + |
| 136 | + let testDate = NSDate(timeIntervalSince1970: timestamp) |
| 137 | + let sf = f.stringFromDate(testDate) |
| 138 | + |
| 139 | + XCTAssertEqual(sf, stringResult) |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + // FullStyle |
| 145 | + // locale stringFromDate example |
| 146 | + // ------ -------------- ------------------------- |
| 147 | + // en_US EEEE, MMMM d, y Friday, December 25, 2015 |
| 148 | + func test_dateStyleFull() { |
| 149 | + |
| 150 | + let timestamps = [ |
| 151 | + -31536000 : "Wednesday, January 1, 1969 at 12:00:00 AM GMT" , 0.0 : "Thursday, January 1, 1970 at 12:00:00 AM GMT", |
| 152 | + 31536000 : "Friday, January 1, 1971 at 12:00:00 AM GMT", 2145916800 : "Friday, January 1, 2038 at 12:00:00 AM GMT", |
| 153 | + 1456272000 : "Wednesday, February 24, 2016 at 12:00:00 AM GMT", 1456358399 : "Wednesday, February 24, 2016 at 11:59:59 PM GMT", |
| 154 | + 1452574638 : "Tuesday, January 12, 2016 at 4:57:18 AM GMT", 1455685038 : "Wednesday, February 17, 2016 at 4:57:18 AM GMT", |
| 155 | + 1458622638 : "Tuesday, March 22, 2016 at 4:57:18 AM GMT", 1459745838 : "Monday, April 4, 2016 at 4:57:18 AM GMT", |
| 156 | + 1462597038 : "Saturday, May 7, 2016 at 4:57:18 AM GMT", 1465534638 : "Friday, June 10, 2016 at 4:57:18 AM GMT", |
| 157 | + 1469854638 : "Saturday, July 30, 2016 at 4:57:18 AM GMT", 1470718638 : "Tuesday, August 9, 2016 at 4:57:18 AM GMT", |
| 158 | + 1473915438 : "Thursday, September 15, 2016 at 4:57:18 AM GMT", 1477285038 : "Monday, October 24, 2016 at 4:57:18 AM GMT", |
| 159 | + 1478062638 : "Wednesday, November 2, 2016 at 4:57:18 AM GMT", 1482641838 : "Sunday, December 25, 2016 at 4:57:18 AM GMT" |
| 160 | + ] |
| 161 | + |
| 162 | + let f = NSDateFormatter() |
| 163 | + f.dateStyle = .FullStyle |
| 164 | + f.timeStyle = .FullStyle |
| 165 | + f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE) |
| 166 | + f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE) |
| 167 | + |
| 168 | + for (timestamp, stringResult) in timestamps { |
| 169 | + |
| 170 | + let testDate = NSDate(timeIntervalSince1970: timestamp) |
| 171 | + let sf = f.stringFromDate(testDate) |
| 172 | + |
| 173 | + XCTAssertEqual(sf, stringResult) |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments