matthewyork / datetools Goto Github PK
View Code? Open in Web Editor NEWDates and times made easy in iOS
License: MIT License
Dates and times made easy in iOS
License: MIT License
Is there an easy way to find the start of the week? Or is there some detailed documentation available somewhere?
If there is no easy way to get the start of the week this might be a nice-to-have feature.
Thanks in advance
Why doesn't the methods have documentation in their header files?
It has been brought to my attention that NSDateFormatter and NSCalendar are both thread-unsafe. We need to introduce a locking system to ensure that no NSCalendar or NSDateFormatter instances are mutated on different threads at the same time.
If I'm not wrong, the date comparison methods requires 24 hours for a day to be counted as 1 day. How can I get the difference between days as calendar days?
As quoted from the comments of this SO answer:
If fromDateTime = Feb 1st 11:00pm and toDateTime = Feb 2nd 01:00am the result should be 1 (even though it's only 2 hours, but it's another date).
That SO answer also returns the difference of days in "calendar days".
I was using that SO answer initially and I realised the abnormality after switching to DateTools. Is there a way that I can achieve that "calendar days difference" result with DateTools?
If I try and create a date from a format string every so often it will return nil, whilst using dateWithYear:month:day:
with the same parameters will return the correct value.
Unsure if this is a bug with the library, looking at the method it seems like it might be something I am doing wrong, however I would like to know what I may be doing wrong.
NSNumber *year = @(2015);
NSNumber *month = @(10);
NSString *dateString = [NSString stringWithFormat:@"01/%@/%@", month, year];
NSDate *key = [NSDate dateWithString:dateString formatString:@"dd/MM/yyyy"]; // Sometimes nil
NSDate *key2 = [NSDate dateWithYear:year month:month day:1]; // Always successful
Recent version of XCode does not generate the always-included .pch
file so each header and implementation file should explicitly declare the full list of the necessary include files.
DTConstants.h refers to NSInteger but does not include any system framework (e.g. #import <Foundation/Foundation.h>
). If DateTools.h
is included to the user code before any other includes than this generates a compile time error as follows:
In file included from
DateTools/DateTools.h:23:
DateTools/DTConstants.h:24:14: error: unknown type name 'NSInteger'
static const NSInteger SECONDS_IN_MONTH_28 = 2419200;
^
There doesn't seem to be any translations included for the localized strings used in DTError.m:
NSLocalizedString(@"Operation was unsuccessful.", nil)
NSLocalizedString(@"Please try again by inserting a DTTimePeriod object.", nil)
This throws "Missing key" project warnings on build.
The translation file seems to be missing:
e.g. in dutch:
"1 hour ago" = "1 uur geleden"
I am trying to implement DateTools into a rubymotion project that I am working on. I added DateTools to the project last night and installed it and everything was great. This morning I added another cocoapod, installed that, and now I can't compile my app because I'm getting the following error. I deleted DateTools from the project and I still get the same error on compile.
2015-10-07 09:00:06.403 plutil[25244:5881404] CFPropertyListCreateFromXMLData(): Old-style plist parser: missing semicolon in dictionary on line 5. Parsing will be abandoned. Break on _CFPropertyListMissingSemicolon to debug.
vendor/Pods/Resources/DateTools.bundle/hr.lproj/DateTools.strings: Property List error: Unexpected character / at line 1 / JSON error: JSON text did not start with array or object and option to allow fragments not set.
rake aborted!
Any thoughts? Am I just missing something silly because I'm a beginner? This just started happening all of a sudden and I can't figure out any way to trouble shoot it.
UPDATE!
I was able to get things back to normal by running rake clean
which wiped out whatever cocoapod cache was lying around, but I still had DateTools uninstalled. I thought maybe it just got messed up in the initial install or something? So, I reinstalled it and got the same error. Had to uninstall and clean and now I'm working but no DateTools. :(
Xcode outputs warnings galore for DateTools when compiling with the iOS 8 base sdk. Any update on when fixes will be made and pushed?
Thanks, DateTools is an awesome project! ๐
Calling timeAgo multiple times (over 1000) in a row is a bit slow because of a repeated call to [NSLocale preferredLanguages].
The method getLocaleFormatUnderscoresWithValue: calls the preferred languages method.
Is there any way we could cache that array instead of calling preferredLanguages.objectAtIndex[0] every time timeAgo() is called?
Thanks!
I've added DateTools to my app using cocoapods. My app supports only English language and there are no more localizations specified in project info. When I use Time Ago on a phone (or simulator) with English localization, I see exactly what I expect to see. But when launched on phone with Russian language selected I see underscores in time. Something like "2 _days ago". And I have no idea how to remove them. Please someone tell me. And I think it should be also added to readme file.
I am about to do a full port of DateTools to Swift. Before getting started, I want to open this thread for suggestions on ways to utilize the Swift language in new and unique ways to make DateTools even easier to use.
I ge the issue of the nscalendaridentifiergeorgian unavailable and cannot get 1.4.2 version
for DateTools, I do think either clearly specify that even if cocoapods is used, we still need to drag the bundle over, or have this automatically solved...
DateTools/NSDate+DateTools.m:50:40: 'NSCalendarIdentifierGregorian' is unavailable: not available on iOS
Xcode 5.1.1
SDK : 7.1
Deployment Target: 5.0
Related issue - CocoaPods/CocoaPods#3226
https://github.com/MatthewYork/DateTools/blob/master/DateTools/NSDate%2BDateTools.h#L23-26
Should use [NSBundle bundleForClass:] to determinate correct resources location path
see the section "Note on Performance". It says using .
notation would incur a performance hit since NSCalendar
would be allocated every time. However, in the source code it is allocated statically via setDefaultCalendarIdentifier:
. Is it a documentation issue or am I missing something?
Actually, shortTimeAgoSinceDate of 710 days ago and 365 days ago have both the same output 1y (ago). It is possible to add more detail in the output ? Ex:
Thank you.
Crashes on the following line
NSLocalizedStringFromTableInBundle(key, @"DateTools", [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DateTools.bundle"]], nil)
The new cocoapods now uses swift modules and embedded frameworks, so DateTools.bundle is no longer at its old location, thus causing a crash due to nil being returned.
is there any chance of getting a mention of / nod to
Pretty Timestamp, which a large chunk of the time ago methods are based on?
As @MatthewYork has submitted pull requests to me before, it's quite clear where the influence came from and that the majority of it has actually been copy-pasted.
Some changes that remove warnings from the code were merged in (Thanks!!! :)...now could we also rev the cocoapod version to pick up those changes?
Thanks in advance
I'm developing a Timer app and I need display something like "It will run in 5 minutes".
In android I can use DateUtils.getRelativeTimeSpanString and it works fine. But in iOS I cannot find any solution.
Actually I can write my own codes, but the most difficulty is localization.
"Time Ago" is perfect for many(maybe most) projects, but it's not enough for me...
Btw I can contribute Chinese localization.
I'm looking for a way to create a date in a specific time zone (not necessarily the current time zone). What I found was this method:
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;
However, this method (and the one including seconds etc.) uses the implicit calendar, for which there's no way to set a certain time zone from the outside. Only the methods requiring a date string (starting mostly with formattedDateWithFormat:
) allow you to specify a time zone that will be used by the date formatter for getting the date. It would be very helpful to have something like this:
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day timeZone:(NSTimeZone *)timeZone;
Are there any plans on supporting this or am I overlooking something?
I found that the timeAgoSinceDate method does not return the correct day. For [yesterday22h timeAgoSinceDate:tomorrow1h] I expected something like "the day before yesterday", but instead "yesterday" was returned, which is wrong.
I use German localization, so the actual example looks like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* yesterday2h = [dateFormat dateFromString:@"2000-01-31 02:00:00"];
NSDate* tomorrow1h = [dateFormat dateFromString:@"2000-02-02 01:00:00"];
XCTAssertNotEqualObjects([yesterday2h timeAgoSinceDate:tomorrow1h], @"Gestern");
I use DateTools 1.4.3.
In the best spirit of DateTools, it would be nice to have helper methods for date parsing too.
I'm sure it not really a bug - but something with my code but I have no more leads...
This crash happens on the field - so all I have is a crash log.
I'm passing an NSDate which is taken from passing a "seconds since epoch" through
"dateWithTimeIntervalSince1970"
The crash:
Terminating app due to an uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument'
0 CoreFoundation 0x2f954f83 __exceptionPreprocess + 131
1 libobjc.A.dylib 0x3a105ccf objc_exception_throw + 39
2 CoreFoundation 0x2f954ec5 -[NSException initWithCoder:] + 1
3 Foundation 0x3027213f -[NSPlaceholderString initWithFormat:locale:arguments:] + 95
4 Foundation 0x30272069 +[NSString stringWithFormat:] + 61
5 Cookila copy 0x000cdeff -[NSDate(DateTools) logicLocalizedStringFromFormat:withValue:] (NSDate+DateTools.m:224)
6 Cookila copy 0x000cdd05 -[NSDate(DateTools) shortTimeAgoSinceDate:] (NSDate+DateTools.m:207)
7 Cookila copy 0x000cd1c7 -[NSDate(DateTools) shortTimeAgoSinceNow] (NSDate+DateTools.m:97)
On my device (both device and simulator) parsing the user's data - everything works...
Any leads would be appreciated...
Hi,
I saw that you require a minimum of ios 6.1. My project usually supports 6.0 and up. Is there anything in your lib that prevents you to support 6.0?
The localization bundle doesn't seem to respect the device used language. I temporary fixed this by including the DateTools.bundle directly in my main project.
Any ways to fix this directly in the pod?
I have an NSDate object that is set to February 16, 2015 which is a Monday. When I use isWeekend() on it, it returns true. This is a swift app using a bridging header.
//Monday, Presidents Day
println(date)
2015-02-16 00:00:00 +0000
println(date.isWeekend())
true
Hello,
while trying to compare dates I get the following results (is slot in range startDate/endDate?)
NO - slot/startDate/endDate:2015-02-14 03:30:00 +0000/2015-02-14 03:30:00 +0000/2015-02-14 23:30:00 +0000
timeInterval slot/startDate/endDate : 1423884600.000000/1423884600.570670/1423956600.570670
Comparison is done with this code
[slotDate isLaterThanOrEqualTo:startDate] && [slotDate isEarlierThanOrEqualTo:endDate]
Please correct
great library, and great selection of utilities in here but must throw it out there that i find it a little strange that i'm not seeing a concept of 'tomorrow' in there.
:-)
Hello,
Thanks for this great library. I have started using it as soon as I discovered.
I was maintaining a project and making sure that I am reusing formatter instances after reading this article of Matt Thompson.
I'll try to apply apply this methodology to this library and I'd love to send a pull request if you are interested. I just wanted to open a discussion because I want to hear thoughts of other developers on this issue.
Currently we have two methods: daysFrom
and isToday
. isToday
is very usefull when comparing with today. However daysFrom
is not that usefull when checking if two dates is in the same day.
Can we add a isSameDay
method?
Thanks.
Hi, I might be wrong because Im pretty new to objc, but in NSDate+DateTools.m in the method getLocaleFormatUnderscoresWithValue there are two NSString compared using isEqual.
// Russian (ru) and Ukrainian (uk)
if([localeCode isEqual:@"ru"] || [localeCode isEqual:@"uk"]) {
should be replaced with
// Russian (ru) and Ukrainian (uk)
if([localeCode isEqualToString:@"ru"] || [localeCode isEqualToString:@"uk"]) {
Cheers, Luca
There's a bug in DateTools/DateTools/NSDate+DateTools.m where there's a '*' instead of a '%' when determining whether a year is a leap year.
I am using this library into tableview. When data loaded from server and displayed in table at that time when ther is no big time diffrence (i.e 4-5 s) it first shows that time difference but after scrolling table and coming back to that cell time diefference will decrease instead of increasing . And this will continue up to 0 s and then time difference will increase and will work fine.
Here How I am using this library :
lblTime.text = [dateFromServer shortTimeAgoSinceNow];
Here is one sample date format : 2014-09-30 07:38:03 +0000
Is it a joke? Can I safely use formatting methods in production?
Hey,
I love Datetools, it's solving so many issues for me and it seems pretty solid - congrats Matt.
I need to be able to find the void areas in a collection (i.e. areas where there are no timespans) and looking for solutions to this problem.
I've been pondering on the solution and I think it would probably require a collection to be merged together to form a chain, from there it would be easily enough to invert the chain to produce a group of timespans of void areas.
Is there any easy solution to merge a collection of timespans together?
Cheers
Chris
Support for 16 corners, 96 hours and 4-simultaneous 24-hour Days within a single rotation of Earth
In my application only the time part of a date matters. Is it possible to compare dates only considering the time? Currently I am doing this by calculating minutes from the time part. This produces a lot of boilerplate code.
Instead of
[date dateByAddingYears:1];
I would like to do
[date setHour:1];
I checked localized strings and could not find any localization for shortened time strings. I want to localize short time strings as well. How can I achieve this?
At the moment DateTools crashes when installed through version of cocoapods 0.36+
The main change is that now the resources bundle are not included into the main one, but they are inside the framework. You can find more information here: http://blog.cocoapods.org/CocoaPods-0.36/
Caveats
One caveat about bundling resources is, that until now we had to embed all resources into the application bundle. The resources were referenced programmatically by [NSBundle mainBundle]. Pod authors were able to use mainBundle referencing to include resources the Pod brought into the app bundle. But with frameworks, you have to make sure that you reference them more specifically by getting a reference to your framework's bundle e.g.
Objective-C
[NSBundle bundleForClass:<#ClassFromPodspec#>]
Swift
NSBundle(forClass: <#ClassFromPodspec#>)
For DateTools the problem is in this macro:
NSLocalizedStringFromTableInBundle(key, @"DateTools", [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DateTools.bundle"]], nil)
Which can't find the bundle and returns nil
causing the crash.
A possible solution, even if probably not the more elegant is to replace the macro with:
NSLocalizedStringFromTableInBundle(key, @"DateTools", [NSBundle bundleForClass:NSClassFromString(@"DTTimePeriod")], nil)
Is there a way to compare two time discarding the date component?
Ie: 15:00 is earlier than 17:00.
(I could create two NSDate of the same day with these two hours but it's the best way to use DateTools?)
In DTTimePeriodGroup.h you define two properties and their accessors called StartDate and EndDate. This is not following Apple's recommendation of using camel case starting with a lowercase letter. If I were you I'd change those to startDate and endDate
My iOS project has an OS X target, used for data generation. I recently migrated to DateTools, but I can not use it from cocoa pods with a mentioned target, because .podspec declares that DateTools are iOS only. Can you add OS X as platform to .podspec? Or is it somehow incompatible with OS X? My idea was that NSDate / date / time operations should be relevant for both platforms. Thank you.
I encountered a requirement that I need to know the days between two date. For example
2015-09-15 10:44:51
2015-09-20 10:00:00
The daysFrom:
method will return 4. Actually it's 4 days 23 hours 15 minutes and 8 seconds. I want it return 5 days, that is the ceil days between the two date.
Could I Add features like this to this repo?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.