GithubHelp home page GithubHelp logo

kevinlawler / nsdate-timeago Goto Github PK

View Code? Open in Web Editor NEW
1.8K 53.0 322.0 177 KB

A "time ago", "time since", "relative date", or "fuzzy date" category for NSDate and iOS, Objective-C, Cocoa Touch, iPhone, iPad

License: ISC License

Swift 40.11% Objective-C 56.90% Ruby 2.99%

nsdate-timeago's Introduction

Carthage compatible

Migration 2014.04.12

NSDate+TimeAgo has merged with DateTools. DateTools is the parent project and Matthew York is the project head.

This project is now part of DateTools. Please visit https://github.com/MatthewYork/DateTools

This repo is here in archive form. New work should be contributed to DateTools.

2016.01.04. To reiterate, all issues and contributions should go to DateTools, there is zero promise anything here will be looked at. Kevin's attention is on Kerf.

Description

This is an iOS, Objective-C, Cocoa Touch, iPhone, iPad category for NSDate. It gives NSDate the ability to report times like "A moment ago", "30 seconds ago", "5 minutes ago", "Yesterday", "Last month", "2 years ago", and so on.

This functionality has variously been referred to as a "time ago", "time since", "relative date", or "fuzzy date" feature.

NSDate+TimeAgo currently supports the following languages:

  • en (English)
  • ee (Estonian)
  • es (Spanish)
  • zh_Hans (Chinese Simplified)
  • zh_Hant (Chinese Traditional)
  • pt (Portuguese)
  • fr (French)
  • it (Italian)
  • ru (Russian)
  • de (German)
  • nl (Dutch)
  • hu (Hungarian)
  • fi (Finnish)
  • ja (Japanese)
  • vi (Vietnamese)
  • ro (Romanian)
  • da (Danish)
  • cs (Czech)
  • nb (Norwegian)
  • lv (Latvian)
  • tr (Turkish)
  • ko (Korean)
  • bg (Bulgarian)
  • he (Hebrew)
  • ar (Arabic)
  • gre (Greek)
  • pl (Polish)
  • sv (Swedish)
  • th (Thai)
  • uk (Ukrainian)
  • is (Icelandic)
  • sq (Albanian)
  • sk (Slovak)
  • ms (Malay)
  • km (Cambodian)
  • id (Bahasa-Indonesia)
  • mn (Mongolian)
  • ur (Urdu)

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

Use

  1. Add the files to your project - manually or via Cocoapods (pod 'NSDate+TimeAgo')
  2. Import the header using #import "NSDate+TimeAgo.h"
  3. Call the timeAgo method in the following way:
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:0];
NSString *ago = [date timeAgo];
NSLog(@"Output is: \"%@\"", ago);
2011-11-12 17:19:25.608 Proj[0:0] Output is: "41 years ago"

2 other methods are available:

  • dateTimeAgo: returns times with only strings of the type: "{value} {unit} ago"
  • dateTimeUntilNow: returns only "yesterday" / "this morning" / "last week" / "this month" -- less precise than dateTimeAgo but more natural

Those three methods can be interchanged as they have the same signature.

Future Directions

Would be nice to

  1. add customization options (e.g., should it report seconds or just "a minute ago")
  2. add string customization
  3. have more localizations
  4. make dateTimeUntilNow more precise: instead of "Last week" use "Last Friday", "Last Monday" etc.
  5. other

License

Released under ISC (similar to 2-clause BSD)

http://wikipedia.org/wiki/ISC_license

Credits

Originally based on code Christopher Pickslay posted to Forrst. Used with permission. http://twitter.com/cpickslay

Ramon Torres began support for internationalization/localization. Added es strings. http://rtorres.me/

Dennis Zhuang added zh_Hans Chinese Simplified strings. http://fnil.net/

Mozart Petter added pt_BR Brazilian Portuguese strings. http://www.mozartpetter.com/

Stéphane Gerardot added fr French strings.

Marco Sanson added it Italian strings. http://marcosanson.tumblr.com/

Almas Adilbek added ru Russian strings. Extended logic to support Russian idioms. http://mixdesign.kz/

