-4

How i can port this set of code in wp7

-(NSString *) timeSincePosted:(NSString *)ad_date {
if (!ad_date || ([ad_date compare:@""] == NSOrderedSame) || ([ad_date rangeOfString:@"-"].location == NSNotFound))
    return @"";

// date format
//2010-04-22,  5:27PM EDT
NSDateFormatter *dateFormatter_CL = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[dateFormatter_CL setLocale:enUSPOSIXLocale];
[dateFormatter_CL setDateFormat:@"yyyy-MM-dd, h:mma zzz"]; 
NSDate *CL_adDate = [dateFormatter_CL dateFromString: ad_date];

// Time since now
NSTimeInterval deltaT = -[CL_adDate timeIntervalSinceNow];
int deltaMin = deltaT/60;
int deltaHr = deltaMin/60;
int deltaDay = deltaHr/24;

if (deltaMin < 1) return [NSString stringWithString:@"0 mins"];
if (deltaMin == 1) return [NSString stringWithString:@"1 min"];
if (deltaMin < 60) return [NSString stringWithFormat:@"%d mins", deltaMin];
if (deltaHr <= 1) return [NSString stringWithString:@"1 hr"];
if (deltaHr < 24) return [NSString stringWithFormat:@"%d hrs", deltaHr];
if (deltaDay < 2) return [NSString stringWithString:@"1 day ago"];
return [NSString stringWithFormat:@"%d days ago", deltaDay];
}
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
Nitha Paul
  • 1,401
  • 1
  • 14
  • 30
  • What is your target language, and what is your source data? you probably want to look at the DateTime and Timespan types – Rowland Shaw Apr 06 '12 at 09:28
  • My target language is C# and source language is objective c.(windows Phone). I think the code is using for time zone conversion. But i tried many alternatives in wp7 but doesn't find any proper solution. – Nitha Paul Apr 06 '12 at 10:20
  • What about your source data? How far have you got with your conversion? – Rowland Shaw Apr 06 '12 at 10:22
  • The source iput would be a string like this "Date: 2012-04-05, 11:29PM CDT" – Nitha Paul Apr 06 '12 at 10:24

2 Answers2

3

I'm flying by wires a bit here, but I'd assume you want something like:

    /// <summary>
    /// Returns a string approximation of the time since the specified time.
    /// </summary>
    /// <param name="adDate">The ad date.</param>
    /// <returns></returns>
    public static string TimeSincePosted(string adDate)
    {
        return TimeSincePosted(DateTime.ParseExact(adDate, "\D\a\t\e: yyyy-MM-dd, hh:mmtt K", CultureInfo.CurrentUICulture));
    }

    /// <summary>
    /// Returns a string approximation of the time since the specified time.
    /// </summary>
    /// <param name="adDate">The ad date.</param>
    /// <returns></returns>
    public static string TimeSincePosted(DateTime adDate)
    {
        TimeSpan delta = DateTime.Now - adDate;

        if( delta.TotalDays > 1 )
        {
            return string.Format("{0} days ago", delta.TotalDays);
        }
        else if( delta.TotalDays == 1)
        {
            return "1 day ago";
        }
        else if( delta.TotalHours > 1)
        {
            return string.Format("{0} hrs", delta.TotalHours);
        }
        else if( delta.TotalHours == 1)
        {
            return "1 hr";
        }
        else if( delta.TotalMinutes > 1)
        {
            return string.Format("{0} mins", delta.TotalMinutes);
        }
        else if( delta.TotalMinutes == 1)
        {
            return "1 min";
        }
        else
        {
            return "0 mins";
        }
    }
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • Your code implements most of what the Objective C code does except for handling the time zone names like "DST". They date format specifier "K" only parses numeric date zone offset like "-07:00". See my answer for the missing piece. – Codo Apr 06 '12 at 11:09
2

The Objective C code first parses date and time from a string and then turns it into an easily readable from like "1 day ago".

While Rowland Shaw's code nicely solves the second part, it fails to parse the date/time string because C# doesn't support time zone names like "CDT". In order to solve that, you should look at the method ConvertZoneToLocalDifferential in https://stackoverflow.com/a/284785/413337. The method preprocesses the date/time string so it's parsable by C#.

Community
  • 1
  • 1
Codo
  • 75,595
  • 17
  • 168
  • 206
  • 1
    suppose if want to implement (IST) Indian Standard Tine what i will do? The issue is that it supports all time zones. What is the solution whether i need to add all the time zones or any other tweak. And also India and Israyel share the same time zone abbreviations in that case what i will do?? – Nitha Paul Apr 06 '12 at 12:23
  • 1
    I dont’t know why u guys down voting the question. This is valid Bug and i am getting the solutions from the kind heats. If you are not interested with the question just leave it, don’t disgrace .. its a request . – Nitha Paul Apr 06 '12 at 12:27
  • @NithaPaul: I cannot tell for sure where your date string is coming from and what standard it adheres to. But it looks like an RFC 822 date-time expression (see [RFC 822](http://www.faqs.org/rfcs/rfc822.html), chapter 5). If it is, then only 10 different time zone can be specified by their abbreviation. The rest has to be specified numerically. – Codo Apr 06 '12 at 14:42