Git Product home page Git Product logo

datetools's Introduction

Banner

DateTools

DateTools was written to streamline date and time handling in iOS. Classes and concepts from other languages served as an inspiration for DateTools, especially the DateTime structure and Time Period Library for .NET. Through these classes and others, DateTools removes the boilerplate required to access date components, handles more nuanced date comparisons, and serves as the foundation for entirely new concepts like Time Periods and their collections.

Build Status CocoaPods CocoaPods

Featured In

Yahoo! Livetext My Disney Experience ALDI Guidebook Youtube Music Khan Academy

Donate

bitcoin: 17ZEBFw5peuoUwYaEJeGkpoJwP1htViLUY

Installation

CocoaPods

Swift

pod 'DateToolsSwift'

Objective-C (Legacy)

pod 'DateTools'

Manual Installation

All the classes required for DateTools are located in the DateTools folder in the root of this repository. They are listed below:

Swift (Found in DateToolsSwift/DateTools)

  • Constants.swift
  • Date+Bundle.swift
  • Date+Comparators.swift
  • Date+Components.swift
  • Date+Format.swift
  • Date+Inits.swift
  • Date+Manipulations.swift
  • Date+TimeAgo.swift
  • DateTools.bundle
  • Enums.swift
  • Integer.DateTools.swift
  • TimeChunk.swift
  • TimePeriod.swift
  • TimePeriodChain.swift
  • TimePeriodCollection.swift
  • TimePeriodGroup.swift

Objective-C (Found in DateTools/DateTools)

  • DateTools.h
  • NSDate+DateTools.{h,m}
  • DTConstants.h
  • DTError.{h,m}
  • DTTimePeriod.{h,m}
  • DTTimePeriodGroup.{h,m}
  • DTTimePeriodCollection.{h,m}
  • DTTimePeriodChain.{h,m}

The following bundle is necessary if you would like to support internationalization or would like to use the "Time Ago" functionality. You can add localizations at the Localizations subheading under Info in the Project menu.

  • DateTools.bundle

DateTools.h contains the headers for all the other files. Import this if you want to link to the entire framework.

Table of Contents

DateTools

Full code documentation can be found here

One of the missions of DateTools was to make Date feel more complete. There are many other languages that allow direct access to information about dates from their date classes, but Date (sadly) does not. It safely works only in the Unix time offsets through the timeIntervalSince... methods for building dates and remains calendar agnostic. But that's not always what we want to do. Sometimes, we want to work with dates based on their date components (like year, month, day, etc) at a more abstract level. This is where DateTools comes in.

Time Ago

No date library would be complete without the ability to quickly make an NSString based on how much earlier a date is than now. DateTools has you covered. These "time ago" strings come in a long and short form, with the latter closely resembling Twitter. You can get these strings like so:

let timeAgoDate = 2.days.earlier
print("Time Ago: ", timeAgoDate.timeAgoSinceNow)
print("Time Ago: ", timeAgoDate.shortTimeAgoSinceNow)

//Output:
//Time Ago: 2 days ago
//Time Ago: 2d

Assuming you have added the localization to your project, DateTools currently supports the following languages:

  • ar (Arabic)
  • bg (Bulgarian)
  • ca (Catalan)
  • zh_Hans (Chinese Simplified)
  • zh_Hant (Chinese Traditional)
  • cs (Czech)
  • da (Danish)
  • nl (Dutch)
  • en (English)
  • fi (Finnish)
  • fr (French)
  • de (German)
  • gre (Greek)
  • gu (Gujarati)
  • he (Hebrew)
  • hi (Hindi)
  • hu (Hungarian)
  • is (Icelandic)
  • id (Indonesian)
  • it (Italian)
  • ja (Japanese)
  • ko (Korean)
  • lv (Latvian)
  • ms (Malay)
  • nb (Norwegian)
  • pl (Polish)
  • pt (Portuguese)
  • ro (Romanian)
  • ru (Russian)
  • sl (Slovenian)
  • es (Spanish)
  • sv (Swedish)
  • th (Thai)
  • tr (Turkish)
  • uk (Ukrainian)
  • vi (Vietnamese)
  • cy (Welsh)
  • hr (Croatian)

If you know a language not listed here, please consider submitting a translation. Localization codes by language.

This project is user driven (by people like you). Pull requests close faster than issues (merged or rejected).

Thanks to Kevin Lawler for his work on NSDate+TimeAgo, which has been officially merged into this library.

Date Components

There is a lot of boilerplate associated with getting date components from an Date. You have to set up a calendar, use the desired flags for the components you want, and finally extract them out of the calendar.

With DateTools, this:

//Create calendar
let calendar = Calendar(identifier: .gregorian)
let dateComponents = calendar.dateComponents(Set<Calendar.Component>([.month,.year]), from: Date())
        
//Get components
let year = dateComponents.year!
let month = dateComponents.month!

...becomes this:

let year = Date().year
let month = Date().month

Date Editing

The date editing methods in DateTools makes it easy to shift a date earlier or later by adding and subtracting date components. For instance, if you would like a date that is 1 year later from a given date, simply call the method dateByAddingYears.

With DateTools, this:

//Create calendar
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:[NSDate defaultCalendar]];
NSDateComponents *components = [[NSDateComponents alloc] init];

//Make changes
[components setYear:1];

//Get new date with updated year
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];

...becomes this:

NSDate *newDate = [date dateByAddingYears:1];

Subtraction of date components is also fully supported through the dateBySubtractingYears

Date Comparison

Another mission of the DateTools category is to greatly increase the flexibility of date comparisons. Date gives you four basic methods:

  • isEqualToDate:
  • earlierDate:
  • laterDate:
  • compare:

earlierDate: and laterDate: are great, but it would be nice to have a boolean response to help when building logic in code; to easily ask "is this date earlier than that one?". DateTools has a set of proxy methods that do just that as well as a few other methods for extended flexibility. The new methods are:

  • isEarlierThan
  • isEarlierThanOrEqualTo
  • isLaterThan
  • isLaterThanOrEqualTo

These methods are great for comparing dates in a boolean fashion, but what if we want to compare the dates and return some meaningful information about how far they are apart? Date comes with two methods timeIntervalSinceDate: and timeIntervalSinceNow which gives you a double offset representing the number of seconds between the two dates. This is great and all, but there are times when one wants to know how many years or days are between two dates. For this, DateTools goes back to the ever-trusty NSCalendar and abstracts out all the necessary code for you.

With Date Tools, this:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:[NSDate defaultCalendar]];
NSDate *earliest = [firstDate earlierDate:secondDate];
NSDate *latest = (secondDate == firstDate) ? secondDate : firstDate;
NSInteger multiplier = (secondDate == firstDate) ? -1 : 1;
NSDateComponents *components = [calendar components:allCalendarUnitFlags fromDate:earliest toDate:latest options:0];
NSInteger yearsApart = multiplier*(components.month + 12*components.year);

..becomes this:

NSInteger yearsApart = [firstDate yearsFrom:secondDate];

Methods for comparison in this category include:

  • yearsFrom:, yearsUntil, yearsAgo, yearsEarlierThan:, yearsLaterThan:
  • monthsFrom:, monthsUntil, monthsAgo, monthsEarlierThan:, monthsLaterThan:
  • weeksFrom:, weeksUntil, weeksAgo, weeksEarlierThan:, weeksLaterThan:
  • daysFrom:, daysUntil, daysAgo, daysEarlierThan:, daysLaterThan:
  • hoursFrom:, hoursUntil, hoursAgo, hoursEarlierThan:, hoursLaterThan:
  • minutesFrom:, minutesUntil, minutesAgo, minutesEarlierThan:, minutesLaterThan:
  • secondsFrom:, secondsUntil, secondsAgo, secondsEarlierThan:, secondsLaterThan:

Formatted Date Strings

Just for kicks, DateTools has a few convenience methods for quickly creating strings from dates. Those two methods are formattedDateWithStyle: and formattedDateWithFormat:. The current locale is used unless otherwise specified by additional method parameters. Again, just for kicks, really.

Time Periods

Dates are important, but the real world is a little less discrete than that. Life is made up of spans of time, like an afternoon appointment or a weeklong vacation. In DateTools, time periods are represented by the TimePeriod class and come with a suite of initializaiton, manipulation, and comparison methods to make working with them a breeze.

Initialization

Time peroids consist of an Date start date and end date. To initialize a time period, call the init function.

DTTimePeriod *timePeriod = [[DTTimePeriod alloc] initWithStartDate:startDate endDate:endDate];

or, if you would like to create a time period of a known length that starts or ends at a certain time, try out a few other init methods. The method below, for example, creates a time period starting at the current time that is exactly 5 hours long.

DTTimePeriod *timePeriod = [DTTimePeriod timePeriodWithSize:DTTimePeriodSizeHour amount:5 startingAt:[NSDate date]];

Time Period Info

A host of methods have been extended to give information about an instance of TimePeriod. A few are listed below

  • hasStartDate - Returns true if the period has a start date
  • hasEndDate - Returns true if the period has an end date
  • isMoment - Returns true if the period has the same start and end date
  • durationIn.... - Returns the length of the time period in the requested units

Manipulation

Time periods may also be manipulated. They may be shifted earlier or later as well as expanded and contracted.

Shifting

When a time period is shifted, the start dates and end dates are both moved earlier or later by the amounts requested. To shift a time period earlier, call shiftEarlierWithSize:amount: and to shift it later, call shiftLaterWithSize:amount:. The amount field serves as a multipler, just like in the above initializaion method.

Lengthening/Shortening

When a time periods is lengthened or shortened, it does so anchoring one date of the time period and then changing the other one. There is also an option to anchor the centerpoint of the time period, changing both the start and end dates.

An example of lengthening a time period is shown below:

DTTimePeriod *timePeriod  = [DTTimePeriod timePeriodWithSize:DTTimePeriodSizeMinute endingAt:[NSDate date]];
[timePeriod lengthenWithAnchorDate:DTTimePeriodAnchorEnd size:DTTimePeriodSizeMinute amount:1];

This doubles a time period of duration 1 minute to duration 2 minutes. The end date of "now" is retained and only the start date is shifted 1 minute earlier.

Relationships

There may come a need, say when you are making a scheduling app, when it might be good to know how two time periods relate to one another. Are they the same? Is one inside of another? All these questions may be asked using the relationship methods of TimePeriod.

Below is a chart of all the possible relationships between two time periods: TimePeriods

A suite of methods have been extended to check for the basic relationships. They are listed below:

  • isEqualToPeriod:
  • isInside:
  • contains:
  • overlapsWith:
  • intersects:

You can also check for the official relationship (like those shown in the chart) with the following method:

-(DTTimePeriodRelation)relationToPeriod:(DTTimePeriod *)period;

All of the possible relationships have been enumerated in the TimePeriodRelation enum.

For a better grasp on how time periods relate to one another, check out the "Time Periods" tab in the example application. Here you can slide a few time periods around and watch their relationships change.

TimePeriods

Time Period Groups

Time period groups are the final abstraction of date and time in DateTools. Here, time periods are gathered and organized into something useful. There are two main types of time period groups, TimePeriodCollection and TimePeriodChain. At a high level, think about a collection as a loose group where overlaps may occur and a chain a more linear, tight group where overlaps are not allowed.

Both collections and chains operate like an NSArray. You may add,insert and remove TimePeriod objects from them just as you would objects in an array. The difference is how these periods are handled under the hood.

Time Period Collections

Time period collections serve as loose sets of time periods. They are unorganized unless you decide to sort them, and have their own characteristics like a StartDate and EndDate that are extrapolated from the time periods within. Time period collections allow overlaps within their set of time periods.

TimePeriodCollections

To make a new collection, call the class method like so:

//Create collection
DTTimePeriodCollection *collection = [DTTimePeriodCollection collection];

//Create a few time periods
 DTTimePeriod *firstPeriod = [DTTimePeriod timePeriodWithStartDate:[dateFormatter dateFromString:@"2014 11 05 18:15:12.000"] endDate:[dateFormatter dateFromString:@"2015 11 05 18:15:12.000"]];
    DTTimePeriod *secondPeriod = [DTTimePeriod timePeriodWithStartDate:[dateFormatter dateFromString:@"2015 11 05 18:15:12.000"] endDate:[dateFormatter dateFromString:@"2016 11 05 18:15:12.000"]];

//Add time periods to the colleciton
[collection addTimePeriod:firstPeriod];
[collection addTimePeriod:secondPeriod];

//Retreive collection items
DTTimePeriod *firstPeriod = collection[0];

Sorting Sorting time periods in a collection is easy, just call one of the sort methods. There are a total of three sort options, listed below:

  • Start Date - sortByStartAscending, sortByStartDescending
  • End Date - sortByEndAscending, sortByEndDescending
  • Time Period Duration - sortByDurationAscending, sortByDurationDescending

Operations It is also possible to check an Date's or TimePeriod's relationship to the collection. For instance, if you would like to see all the time periods that intersect with a certain date, you can call the periodsIntersectedByDate: method. The result is a new TimePeriodCollection with all those periods that intersect the provided date. There are a host of other methods to try out as well, including a full equality check between two collections.

TimePeriodCollectionOperations

Time Period Chains

Time period chains serve as a tightly coupled set of time periods. They are always organized by start and end date, and have their own characteristics like a StartDate and EndDate that are extrapolated from the time periods within. Time period chains do not allow overlaps within their set of time periods. This type of group is ideal for modeling schedules like sequential meetings or appointments.

TimePeriodChains

To make a new chain, call the class method like so:

//Create chain
DTTimePeriodChain *chain = [DTTimePeriodChain chain];

//Create a few time periods
 DTTimePeriod *firstPeriod = [DTTimePeriod timePeriodWithStartDate:[dateFormatter dateFromString:@"2014 11 05 18:15:12.000"] endDate:[dateFormatter dateFromString:@"2015 11 05 18:15:12.000"]];
