New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Has to be an easier way to deal with Time in Objective C


Post Reply New Thread Subscribe

 
Thread Tools
fajhetti

 
Member Since: May 08, 2009
Posts: 6
fajhetti is on a distinguished road

fajhetti is offline
I am really a .NET developer with some knowledge of x-code and objective C. What I am doing is calling a .NET web service to get a collection of Songs. I get these songs from the .NET web service: Name (String), Duration(TimeSpan), Hours(Int), Minutes(Int), and Seconds(Int). I only added the Hours, Minutes, and Seconds because I could not find any data type in objective C that would mimic the TimeSpan.

Now what I have to do is as they select multiple songs I need to add the Time of each song for a total duration of songs added.

Currently in the Music.h file I am using an NSDateComponent to hold the Hours, Minutes, and Seconds. Now I assume I could pull each piece out and add them up, make the conversion to handle seconds running over a minute, and stuff the total into another NSDateComponent, but there has to be an easier way.

Please help me find that easier way.
Thanks
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09

nabl is offline
I'm not .NET developer (nor even a C# one), but I just read a quick summary of the TimeSpan type, and from my knowledge (and as you found) there is no comparable Cocoa type. With that in mind, I'd say your best bet will be to work with NSTimeIntervals behind the scenes, which are simply doubles that represent a number of seconds. From the TimeSpan summary I read, it looks like there's a TotalSeconds method, so perhaps you could just send that from the Web service and then work with it in your app as an NSTimeInterval.

Then when you're done doing whatever calculations you need to do (or more precisely, when you're ready to display the interval in a more readable form) you can convert it to an NSDateComponents object and use that for display. For example, if you have an array of the selected songs with a -combinedDuration method that returns an NSTimeInterval of the sum of all the songs' durations:
Code:
// Get the total duration of the songs.
NSTimeInterval totalDuration = [selectedSongs combinedDuration];

// Create two dates that are totalDuration apart for use
// in creating an NSDateComponents object.
NSDate *date1 = [NSDate date]; // Now.
NSDate *date2 = [NSDate dateWithTimeInterval:totalDuration 
                                   sinceDate:date1];

// Get the system calendar. If you're positive it will be the 
// Gregorian, you could use the specific method for that.
NSCalendar *currentCalendar = [NSCalendar currentCalendar];

// Specify which date components to get. This will get the hours, 
// minutes, and seconds, but you could do days, months, and so on
// (as I believe iTunes does).
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

// Create an NSDateComponents object from these dates using the system calendar.
NSDateComponents *durationComponents = [currentCalendar components:unitFlags 
                                                          fromDate:date1 
                                                            toDate:date2 
                                                           options:0];

// Format this as desired for display to the user.
NSString *durationString = [NSString stringWithFormat:
      @"%d Hours, %d Minutes, and %d Seconds", 
      [durationComponents hour],
      [durationComponents minute], 
      [durationComponents second]];

// Display it however necessary. For example,
NSLog(@"The total duration of the selected songs is %@.", durationString);
Using this code, if I replace the first line with NSTimeInterval totalDuration = 5843;, I get this output:
Quote:
The total duration of the selected songs is 1 Hours, 37 Minutes, and 23 Seconds.
Of course, you'd want to add some smarts as to whether to pluralize the time units (that is, whether to use "hour" or "hours"), and so on, but this should get you on the right track. Also, it should be significantly easier to work with NSTimeIntervals than NSDateComponents objects.

With assistance from the NSDate and NSDateComponents class references, the Date and Time Programming Guide for Cocoa, and this stackoverflow post.
QUOTE Thanks
fajhetti

 
Member Since: May 08, 2009
Posts: 6
fajhetti is on a distinguished road

fajhetti is offline
Thank you very much for the help. I appreciate the time you put into helping me. I will implement this today.

Thanks again.
QUOTE Thanks

Post Reply New Thread Subscribe


« httpd.conf file wrecked | applescript to bring ichat window to front after message received »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Time machine, space issues. Jonik OS X - Apps and Games 8 01-04-2010 04:46 PM
Time capsule (WLAN only,Compress rate of time machine) jerryhilbert Internet, Networking, and Wireless 1 01-02-2010 11:20 AM
Time Machine full system restore changes your login password- Not Robust darthvader747 OS X - Operating System 5 01-28-2009 02:56 PM
Question on Time Machine back up acortes OS X - Apps and Games 5 12-02-2008 12:19 PM
Problem with Time Machine/Finder not finding remote drive markwco OS X - Operating System 0 04-22-2008 01:18 AM

All times are GMT -4. The time now is 05:24 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?