2013-10-22 5 views
5

मैं "जिरा समय नोटेशन" में दर्ज उपयोगकर्ता इनपुट से मिनटों की संख्या निकालने का प्रयास कर रहा हूं।"जिरा नोटेशन" में समय स्ट्रिंग से मिनट निकालें

उदाहरण के लिए, मैं निम्नलिखित परिणाम

प्राप्त करने के लिए चाहते हैं
  • इनपुट: "30"/आउटपुट: 30
  • इनपुट: "1 घंटे 20 मीटर"/आउटपुट: 80
  • इनपुट: "3 ज "/ आउटपुट 180

अनुसंधान से, मुझे टाइमस्पेन मिला है। पैरासेक्स लेकिन मुझे यह पता नहीं चल सकता कि मुझे क्या चाहिए इसकी आवश्यकता है।

सभी मदद की बहुत सराहना की जाएगी।

मेरे कोड अब तक:

एक हथौड़े दृष्टिकोण के
public static int TextToMins(string TimeText) 
    { 
     CultureInfo culture = new CultureInfo("en-IE"); 
     string[] formats = { "What goes here?" }; 
     TimeSpan ts = TimeSpan.ParseExact(TimeText.Trim().ToLower(), formats, culture); 
     return Convert.ToInt32(ts.TotalMinutes); 
    } 
+0

मैंने आपका शीर्षक संपादित किया है। कृपया देखें, "[प्रश्नों में उनके शीर्षक में" टैग "शामिल होना चाहिए?] (Http://meta.stackexchange.com/questions/19190/)", जहां आम सहमति है "नहीं, उन्हें नहीं करना चाहिए"। –

उत्तर

1

बिट, लेकिन कैसे के बारे में:

public static int TextToMins(string timeText) 
{ 
    var total = 0; 
    foreach (var part in timeText.Split(' ')) 
    { 
     if (part[part.Length - 1] == 'h') 
     { 
      total += 60 * int.Parse(part.Trim('h')); 
     } 
     else 
     { 
      total += int.Parse(part.Trim('m')); 
     } 
    } 
    return total; 
} 
+0

मेरे लिए काम करता है! धन्यवाद! – user1515364

+0

@ user1515364 इसे सुनकर खुशी हुई। यह समस्या के समाधान के रूप में साफ नहीं है क्योंकि आप यद्यपि कोशिश कर रहे थे। लेकिन अगर यह नौकरी करता है, और पठनीय है, तो शायद यह काफी अच्छा है ... –

1

करने के लिए "क्या यहाँ जाता है" जवाब, एक स्ट्रिंग विकल्पों में से बाहर बनाया गया है Custom Timespan Format Strings से। यदि मैं दस्तावेज़ीकरण को सही तरीके से पढ़ रहा हूं, तो उस सूची में वर्ण नहीं है - व्हाइटस्पेस समेत - \ वर्ण से बचने के लिए या एकल उद्धरण चिह्नों से घिरा होना चाहिए।

उदाहरण के लिए, "1 एम 10 मीटर" और h\h m\m को "1h 10m" पार्स करने के लिए m\m आज़माएं। तो अपने कोड होगा:

string[] formats = { "m\m", "h\h\ m\m" }; 

चेतावनी: मैं TimeSpan वस्तुओं को पार्स प्रयास नहीं किया है। लेकिन मैंने डेटटाइम ऑब्जेक्ट्स किया है, और यह बहुत समान है। तो मुझे लगता है कि यह काम करना चाहिए।

3

मैं शायद ऐसा कुछ करूंगा और टाइम्सपैन के अंतर्निहित पार्सिंग के बारे में सोचने से बचूंगा, क्योंकि प्रति दिन [कार्य] सप्ताह और [काम] घंटे प्रति दिन [काम] दिन जिरा में विन्यास योग्य मान हैं (हमारे सिस्टम पर वे प्रति सप्ताह 5 दिन और प्रतिदिन 8 घंटे के रूप में कॉन्फ़िगर किए जाते हैं।)

class JiraTimeDurationParser 
{ 

    /// <summary> 
    /// Jira's configured value for [working] days per week ; 
    /// </summary> 
    public ushort DaysPerWeek  { get ; private set ; } 

    /// <summary> 
    /// Jira's configured value for [working] hours per day 
    /// </summary> 
    public ushort HoursPerDay  { get ; private set ; } 

    public JiraTimeDurationParser(ushort daysPerWeek = 5 , ushort hoursPerDay = 8) 
    { 
    if (daysPerWeek < 1 || daysPerWeek > 7) throw new ArgumentOutOfRangeException("daysPerWeek" ) ; 
    if (hoursPerDay < 1 || hoursPerDay > 24) throw new ArgumentOutOfRangeException("hoursPerDay" ) ; 

    this.DaysPerWeek = daysPerWeek ; 
    this.HoursPerDay = hoursPerDay ; 

    return ; 
    } 

    private static Regex rxDuration = new Regex(@" 
    ^         # drop anchor at start-of-line 
     [\x20\t]* ((?<weeks> \d+) w)? # Optional whitespace, followed by an optional number of weeks 
     [\x20\t]* ((?<days> \d+) d)? # Optional whitesapce, followed by an optional number of days 
     [\x20\t]* ((?<hours> \d+) h)? # Optional whitespace, followed by an optional number of hours 
     [\x20\t]* ((?<minutes> \d+) m) # Optional whitespace, followed by a mandatory number of minutes 
     [\x20\t]*       # Optional trailing whitespace 
    $         # followed by end-of-line 
    " , 
    RegexOptions.IgnorePatternWhitespace 
    ) ; 

