"iOS Open Source Code" (3):D Atetools

Source: Internet
Author: User

First, Introduction

Datetools is a o-c library for working with dates and times. It consists of 3 sub-Libraries:

Nsdate+datetools, Time Periods, time Periods Group.

1, Nsdate+datetools

Do not know whether you have this feeling, O-c provided nsdate seems to be different from other languages of the date class, it is more like the timeintervalsince ... Method constructs the Unix time, plus a calendar feature. But sometimes we prefer to access dates by the date component "year, month, and Day". This is what nsdate+datetools to provide for us. It mainly provides a few functions.

1) Time ago

This part is actually a Nsdate+timeago library written by Kevin Lawler, which has now been formally incorporated into the Datetools library.

As the name implies, time ago is the conversion of dates into relative dates, the way we used to say "Yesterday, today, tomorrow, a few days ago, a week later ...". It has a "long, short" format, which is a shorter description of the former. For example:

NSDate *timeagodate = [nsdatedatewithtimeintervalsincenow:-4];

NSLog (@ "Time Ago:%@", Timeagodate.timeagosincenow);

NSLog (@ "Time Ago:%@", Timeagodate.shorttimeagosincenow);

The above code is output separately:

Time Ago:4 seconds Ago

Time ago:4s

Of course, Datetools offers support in up to 33 languages, including, of course, simplified/Traditional Chinese.

2) Access date component

Previously, we had to read a nsdate date, which must be done:

Create Calendar

Nscalendar *calendar =[[nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar];

Unitflags = Nsyearcalendarunit | Nsmonthcalendarunit;

nsdatecomponents *datecomponents= [Calendar components:unitflags fromdate:date];

Get components

Nsinteger year =datecomponents.year;

Nsinteger month =datecomponents.month;

However, using Datetools, you only need to:

Nsinteger year = date.year;

Nsinteger month = Date.month;

If you are using a non-Georgian calendar, just do this:

Nsinteger day = [Datedaywithcalendar:calendar];

You can also modify the Defaultcalendar method in the NSDATE+DATETOOLS.M file by modifying the entire Datetools default calendar from the Georgian calendar to others.

3) operation of the date

In the past, to add/subtract one year from an existing date to form a new date, we must:

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 = [calendardatebyaddingcomponents:components todate:date options:0];

Using Datetools, you can use:

NSDate *newdate = [datedatebyaddingyears:1];

If it is minus one year, you can use the Datebysubtractingyears method.

4) Comparison operation

There are 4 basic comparison operations for NSDate:

Isequaltodate:

Earlierdate:

Laterdate:

Compare

The datetools then changed them to:

Isearlierthan

Isearlierthanorequalto

Islaterthan

Islaterthanorequalto

In addition to this, Datetools also offers additional operations:

· 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:

In this way, you can easily know how many years/months/days the two dates differ:

Nsinteger Yearsapart =[firstdate Yearsfrom:seconddate];

5) Format Date string

Datetools simplifies the operation of formatting date strings, namely Formatteddatewithstyle: and Formatteddatewithformat: Methods.

2. Period

The period is the time period, including a continuous period of time, datetools using Dttimeperiod to represent. The Dttimeperiod class includes a series of initialization methods, as well as operations and comparison operations on the time period.

1) Create

There are two ways to create a time period (initialization method).

The period includes a start time and an end time. This is also reflected in its initialization method:

Dttimeperiod *timeperiod =[[dttimeperiod alloc] initwithstartdate:startdate enddate:enddate];

Of course, you can also not specify the end time, the end time can be obtained by the start time and the length of the window. For example, the following period is described by the start time and length of 5 hours:

Dttimeperiod *timeperiod =[dttimeperiod timeperiodwithsize:dttimeperiodsizehour amount:5startingAt:[NSDate Date]];

2) Properties of the period

· Hasstartdate-Returns Yes if the period has a start time

· Hasenddate-Returns Yes if the period has an end time

· Ismoment-Returns Yes if the start time of the period equals the end time

· Durationin.-Returns the length in the specified unit.

3) operation

3 operations are supported during the period: move, increase the duration, and reduce the length of time.

A move is an action that takes a period start time forward or deferred. Use method Shiftearlierwithsize:amount: and Shiftlaterwithsize:amount: proceed.

The increase or decrease in length can be divided into two situations. One is a fixed start time, then increases/decreases the length of time, which eventually changes the end time, one is the fixed end time, then increases/decreases the length, and eventually changes the start time. For example:

Dttimeperiod *timeperiod = [Dttimeperiodtimeperiodwithsize:dttimeperiodsizeminute endingAt:[NSDate Date]];

[Timeperiodlengthenwithanchordate:dttimeperiodanchorend size:dttimeperiodsizeminuteamount:1];

The code above increases the duration from 1 minutes to 2 minutes, fixed end time, so the start time is 1 minutes ahead.

4) Relational operations

For two time periods, we often consider the relationship between the two: Are they the same? is one within the range of the other?

The DTTIMEPERIOD supports 5 basic relational operations:

Isequaltoperiod: Equal

Isinside: Included

Contains: Contains

Overlapswith: Overlapping, partly identical

Intersects: Intersect, add two more cases on the basis of overlap: the former end time = The latter start time, the former start time = the latter end time.

3. Period Group

The time-of-day collection, consisting of two classes: Dttimeperiodcollection and Dttimeperiodchain. The biggest difference is that the latter does not allow overlapping periods in the collection.

"iOS Open Source Code" (3):D Atetools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.