Skip to content

Commit

Permalink
Added editor config and perf improvements by parsing the most common …
Browse files Browse the repository at this point in the history
…time units first.
  • Loading branch information
niemyjski committed Dec 16, 2016
1 parent 2d2b72b commit bbc9920
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 17 deletions.
65 changes: 65 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# http://EditorConfig.org
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference

root = true

[*]
charset = utf-8
end_of_line = crlf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
indent_size = 4
trim_trailing_whitespace = false


# Dotnet code style settings:
[*.{cs, vb}]
indent_size = 4

# Avoid "this." and "Me." if not necessary
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Use language keywords instead of framework type names for type references
dotnet_style_predefined_type_for_locals_parameters_members = true:error
dotnet_style_predefined_type_for_member_access = false:error

# Suggest more modern language features when available
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion

# CSharp code style settings:
[*.cs]
# K&R styling
csharp_new_line_before_open_brace = none:error

# Prefer "var" everywhere
csharp_style_var_for_built_in_types = false:error
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion

# Prefer method-like constructs to have a block body
csharp_style_expression_bodied_methods = true:none
csharp_style_expression_bodied_constructors = true:none
csharp_style_expression_bodied_operators = true:none

# Prefer property-like constructs to have an expression-body
csharp_style_expression_bodied_properties = true:none
csharp_style_expression_bodied_indexers = true:none
csharp_style_expression_bodied_accessors = true:none

# Suggest more modern language features when available
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
52 changes: 35 additions & 17 deletions src/Exceptionless.DateTimeExtensions/TimeUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,40 @@ public static TimeSpan Parse(string value) {
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException(nameof(value));

var time = ParseTime(value);
if (time.HasValue)
return time.Value;

throw new ArgumentException($"Unable to parse value '{value}' as a valid time value.");
}

public static bool TryParse(string value, out TimeSpan? time) {
time = null;
if (String.IsNullOrEmpty(value))
return false;

time = ParseTime(value);
return time.HasValue;
}

private static TimeSpan? ParseTime(string value) {
// compare using the original value as uppercase M could mean months.
if (value.EndsWith("m")) {
int minutes = Int32.Parse(normalized.Substring(0, normalized.Length - 1));
return new TimeSpan(0, minutes, 0);
}

var normalized = value.ToLowerInvariant().Trim();
if (normalized.EndsWith("h")) {
int hours = Int32.Parse(normalized.Substring(0, normalized.Length - 1));
return new TimeSpan(hours, 0, 0);
}

if (normalized.EndsWith("d")) {
int days = Int32.Parse(normalized.Substring(0, normalized.Length - 1));
return new TimeSpan(days, 0, 0, 0);
}

if (normalized.EndsWith("nanos")) {
long nanoseconds = Int64.Parse(normalized.Substring(0, normalized.Length - 5));
return new TimeSpan((int)Math.Round(nanoseconds / 100d));
Expand All @@ -21,28 +54,13 @@ public static TimeSpan Parse(string value) {
int milliseconds = Int32.Parse(normalized.Substring(0, normalized.Length - 2));
return new TimeSpan(0, 0, 0, 0, milliseconds);
}

if (normalized.EndsWith("s")) {
int seconds = Int32.Parse(normalized.Substring(0, normalized.Length - 1));
return new TimeSpan(0, 0, seconds);
}

// compare using the original value as uppercase M could mean months.
if (value.EndsWith("m")) {
int minutes = Int32.Parse(normalized.Substring(0, normalized.Length - 1));
return new TimeSpan(0, minutes, 0);
}

if (normalized.EndsWith("h")) {
int hours = Int32.Parse(normalized.Substring(0, normalized.Length - 1));
return new TimeSpan(hours, 0, 0);
}

if (normalized.EndsWith("d")) {
int days = Int32.Parse(normalized.Substring(0, normalized.Length - 1));
return new TimeSpan(days, 0, 0, 0);
}

throw new ArgumentException($"Unable to parse value '{value}' as a valid time value.");
return null;
}
}
}

0 comments on commit bbc9920

Please sign in to comment.