    public TimeSpan Parse(string jiraDuration) 
    { 
    if (string.IsNullOrEmpty(jiraDuration)) throw new ArgumentOutOfRangeException("jiraDuration"); 

    Match m = rxDuration.Match(jiraDuration) ; 
    if (!m.Success) throw new ArgumentOutOfRangeException("jiraDuration") ; 

    int weeks ; bool hasWeeks = int.TryParse(m.Groups[ "weeks" ].Value , out weeks ) ; 
    int days ; bool hasDays = int.TryParse(m.Groups[ "days" ].Value , out days ) ; 
    int hours ; bool hasHours = int.TryParse(m.Groups[ "hours" ].Value , out hours ) ; 
    int minutes ; bool hasMinutes = int.TryParse(m.Groups[ "minutes" ].Value , out minutes) ; 

    bool isValid = hasWeeks|hasDays|hasHours|hasMinutes ; 
    if (!isValid) throw new ArgumentOutOfRangeException("jiraDuration") ; 

    TimeSpan duration = new TimeSpan(weeks*DaysPerWeek*HoursPerDay + days*HoursPerDay + hours , minutes , 0); 
    return duration ; 

    } 

    public bool TryParse(string jiraDuration , out TimeSpan timeSpan) 
    { 
    bool success ; 
    try 
    { 
     timeSpan = Parse(jiraDuration) ; 
     success = true ; 
    } 
    catch 
    { 
     timeSpan = default(TimeSpan) ; 
     success = false ; 
    } 
    return success ; 
    } 

} 
+0

कोड में कुछ छोटी त्रुटियां; तय की। –

1

बस एक ही समस्या थी। यहाँ मेरी समाधान (इकाई का परीक्षण किया) है, क्या इसके लायक है के लिए:

public static TimeSpan Parse(string s) 
{ 
    long seconds = 0; 
    long current = 0; 

    int len = s.Length; 
    for (int i=0; i<len; ++i) 
    { 
     char c = s[i]; 

     if (char.IsDigit(c)) 
     { 
      current = current * 10 + (int)char.GetNumericValue(c); 
     } 
     else if (char.IsWhiteSpace(c)) 
     { 
      continue; 
     } 
     else 
     { 
      long multiplier; 

      switch (c) 
      { 
       case 's': multiplier = 1; break;  // seconds 
       case 'm': multiplier = 60; break;  // minutes 
       case 'h': multiplier = 3600; break; // hours 
       case 'd': multiplier = 86400; break; // days 
       case 'w': multiplier = 604800; break; // weeks 
       default: 
        throw new FormatException(
         String.Format(
          "'{0}': Invalid duration character {1} at position {2}. Supported characters are s,m,h,d, and w", s, c, i)); 
      } 

      seconds += current * multiplier; 
      current = 0; 
     } 
    } 

    if (current != 0) 
    { 
     throw new FormatException(
      String.Format("'{0}': missing duration specifier in the end of the string. Supported characters are s,m,h,d, and w", s)); 
    } 

    return TimeSpan.FromSeconds(seconds); 
} 
2

निम्नलिखित कोड की तरह तार पार्स होगा: "1 घंटे", "1h30m", "12h 45m", "1 घंटे 4 मीटर", "1 दिन 12h 34m 20s "," 80h "," 3000ms "," 20mins "," 1min "।

हर मामले में "रिक्त स्थान अनदेखा किए जाते हैं", यह "दिन, घंटे, मिनट, सेकंड और मिलीसेकंड" का समर्थन करता है लेकिन आसानी से आप महीनों, सप्ताह, साल आदि जोड़ सकते हैं ... बस स्थिति सूची में सही अभिव्यक्ति जोड़ें ।

public static TimeSpan ParseHuman(string dateTime) 
{ 
    TimeSpan ts = TimeSpan.Zero; 
    string currentString = ""; string currentNumber = ""; 
    foreach (char ch in dateTime+' ') 
     { 
      currentString += ch; 
      if (Regex.IsMatch(currentString, @"^(days(\d|\s)|day(\d|\s)|d(\d|\s))", RegexOptions.IgnoreCase)) { ts = ts.Add(TimeSpan.FromDays(int.Parse(currentNumber))); currentString = ""; currentNumber = ""; } 
      if (Regex.IsMatch(currentString, @"^(hours(\d|\s)|hour(\d|\s)|h(\d|\s))", RegexOptions.IgnoreCase)) { ts = ts.Add(TimeSpan.FromHours(int.Parse(currentNumber))); currentString = ""; currentNumber = ""; } 
      if (Regex.IsMatch(currentString, @"^(ms(\d|\s))", RegexOptions.IgnoreCase)) { ts = ts.Add(TimeSpan.FromMilliseconds(int.Parse(currentNumber))); currentString = ""; currentNumber = ""; } 
      if (Regex.IsMatch(currentString, @"^(mins(\d|\s)|min(\d|\s)|m(\d|\s))", RegexOptions.IgnoreCase)) { ts = ts.Add(TimeSpan.FromMinutes(int.Parse(currentNumber))); currentString = ""; currentNumber = ""; } 
      if (Regex.IsMatch(currentString, @"^(secs(\d|\s)|sec(\d|\s)|s(\d|\s))", RegexOptions.IgnoreCase)) { ts = ts.Add(TimeSpan.FromSeconds(int.Parse(currentNumber))); currentString = ""; currentNumber = ""; } 
      if (Regex.IsMatch(ch.ToString(), @"\d")) { currentNumber += ch; currentString = ""; } 
     } 
    return ts; 
} 
+0

मुझे यह विचार पसंद आया।लेकिन यह स्ट्रिंग के लिए विफल रहता है ** 3 दिन 20hours 36mins 17secs 156ms ** – Milad

+1

@ मिलाद, आप सही हैं, मैंने इसे ठीक किया है, अब यह सभी मामलों को शामिल करता है। –

संबंधित मुद्दे