-
Notifications
You must be signed in to change notification settings - Fork 338
/
DateExtensions.cs
59 lines (48 loc) · 3.13 KB
/
DateExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright (c) 2014, Firely (info@fire.ly) and contributors
* See the file CONTRIBUTORS for details.
*
* This file is licensed under the BSD 3-Clause license
* available at https://raw.githubusercontent.com/FirelyTeam/fhir-net-api/master/LICENSE
*/
using Hl7.Fhir.Serialization;
using System;
namespace Hl7.Fhir.Support
{
public static class DateExtensions
{
/// <summary>
/// This function will return only the date part (as a string) of the Datetime parameter.
/// Be aware: this function will not bother about the timezone. 21-11-2018 23:00 in Amsterdam is not the same as in Austrilia: the date
/// will be there already 22-11-2018.
/// </summary>
/// <param name="me">a Date time</param>
/// <returns>The date part of the datetime as a string</returns>
public static string ToFhirDate(this System.DateTime me) => me.ToString("yyyy-MM-dd");
/// <summary>
/// This function will return only the date part (as a string) of the Datetime parameter.
/// Be aware: this function will not bother about the timezone. 21-11-2018 23:00 in Amsterdam is not the same as in Austrilia: the date
/// will be there already 22-11-2018.
/// </summary>
/// <param name="me">a Date time</param>
/// <returns>if me is empty then return null otherwie return the date part of the datetime as a string</returns>
public static string ToFhirDate(this System.DateTime? me) => me.HasValue ? me.Value.ToString("yyyy-MM-dd") : null;
/// <summary>
/// Returns a FHIR datetime as a string
/// </summary>
/// <param name="me">A date and time.</param>
/// <param name="offset">The time's offset from Coordinated Universal Time (UTC).</param>
/// <returns>Returns a FHIR datetime as a string</returns>
public static string ToFhirDateTime(this System.DateTime me, TimeSpan offset) => ToFhirDateTime(new DateTimeOffset(me, offset));
public static string ToFhirDate(this System.DateTimeOffset me) => me.ToString("yyyy-MM-dd");
public static string ToFhirDate(this System.DateTimeOffset? me) => me.HasValue ? me.Value.ToString("yyyy-MM-dd") : null;
public static string ToFhirDateTime(this System.DateTimeOffset me) => PrimitiveTypeConverter.ConvertTo<string>(me);
public static string ToFhirDateTime(this System.DateTimeOffset? me) => me.HasValue ? PrimitiveTypeConverter.ConvertTo<string>(me) : null;
#region Obsolete members
[Obsolete("Use ToFhirDateTime(this System.DateTimeOffset me) instead, see https://docs.microsoft.com/en-us/dotnet/standard/datetime/converting-between-datetime-and-offset")]
public static string ToFhirDateTime(this System.DateTime me) => PrimitiveTypeConverter.ConvertTo<string>(me);
[Obsolete("Use ToFhirDateTime(this System.DateTimeOffset? me) instead, see https://docs.microsoft.com/en-us/dotnet/standard/datetime/converting-between-datetime-and-offset")]
public static string ToFhirDateTime(this System.DateTime? me) => me.HasValue ? PrimitiveTypeConverter.ConvertTo<string>(me) : null;
#endregion
}
}