Mallox51 added de German strings. https://github.com/Mallox51

Tieme van Veen added nl Dutch strings. http://www.tiemevanveen.nl

Árpád Goretity added hu Hungarian strings. http://apaczai.elte.hu/~13akga/

Anajavi added fi Finnish strings. https://github.com/anajavi

Tonydyb added ja Japanese strings.

Vinhnx added vi Vietnamese strings. http://vinhnx.github.io/

Ronail added zh_Hant Traditional Chinese strings. https://github.com/ronail

SorinAntohi added ro Romanian strings. https://github.com/SorinAntohi

spookd added da Danish strings. https://github.com/spookd

Barrett Jacobsen added cs Czech strings. https://github.com/barrettj

Dmitry Shmidt added nb Norwegian strings. https://github.com/shmidt

Martins Rudens added lv Latvian strings. https://github.com/rudensm

Osman Saral added tr Turkish strings. https://github.com/osrl

analogstyle added ko Korean strings. http://almacreative.net/

Flavio Caetano fixed pt Portuguese strings. http://flaviocaetano.com

kolarski added bg Bulgarian strings. http://github.com/kolarski

Vladimir Kofman added he Hebrew strings. https://github.com/vladimirkofman

Viraf Sarkari added ar Arabic, gre Greek, pl Polish, sv Swedish, and th Thai strings. https://github.com/viraf

Vasyl Skrypii added uk Ukranian strings. https://github.com/medlay

Maggi Trymbill added is Icelandic strings. https://github.com/grundvollur

Erid Bardhaj added sq Albanian strings. https://github.com/eridbardhaj

Joan Biscarri added ca Catalan strings. https://github.com/jbiscarri

Gantulga added mn Mongolian strings. https://github.com/gangstarmn

ali-ehsan added ur Urdu strings. https://github.com/ali-ehsan

Zonily Jame Pesquera added id Bahasa Indonesia strings. https://github.com/kuyazee

Zonily Jame Pesquera added km Khmer strings. https://github.com/kuyazee

nsdate-timeago's People

Contributors

alexandrebarbier avatar alexkunitsa avatar ali9091 avatar anajavi avatar barrettj avatar chencan avatar einsteinx2 avatar eridbardhaj avatar fjcaetano avatar gangstarmn avatar gillesfabio avatar gonzo-oin avatar h2co3 avatar iamariffikri avatar javisoto avatar kbeyer avatar kevinlawler avatar kgn avatar kolarski avatar krzd avatar kubatruhlar avatar maesepptarvo avatar marcosanson avatar mixdesign avatar nurdabolatov avatar raymondjavaxx avatar robos23 avatar rudensm avatar shmidt avatar teameh 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

nsdate-timeago's Issues

let dateFormatter: NSDateFormatter = NSDateFormatter()

let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
if let date = dateFormatter.dateFromString(dateString) {
// I get valid a NSDate object
}

Originally posted by @khakha010 in #90 (comment)

Adding new localized strings.

Hi.

Since I am new to ios development, it might sounds silly but need to ask about it. I'd like to add new localized strings for Korean developers. But I couldn't find solution to add new file to NSDateTimeAgo.bundle. I, of course, need the time-ago method for the current project. Please let me know how to add new locazlied string so that I can make pull request to enrich this fascinating category.

Thanks.

CocoaPods

Not really an issue, more of a suggestion. Have you considered putting it in Cocoapods? It-s a fast way for a lot of us to have access to your library. Thanks...

I cannot import pod to Swift project

I cannot import pod to Swift project for some reason, when trying to bridge it all I get is a "not found" error. What am I doing wrong? Thanks!!

time to

could you plz add time to - functionality
like if iam at 1:30pm ,
i can get the time to 2:00 is 30 min

Addition to ReadMe

I think would be helpful to add in Readme a way to not have localazation and always use the English version. In my project wanted all the features of the category without localization. An easy way to achieve it is to add

define NSDateTimeAgoLocalizedStrings(key) key

in the [PROJECTNAME]-Prefix.pch file
Thanks

