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: -
public class TimeConverter : TypeConverter
-
{
-
-
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
-
{
-
if (sourceType == typeof(string))
-
return true;
-
return base.CanConvertFrom(context, sourceType);
-
}
-
-
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
-
{
-
if (destinationType == typeof(InstanceDescriptor))
-
return true;
-
return base.CanConvertTo(context, destinationType);
-
}
-
-
public override object ConvertFrom(ITypeDescriptorContext context,
-
System.Globalization.CultureInfo culture, object value)
-
{
-
if (value is string)
-
{
-
string str = ((string)value);
-
TimeSpan ts;
-
-
ts = TimeSpan.Parse(str);
-
-
return ts;
-
}
-
return base.ConvertFrom(context, culture, value);
-
}
-
-
// Overrides the ConvertTo method of TypeConverter.
-
public override object ConvertTo(ITypeDescriptorContext context,
-
System.Globalization.CultureInfo culture, object value, Type destinationType)
-
{
-
return base.ConvertTo(context, culture, value, destinationType);
-
}
-
}
-
-
Thanks for your help.
Alex
1 2297
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. -
string timestr = 125us;
-
double timevalue;
-
if(timestr.EndsWith("us") && double.TryParse(timestr.Substring(0, timestr.Length - 2), out timevalue)) {
-
//create timespan
-
} else if(timestr.EndsWiths("s") && double.TryParse(timestr.Substring(0, timestr.Length - 1), out timevalue)) {
-
//create timespan
-
}
-
else if //...
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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>
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| | |