473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How do you parse a time string with unit (us, ms, s, etc.) to TimeSpan

Hi folks,

I am willing to create a TypeConverter that allows converting a string in a TimeSpan.
The thing is I want to be able to enter a time with units such as:
75ms or m
125us or u
0.25s

I don't really know what to use for parsing the string.
I would like to use RegEx but I can't figure what format to use.

So far I have this structure of Converter:

Expand|Select|Wrap|Line Numbers
  1.     public class TimeConverter : TypeConverter
  2.     {
  3.  
  4.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  5.         {
  6.             if (sourceType == typeof(string))
  7.                 return true;
  8.             return base.CanConvertFrom(context, sourceType);
  9.         }
  10.  
  11.         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  12.         {
  13.             if (destinationType == typeof(InstanceDescriptor))
  14.                 return true;
  15.             return base.CanConvertTo(context, destinationType);
  16.         }
  17.  
  18.         public override object ConvertFrom(ITypeDescriptorContext context,
  19.             System.Globalization.CultureInfo culture, object value)
  20.         {
  21.             if (value is string)
  22.             {
  23.                 string str = ((string)value);
  24.                 TimeSpan ts;
  25.  
  26.                 ts = TimeSpan.Parse(str);
  27.  
  28.                 return ts;
  29.             }
  30.             return base.ConvertFrom(context, culture, value);
  31.         }
  32.  
  33.         // Overrides the ConvertTo method of TypeConverter.
  34.         public override object ConvertTo(ITypeDescriptorContext context,
  35.            System.Globalization.CultureInfo culture, object value, Type destinationType)
  36.         {
  37.             return base.ConvertTo(context, culture, value, destinationType);
  38.         }
  39.     }
  40.  
  41.  
Thanks for your help.

Alex
Nov 10 '10 #1
1 2297
Christian Binder
218 Expert 100+
I think, the simplest way would be if the last character are m, s, u ms or us.
And if its one of these check the previous string (without time-unit) if it's a number.
I don't know much about RegEx, so I would do it this way.
E.g.
Expand|Select|Wrap|Line Numbers
  1. string timestr = 125us;
  2. double timevalue;
  3. if(timestr.EndsWith("us") && double.TryParse(timestr.Substring(0, timestr.Length - 2), out timevalue)) {
  4.   //create timespan
  5. } else if(timestr.EndsWiths("s") && double.TryParse(timestr.Substring(0, timestr.Length - 1), out timevalue)) {
  6.   //create timespan
  7. }
  8. else if //...
  9.  
Nov 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Peter Kleiweg | last post by:
Is there a safe and clean way to parse a date/time string into seconds since epoch? I have a string with date and time in GMT. I can get the correct value using this: #!/usr/bin/env python...
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
9
by: Danny | last post by:
HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this<br>is<br>a<br>test would return 3 for <br>
3
by: Ilja Booij | last post by:
Hi all, I have some trouble with the following: I'm getting a time string, in YYYY-MM-DD HH:mm:ss format, which I need to translate into a string with DD-Mon-YYYY HH:mm:ss +HHMM, where the...
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
4
by: tony | last post by:
Hello! I have this method ConvStdTimeToSec here that convert format hh:mm:ss into total number of seconds and return it. I just wonder if there exist a metod in the .NET framework that can be...
3
by: Robert Dailey | last post by:
Hi, I have a string in the following format: "00:00:25.886411" I would like to pass this string into the datetime.time() class and have it parse the string and use the values. However, the...
14
by: Arjen | last post by:
Hi, I have a string with this value 07/11/2008. As you see no time included. How can I parse this string to a datetime variable? I tried to do this but I get the exception that the format is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.