Projects including this file are unable to export XLIFFs in Xcode 6.3 and up.

When attempting to export an XLIFF with this class in the project in Xcode 6.2, an error is thrown but the XLIFF export succeeds. As of Xcode 6.3 and higher however, XLIFF export completely fails. I will be working on a solution, but I wanted to raise the issue.

Here is the error from Xcode 6.2.

Bad entry in file Classes/Utilities/Catagories/Time Ago/NSDate+TimeAgo.m 
(line = 15): Argument is not a literal string.

Slovak strings - rename file

The Slovak strings file (in DateTools.bundle - sk.lproj) should be named "DateTools.strings" like in any other lproj. Otherwise the Slovak translations aren't working.

Bahasa Strings

I'll send Bahasa strings in a few days. could you please also support it for version 1.0.6

I use Swift 2.2 btw that's why I need it in that version 👍

Thank you very much for the library btw

Exception warning

There is issue with current implementation using some localisations:
[__NSCFString substringWithRange:]: Range {4294967295, 1} out of bounds; string length 1. This will become an exception for apps linked after 10.9. Warning shown once per app execution.

Problem With Loacalization

Hey guys, I'm having a problem with localization and I think it may be stupid, but I couldn't find anything and it's really bugging me now.

I've already set the project's locale and everything. For instance, the main bundle preferredLocalization is set with my language pt. But when I get the pod's bundle that contains the localized strings, its preferredLocation is en.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"NSDateTimeAgo" ofType:@"bundle"];
    NSBundle *b = [NSBundle bundleWithPath:path];
    NSLog(@"%@", [b localizedStringForKey:@"Just now" value:Nil table:@"NSDateTimeAgo"]);
    // Output: "Just now"

    NSLog(@"%@", b.preferredLocalizations);
    // Output: (en)

    NSLog(@"%@", [[NSBundle mainBundle] preferredLocalizations]);
    // Output: (pt)

Please, if anyone can tell me what I'm doing wrong I'd really appreciate.

Push to Cocoapods Trunk

hi @kevinlawler is there any reason why the latest version isn't pushed/published?
so we can stop doing to get the latest stable version :)

 pod 'NSDate+TimeAgo', :git => 'https://github.com/kevinlawler/NSDate-TimeAgo'

Error in iOS 6.1

Hi - I installed this and used the demo code and received this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument'

I traced it to the method - (NSString *) stringFromFormat:(NSString *)format withValue:(NSInteger)value

Swift 3

I made a swift3 fork & branch. If you make a swift3 branch I can submit a pull request.

How to add New Language ?

Hi,

I am willing to add a new language. I have already cloned the repository. I have seen that language supports are in a bundle named NSDateTimeAgo.bundle. But i have a added a new folder inside the bundle NSDateTimeAgo.bundle. Renamed the folder to bn.lproj, but after that i was unable to add any file inside that newly created directory.

It would be great if you share that how a language support can be added in the NSDateTimeAgo.bundle.

Here i have added a gif which demonstrating that, how i attempted this.

Thanks in Advance.

string capitalization

Hi there,

Thanks for this piece of code, it's very useful.

I'd like to get strings in a non-capitalized way.
If we take some output: "A moment ago", "30 seconds ago", "5 minutes ago", "Yesterday", "Last Month", "2 Years Ago", and so on

some have capitalized words, other not.

I want to create a full sentence out of the output date string like:-
"Somebody posted a document 3 hours ago"

if it's now been one day since the string is now:
"Somebody posted a document Yesterday"

the capital letter 'Y' looks odd in this sentence.

For now I just lowercase everything that comes out of your library, but it's not very good as in some languages (cf. translations for your library) this transformation might change letters within words.
Could you instead output everything in lowercase? If we need capitalization of words/sentences it's then quite easy to do so with NSString.

Hope I made sense. Cheers.

Use of the term "Yesterday" is incorrect

Looking at the code, I see that the library returns the string "yesterday" if the difference in time between the given date and now is more than 24 hours ago but less than 2*24 hours ago.
Saying "yesterday" is wrong because the given date could be actually the day before yesterday.
For instance:

