|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Signum.Utilities;
namespace Signum.Entities
{
public static class TimeZoneManager
{
public static TimeZoneMode Mode { get; set; }
//TimeZoneInfo.Local is read-only. For ASP.Net or other server providers where local time is needed.
[ThreadStatic]
static TimeZoneInfo overrideTimeZone;
public static TimeZoneInfo OverrideTimeZone
{
get { return overrideTimeZone; }
set { overrideTimeZone = value; }
}
public static DateTime Now
{
get
{
if (Mode == TimeZoneMode.Local)
return DateTime.Now;
else
return DateTime.UtcNow;
}
}
public static DateTime ToUserInterface(this DateTime dbDateTime)
{
if (Mode == TimeZoneMode.Local)
return dbDateTime;
else
if (OverrideTimeZone == null)
return dbDateTime.ToLocalTime();
else
{
var result = TimeZoneInfo.ConvertTimeFromUtc(dbDateTime, OverrideTimeZone);
if (dbDateTime.Kind == DateTimeKind.Unspecified)
result = new DateTime(dbDateTime.Ticks, DateTimeKind.Local); //Convert asserts TimeZoneInfo to be Local if DateTime.King is Local
return result;
}
}
public static DateTime FromUserInterface(this DateTime uiDateTime)
{
if (Mode == TimeZoneMode.Local)
return uiDateTime;
else
if (OverrideTimeZone == null)
return uiDateTime.ToUniversalTime();
else
{
if (uiDateTime.Kind == DateTimeKind.Local)
uiDateTime = new DateTime(uiDateTime.Ticks, DateTimeKind.Unspecified);
return TimeZoneInfo.ConvertTimeToUtc(uiDateTime, OverrideTimeZone);
}
}
}
public enum TimeZoneMode
{
Local,
Utc,
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.
www.signumframework.com
I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...