NSDate odd behaviour - macos

I'm trying to use +[NSDate dateWithNaturalLanguageString] on Mac OS X (Snow Leopard) to get tomorrow's date.
I was expecting that it would return tomorrow's date at 12AM but it returns tomorrow's date at 12PM. Is this a bug or am I doing something wrong?
Code:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow"];
NSLog(#"DATE: %#", date);
Output: DATE: 2012-08-23 12:00:00 +0100

You need to use tomorrow at midnight instead tomorrow like this:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow at midnight"];
NSLog(#"DATE: %#", date);
Output:
2012-08-24 00:00:00 +0100
Note:
You also can use Tomorrow at < time > AM if You want specific time, for example:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow at 1:00 AM"];
or midnight:
date = [NSDate dateWithNaturalLanguageString:#"tomorrow at 12:00 AM"];

Related

Issue with checking time ranges overlapping

Issue:
overlaps? method doesn't return expect value.
(start_on.to_i..end_on.to_i).overlaps?(ti.start_time.to_i..ti.end_time.to_i)
It returns false, but should be true.
start_on: 2016-08-19 11:00:00 +0200
end_on: 2016-08-19 12:00:00 +0200
ti.start_time: 2000-01-01 08:00:00 UTC
ti.end_time: 2000-01-01 12:00:00 UTC
Hours like 11:00-12:00 overlaps with 08:00-12:00. Why method returns false? All columns in database are type of time. Current date is caused by Time.parse method.
I suppose that problem is with date second part of times have 2000 year, but first 2016. Anyone know how to fix it?
You can try by getting only time by this strftime("%H:%M").
Result:
(start_on.strftime("%H:%M")..end_on.strftime("%H:%M")).overlaps?(ti.start_time.strftime("%H:%M")..ti.end_time.strftime("%H:%M"))

String from Date works, Date from String doesn't work

I'm taking a NSDate and turning it into a dateString. But then...
When I try to take that same dateString and turn it back into a NSDate my dates aren't correct.
NSDate to dateString...
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy H:mm a"
let dateString = dateFormatter.stringFromDate(timeDate as! NSDate)
print("Date: \(dateString)")
Console:
Date: Mar 22, 2016 22:30 PM
Date: Mar 23, 2016 1:00 AM
Date: Mar 23, 2016 9:00 AM
dateString to plainDate...
let reverseDateFormatter = NSDateFormatter()
reverseDateFormatter.dateFormat = "MMM d, yyyy H:mm a"
let plainDate = reverseDateFormatter.dateFromString(dateString)
print("Date Reverse: \(plainDate)")
Console:
Date Reverse: Optional(2016-03-22 17:30:00 +0000)
Date Reverse: Optional(2016-03-23 05:00:00 +0000)
Date Reverse: Optional(2016-03-23 05:00:00 +0000)
When your converting the Date to a string, your losing the timezone information, and then its not able to piece it back together.
Is there some reason you can't store the Date itself as an NSDate and then use that instead of re-building the date from a string?
If you need to convert it back to the full date from the string, you will need to adjust your format to store the timezone as well
stringFromDate returns a non-optional string, but dateFromString returns an optional date. Conditionally unwrap the result before printing it out:
if let result = reverseDateFormatter.dateFromString(dateString) {
print(result)
}

Lua UTC time inconsistencies

Lua 5.1 doc says:
If format starts with '!', then the date is formatted in Coordinated
Universal Time.
If format is %c, !'s behavior seems correct
local date_1 = os.date("!%c")
local date_2 = os.date("%c")
print("utc date: "..date_1)
print("not utc date: "..date_2)
If format is *t, !'s behavior seems swapped
local time_1 = os.time(os.date("!*t"))
local time_2 = os.time(os.date("*t"))
print("should be utc time, but is not: "..time_1) -- this should be UTC, and is not
print("should not be utc time, but is: "..time_2) -- this should not be UTC, but is
Dates are tested with: http://www.epochconverter.com/
Why is that?
The table returned by os.date("!*t") and os.date("*t") is correct. I'm printing only the hour field. Note that they are consistent with %c format:
local date_1 = os.date("!%c")
local date_2 = os.date("%c")
print("utc date: "..date_1)
print("not utc date: "..date_2)
print("utc date hour: " .. os.date("!*t").hour)
print("not utc date hour: " .. os.date("*t").hour)
Output on my machine (China Standard Time, UTC+08:00):
utc date: 02/06/15 02:02:29
not utc date: 02/06/15 10:02:29
utc date hour: 2
not utc date hour: 10
However, os.time takes the table, assuming it's the local time, and returns the epoch. So, the local time is converted to the real epoch, but the utc time is not.
print(os.time{year=1970, month=1, day=1, hour=8})
outputs 0 on my machine.

DateFromString returns wrong value

I'm using NSDateFormatter and datefromString is a whole year off
NSString *date = [[command substringWithRange:NSMakeRange(pos.location + 3, command.length - pos.location - 9)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#" date [%#] " , date );
NSLog(#" lastAccess [%#] " , lastAccess );
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss Z"];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *filedate = [dateFormat dateFromString:date];
NSLog(#"1 %# %f " , filedate , [filedate timeIntervalSince1970]);
NSLog(#"2 %# %f " , lastAccess , [lastAccess timeIntervalSince1970]);
NSLog(#"3 %# " , date);
produces the following output
date [2012-11-18 19:00:23 +0000]
lastAccess [2012-11-18 19:00:21 +0000]
1 2011-11-18 19:00:23 +0000 1321642823.000000
2 2012-11-18 19:00:21 +0000 1353265221.929860
3 2012-11-18 19:00:23 +0000
Date has 2012 which is confirm again with 3)
But check out 1) it has 2011
Yet 1) filedate datefromstring of date
Any ideas ?
Thanks in advance
use lower case letters for the year.
your dateformat should be yyyy-MM-dd HH:mm:ss Z.
according to the unicode date format guide
yyyy means Year
YYYY means Year in "Week of Year" based calendars.
I am not sure why this yields different results, but you should almost always use the lowercase variant.

I want convert from string to date

NSDate *LastBuildDate;
NSDate *PubDate;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyyMMddhhmmss"];
LastBuildDate = [formatter dateFromString:buttoninfoamtion.mLastdate];
PubDate=[formatter dateFromString:newsItem.mPubDate];
if([PubDate compare:LastBuildDate])
buttoninfomation.mlastdate and newsItem.mPubDate is string from OpenAPI
ex)
buttoninfomation.mlastdate: Tue, 18 Oct 2011 15:54:43 +0900
newspub:Tue, 18 Oct 2011 15:58:00 +0900
but
LastBuidDate = (null)
PubDate = (null)
I want to compare two factor .
So date- date or string- string but I think string - string is so complex.
what wrong up codes?
Well the format you've given of "yyyyMMddhhmmss" doesn't match "Tue, 18 Oct 2011 15:54:43 +0900" even slightly.
Try
EEE, dd MMM yyyy HH:mm:ss Z
as the format string - see the iOS documentation and the Unix TR#35 for more information.

Resources