Date: Monday 11am
Now: Wednesday 10am
difference : 1day and 23hours
=> returns "Yesterday", but "Yesterday" here was Tuesday.

I suggest using "calendar days" instead of just plain days (=24 hours) to calculate the difference. Yesterday will have its true meaning then.

For an example on how to do this, see the second answer: http://stackoverflow.com/questions/4739483/number-of-days-between-two-nsdates

By the way, same problem with "last month" / "last year" => months are not always 31 days long, and years are not 365.25 days long.

Time Ahead

Hi,

Is there any way to do the same in future ? like this library in java
Eg 3 hours from now, In 30 minutes, etc

Or may be it is there and i missed it ??

Localization is not working for pt

Hi guys,

I'm using the NSDate-TimeAgo pod and the bundle was added to copy bundle resources on build phases but the translation is not working. Do you know if I need to set anything else?
It would be great if you guys can add the how-to on readme file.

Thanks

[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument issue

Hi there!

I am trying to use NSDate-TimeAgo for the first time. I've reproduced all steps to set up it. But still have an exception

[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument

Debuggingly I conclude that app crashes during execution of this line

return [self stringFromFormat:@"%%d %@years ago" withValue:minutes];

How we can solve it?

Swift timeAgo returns empty string.

I am calling timeAgo from NSDate and it returns an empty string.

if let validDate = lastUpdated{
    let dateText = validDate.timeAgo
    self.updatedLabel.text = dateText
    print(validDate.timeAgo)
}

I tried to debug the Swift extension and found it gets an empty string there.

screen shot 2015-09-28 at 3 16 46 pm

This is how I am creating my NSDate if it is important:

let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
if let date = dateFormatter.dateFromString(dateString) {
    // I get valid a NSDate object
}

Returns null when used through cocoapods

When you use this category as a pod timeAgo method returns null, when adding the category directly to my main project it works fine.

I think it might be because the resources are not added to the mainBundle when they are in cocoapod project, so NSLocalizedStringFromTableInBundle is unable to read translated values

Time Until

By changing a few strings the library supports Time Until too.

E.g. "A minute ago" becomes "In a minute"
or "4 days ago" becomes "In 4 days"

It does mean a few more LocalizedStrings though.

iOS8.0 deprcated

thanks for your good code.
I built using Cocoapod platform :ios, '8.0'.
If I build project , I get 32 warnings.

NSYearCalendarUnit
NSMonthCalendarUnit
NSDayCalendarUnit ... so on
Can you please look at it and make some fixs ?

arm64 Support!

Hello It's not getting compiled using arm64 architecture. Without that apple will not accept new build.

Warnings in ios8

-week has been deprecated in favor of -weekOfYear. -weekOfYear

stringFromFormat throws exception

- (NSString *) stringFromFormat:(NSString *)format withValue:(NSInteger)value { NSString * localeFormat = [NSString stringWithFormat:format, [self getLocaleFormatUnderscoresWithValue:value]]; return [NSString stringWithFormat:NSDateTimeAgoLocalizedStrings(localeFormat), value]; }

the return line gives the fallowing exception:

2014-02-14 23:33:59.347 Pico[12514:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument' *** First throw call stack: ( 0 CoreFoundation 0x027665e4 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x024e98b6 objc_exception_throw + 44 2 CoreFoundation 0x027663bb +[NSException raise:format:] + 139 3 Foundation 0x02105664 -[NSPlaceholderString initWithFormat:locale:arguments:] + 99 4 Foundation 0x0210670d +[NSString stringWithFormat:] + 88 5 Pico 0x001892c6 -[NSDate(TimeAgo) stringFromFormat:withValue:] + 454 6 Pico 0x00186b05 -[NSDate(TimeAgo) timeAgo] + 1941 7 Pico 0x0006cef4 -[MCDWarningsViewController tableView:cellForRowAtIndexPath:] + 2052

this is my console:
(lldb) po localeFormat %d days ago (lldb) p value (NSInteger) $1 = 2

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.