DTTimePeriod *secondPeriod = [DTTimePeriod timePeriodWithStartDate:[dateFormatter dateFromString:@"2015 11 05 18:15:12.000"] endDate:[dateFormatter dateFromString:@"2016 11 05 18:15:12.000"]];

//Add test periods
[chain addTimePeriod:firstPeriod];
[chain addTimePeriod:secondPeriod];

//Retreive chain items
DTTimePeriod *firstPeriod = chain[0];

Any time a date is added to the time chain, it retains its duration, but is modified to have its StartDate be the same as the latest period in the chain's EndDate. This helps keep the tightly coupled structure of the chain's time periods. Inserts (besides those at index 0) shift dates after insertion index by the duration of the new time period while leaving those at indexes before untouched. Insertions at index 0 shift the start date of the collection by the duration of the new time period. A full list of operations can be seen below.

Operations Like collections, chains have an equality check and the ability to be shifted earlier and later. Here is a short list of other operations.

TimePeriodChainOperations

Documentation

All methods and variables have been documented and are available for option+click inspection, just like the SDK classes. This includes an explanation of the methods as well as what their input and output parameters are for. Please raise an issue if you ever feel documentation is confusing or misleading and we will get it fixed up!

Unit Tests

Unit tests were performed on all the major classes in the library for quality assurance. You can find theses under the "Tests" folder at the top of the library. There are over 300 test cases in all!

If you ever find a test case that is incomplete, please open an issue so we can get it fixed.

Continuous integration testing is performed by Travis CI: Build Status

Credits

Many thanks to Grayson Webster for helping rethink DateTools for Swift and crank out the necessary code!

Thanks to Kevin Lawler for his initial work on NSDate+TimeAgo. It laid the foundation for DateTools' timeAgo methods. You can find this great project here.

Many thanks to the .NET team for their DateTime class and a major thank you to Jani Giannoudis for his work on ITimePeriod.

Images were first published through itenso.com through Code Project

I would also like to thank God through whom all things live and move and have their being. Acts 17:28

License

The MIT License (MIT)

Copyright (c) 2014 Matthew York

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

datetools's People

Contributors

albinekcom avatar bendihossan avatar danthorpe avatar dezinezync avatar divergio avatar edpelesh avatar fahlout avatar faizmokh avatar greysonwright avatar hlfcoding avatar ikhsan avatar itsmebhavin avatar jlubeck avatar kevinlawler avatar m1kachu avatar matthewyork avatar mjanda avatar morizotter avatar mortonfox avatar ndbroadbent avatar pikdays avatar piotrzbyszynski avatar raphaelcruzeiro avatar readmecritic avatar rmannion avatar romainbiard avatar sendoa avatar tbaranes avatar tiberius33 avatar zhukn1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

datetools's Issues

More detailed localization description needed to be added to the readme file

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.

Short Time displaying wrong in very short interval

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

Want to add floor/ceil days feature.

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?

Thread Safety

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.

Reusing formatter instances

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.

Minimum ios 6.1

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?

Finding void periods in a collection

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

No translations for localized strings in DTError

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.

Can't integrate v1.2.0 with CocoaPods

Hey,
I'm having some issues with v1.2.0 and integrating into a cocoapods project. I cleaned the project, cleared derived data, and tried installing multiple times but the only files that install are these:
screen shot 2014-05-07 at 7 00 07 pm

Can we add a `isSameDay` method?

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.

timeAgo cause app crash

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...

Property List error: Unexpected character at line 1 (JSON error)

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. :(

dateWithString:format: returning nil

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

Support Cocoapods 0.36

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)

Crash on shortTimeAgoSinceNow

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...

OS X platform in .podspec

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.

Swift Support

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.

Start of week

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

Crash when using with cocoapods

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.

timeAgo() 1000k+ times is slightly slow due to [NSLocale preferredLanguages].

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!

Create dates in a certain time zone

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?

isWeekend() returning true on a monday

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

Comparing difference in "calendar days"

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?

[Feature request] Morre details for shortTimeAgoSinceDate

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:

  • 1y11m (ago)
  • 11m3w (ago)
  • 3w6d (ago)
  • 6d23h (ago)
  • 23h59m (ago)
  • 59m59s (ago)

Thank you.

Excellent library, butโ€ฆ

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.

Compare two times?

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?)

Localization not working using Cocoapods Frameworks

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?

Comparing date fails (due to NSTimeInterval - a floating point)

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

Any plan to support "Time Later" beside "Time Ago"?

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.

Compiler error "unknown type name NSInteger"

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;
             ^

No localization for short time strings?

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?

tomorrow?

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.

:-)

isEqual instead of isEqualToString? (Russian and Ukrainian locales)

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

Update cocoapod for latest code

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

timeAgoSinceDate returns "yesterday" when it should be "day before yesterday"

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.

StartDate / EndDate

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.