Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
auto-tx
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vamp
auto-tx
Commits
7cecf8de
Commit
7cecf8de
authored
7 years ago
by
Niko Ehrenfeuchter
Browse files
Options
Downloads
Patches
Plain Diff
Make the time delta description suffix optional.
Refers to
#25
parent
0b515eb1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ATxCommon/TimeUtils.cs
+24
-17
24 additions, 17 deletions
ATxCommon/TimeUtils.cs
with
24 additions
and
17 deletions
ATxCommon/TimeUtils.cs
+
24
−
17
View file @
7cecf8de
...
...
@@ -34,14 +34,18 @@ namespace ATxCommon
/// Convert a number of seconds to a human readable string.
/// </summary>
/// <param name="delta">The time span in seconds.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns>
public
static
string
SecondsToHuman
(
long
delta
)
{
var
desc
=
"ago"
;
public
static
string
SecondsToHuman
(
long
delta
,
bool
addDesc
=
true
)
{
var
desc
=
"
ago"
;
if
(
delta
<
0
)
{
delta
*=
-
1
;
desc
=
"in the future"
;
desc
=
"
in the future"
;
}
if
(!
addDesc
)
desc
=
""
;
const
int
second
=
1
;
const
int
minute
=
second
*
60
;
const
int
hour
=
minute
*
60
;
...
...
@@ -51,27 +55,27 @@ namespace ATxCommon
const
int
year
=
day
*
365
;
if
(
delta
<
minute
)
return
$"
{
delta
}
seconds
{
desc
}
"
;
return
$"
{
delta
}
seconds
{
desc
}
"
;
if
(
delta
<
2
*
minute
)
return
$"a minute
{
desc
}
"
;
return
$"a minute
{
desc
}
"
;
if
(
delta
<
hour
)
return
$"
{
delta
/
minute
}
minutes
{
desc
}
"
;
return
$"
{
delta
/
minute
}
minutes
{
desc
}
"
;
if
(
delta
<
day
)
{
var
hours
=
delta
/
hour
;
var
mins
=
(
delta
-
hours
*
hour
)
/
minute
;
if
(
mins
>
0
)
return
$"
{
hours
}
hours
{
mins
}
minutes
{
desc
}
"
;
return
$"
{
hours
}
hours
{
desc
}
"
;
return
$"
{
hours
}
hours
{
mins
}
minutes
{
desc
}
"
;
return
$"
{
hours
}
hours
{
desc
}
"
;
}
if
(
delta
<
2
*
week
)
return
$"
{
delta
/
day
}
days
$
{
desc
}
"
;
return
$"
{
delta
/
day
}
days
{
desc
}
"
;
if
(
delta
<
2
*
month
)
return
$"
{
delta
/
week
}
weeks
{
desc
}
"
;
return
$"
{
delta
/
week
}
weeks
{
desc
}
"
;
if
(
delta
<
year
)
{
var
months
=
delta
/
month
;
...
...
@@ -82,7 +86,7 @@ namespace ATxCommon
ret
+=
$"
{
weeks
}
weeks"
;
if
(
days
>
0
)
ret
+=
$"
{
days
}
days"
;
return
$"
{
ret
}
{
desc
}
"
;
return
$"
{
ret
}{
desc
}
"
;
}
if
(
delta
<
10
*
year
)
{
...
...
@@ -94,34 +98,37 @@ namespace ATxCommon
ret
+=
$"
{
months
}
months"
;
if
(
days
>
0
)
ret
+=
$"
{
days
}
days"
;
return
$"
{
ret
}
{
desc
}
"
;
return
$"
{
ret
}{
desc
}
"
;
}
return
$"
{
delta
/
year
}
years
{
desc
}
"
;
return
$"
{
delta
/
year
}
years
{
desc
}
"
;
}
/// <summary>
/// Wrapper to use <see cref="SecondsToHuman"/> with minutes as input.
/// </summary>
/// <param name="delta">The time span in minutes.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns>
public
static
string
MinutesToHuman
(
long
delta
)
{
return
SecondsToHuman
(
delta
*
60
);
public
static
string
MinutesToHuman
(
long
delta
,
bool
addDesc
=
true
)
{
return
SecondsToHuman
(
delta
*
60
,
addDesc
);
}
/// <summary>
/// Wrapper to use <see cref="SecondsToHuman"/> with days as input.
/// </summary>
/// <param name="delta">The time span in days.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the duration, e.g. "12 days" or "3 weeks".</returns>
public
static
string
DaysToHuman
(
long
delta
)
{
return
MinutesToHuman
(
delta
*
60
*
24
);
public
static
string
DaysToHuman
(
long
delta
,
bool
addDesc
=
true
)
{
return
MinutesToHuman
(
delta
*
60
*
24
,
addDesc
);
}
/// <summary>
/// Wrapper to convert a date into a human readable string relative to now.
/// </summary>
/// <param name="refDate">The reference DateTime to check.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the delta, e.g. "12 days" or "3 weeks".</returns>
public
static
string
HumanSince
(
DateTime
refDate
)
{
if
(
refDate
==
DateTime
.
MinValue
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment