Swift 4 DateFormatter wrong timezone - nsdate

Starting Swift 4, I have one unit test that starts to fail. It seems that the timezone data I get is quite incorrect.
The following working code:
let f1 = DateFormatter()
f1.dateFormat = "yyyy-MM-dd"
let d = f1.date(from: "1999-01-01")
let f2 = DateFormatter()
f2.dateFormat = "Z"
f2.string(from: d)
I'm in America/Los_Angeles, so I get -0800
If I swap 1999 to 1500 with this code:
let f1 = DateFormatter()
f1.dateFormat = "yyyy-MM-dd"
let d = f1.date(from: "1500-01-01")
let f2 = DateFormatter()
f2.dateFormat = "Z"
f2.string(from: d)
I get -075258
Is this even a correct/allowed value? How can I get something valid for usage in a ISO 8601 ?

Related

compare just time part from datetime in kql

I want to compare just the time part of different datetimes.
the result gives me false, but it should be true. what am I missing here?
is there a better way to do it.
this is what I tried, and the logic seems to fail:
let current_future = format_datetime(now(+2h), 'HH:m:s.fff');
//print current_future
let current_past = format_datetime(now(-1h), 'HH:m:s.fff');
//print current_past
let result = iff((todatetime(current_past) < todatetime(current_future)), "true", "false");
print result ```
To extract the time from a datetime value, you should do % 1d.
let x = datetime(2022-01-23 09:55:16);
let y = datetime(2022-01-21 19:55:16);
print x<y, (x%1d) < (y%1d)
Result:
print_0
print_1
False
True
You could use timespan.
let t1 = datetime(2022-01-01 23:44:55);
let t2 = datetime(2022-02-01 08:22:33);
print (t1 - bin(t1,1d)) < (t2 - bin(t2,1d))
Your solution is problematic since you are not using 2 digits hour and alphabetically '8' (like in 8:22:33) is bigger than '23' (like in 23:44:55)

Swift 3.0 convert Double() to NSMutableAttributedString

in advance thanks for help.
I am trying to make calculator application (for specific purposes) and I would like to know, if there exist a way how to convert Double() to NSMutableAttributedString. I need this for label output answer.
Reason of using NSMutableAttributedString is because I would like to have answer with subscripts and upper-scripts.
//example of my code
var a = Double(), b = Double(), c = Double()
a = Double(textField1.text!)
b = Double(textField2.text!)
c = a + b
let font:UIFont? = UIFont(name: "Courier", size:12)
let fontSuper:UIFont? = UIFont(name: "Courier", size:10)
//for x_1 (subscript for "1")
x1_t:NSMutableAttributedString = NSMutableAttributedString(string: "x1", attributes: [NSFontAttributeName:font!])
x1_t.setAttributes([NSFontAttributeName:fontSuper!,NSBaselineOffsetAttributeName:-4], range: NSRange(location:1,length:1))
var result = NSMutableAttributedText()
// what to do to get output for a label like "x_1 = String(c) m"
If there exist another way like append String() to NSAtributedString() - I am looking forward for answers.
As I understand it, your input strings (named "prestring1" and "afterstring1" in your own answer) could just be normal strings without attributes, because you only need the final result to be an attributed string.
This would drastically simplify your function, for example you could use string interpolation first and then only make an attributed string and move up (or down) the last part (or any part you want, I'm using an hardcoded range in my example but it's just an example).
Like:
let randomstring = "Random ="
let afterstring = "m2"
let result: Double = 42.1
func stringer (pre: String,
result: Double,
post: String) -> NSMutableAttributedString
{
let base = "\(pre) \(result) \(post)"
let mutable = NSMutableAttributedString(string: base)
mutable.addAttribute(NSBaselineOffsetAttributeName, value: 4,
range: NSRange(location: mutable.length - 2, length: 2))
return mutable
}
let attributedString = stringer(pre: randomstring, result: result, post: afterstring)
Gives:
I am still not quit sure how to do it, but I could create simple function, which is approximately doing what I need. Here I am sharing my answer in case someone has the same question, but in case someone knows better answer, share it with others :)
var randomstring = "Random ="
var prestring1 = NSMutableAttributedString(string: randomstring)
var afterstring1 = NSMutableAttributedString(string: "m2")
var result1 = Double()
result1 = 42.1
func stringer (prestring: NSMutableAttributedString, result: Double, afterstring: NSMutableAttributedString) -> NSMutableAttributedString {
var mutableatributedresult = NSMutableAttributedString(string: String(result))
var mutableaddition = NSMutableAttributedString(string: " ")
var alltext = NSMutableAttributedString()
alltext.append(prestring)
alltext.append(mutableaddition)
alltext.append(mutableatributedresult)
alltext.append(mutableaddition)
alltext.append(afterstring)
return alltext
}
stringer(prestring: prestring1, result: result1, afterstring: afterstring1)
//This should give result of "Random = 42.1 m2"
If someone knows better solution I am curious.

swift how to convert string to date fraction seconds

I have a string want to convert to date, but below code,
that str
print out show Optional(2016-04-25 17:00:16 +0000)
I want to know how to show exact what it is like Optional(2016-04-26T03:00:16.047)
var str = "2016-04-26T03:00:16.047"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"//this your string date format
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString(str)
print(date) //Optional(2016-04-25 17:00:16 +0000)

Specific String format to NSDate

I have this String 2015-02-17T08:53:22.9170000+00:00 and I want to converted to NSdate.
The prefered format to print is hh:mm:ss-dd-mm-yyyy.
Try this one
let string = "2015-02-17T08:53:22.9170000+00:00"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSz"
if let date = formatter.dateFromString(string) {
//Now you can format what ever you want, like
formatter.dateFormat = "hh:mm:ss-dd-mm-yyyy"
print(formatter.stringFromDate(date))
}

Difference between output NSDate from NSDateFormatter and NSDateComponents

let stringWithDate = "2015-07-16T6:08:32.000Z"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let dateComponents = NSDateComponents()
dateComponents.year = 2015
dateComponents.month = 7
dateComponents.day = 16
dateComponents.hour = 6
dateComponents.minute = 8
dateComponents.second = 32
let dateFromDateFormatter = dateFormatter.dateFromString(stringWithDate) //"Jul 16, 2015, 8:08 AM"
let dateFromDateComponents = NSCalendar.currentCalendar().dateFromComponents(dateComponents)! //"Jul 16, 2015, 6:08 AM"
dateFromDateFormatter == dateFromDateComponents //false
Why these dates are not the same?
After some digging, I have found a solution:
NSDateFormatter includes information about NSTimeZone (Z at the end of Sting) while NSDateComponents doesn't. So we need to simple inform dateComponents about NSTimeZone:
dateComponents.timeZone = NSTimeZone(abbreviation: "UTC")
The output dates are now the same.
Adding the same information to dateFormatter:
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
doesn't change anything, because there is information about NSTimeZone yet.

Resources