📄 usertime.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
namespace CommunityServer.Components
{
/// <summary>
/// Provides logic to move to and from User and Server time
/// </summary>
public class UserTime
{
private UserTime(){}
/// <summary>
/// Converts a date to the Client/user's local time
/// </summary>
public static DateTime ConvertToUserTime(DateTime dt, double ClientTimeZone)
{
return dt.AddHours(FromClientToServerFactor(ClientTimeZone, CSContext.Current.SiteSettings.TimezoneOffset));
}
public static DateTime ConvertToUserTime(DateTime dt)
{
CSContext csContext = CSContext.Current;
return dt.AddHours(FromClientToServerFactor(csContext.User.Profile.Timezone, csContext.SiteSettings.TimezoneOffset));
}
/// <summary>
/// Converts a date to the Servers local datetime
/// </summary>
public static DateTime ConvertToServerTime(DateTime dt, double ClientTimeZone)
{
return dt.AddHours(FromServerToClientFactor(ClientTimeZone,CSContext.Current.SiteSettings.TimezoneOffset));
}
public static DateTime ConvertToServerTime(DateTime dt)
{
CSContext csContext = CSContext.Current;
return dt.AddHours(FromServerToClientFactor(csContext.User.Profile.Timezone,csContext.SiteSettings.TimezoneOffset));
}
/// <summary>
/// Returns the local time of the current user
/// </summary>
public static DateTime CurrentUserTime
{
get
{
if(CSContext.Current.User.IsAnonymous)
return DateTime.Now;
return DateTime.Now.AddHours(ClientToServerTimeZoneFactor);
}
}
/// <summary>
/// Returns the local server time
/// </summary>
public static DateTime CurrentServerTime
{
get {return DateTime.Now;}
}
#region helpers
/// <summary>
/// Returns the TimeZone factor for the current user
/// </summary>
private static double ClientToServerTimeZoneFactor
{
get
{
CSContext context = CSContext.Current;
return FromClientToServerFactor(context.User.Profile.Timezone,context.SiteSettings.TimezoneOffset);
}
}
private static double FromClientToServerFactor(double Client, double Server)
{
return Client - Server;
}
private static double FromServerToClientFactor(double Client, double Server)
{
return Server - Client;
}
/// <summary>
/// Returns the TimeZone factor the server compared to the current user
/// </summary>
private static double ServerToClientTimeZoneFactor
{
get
{
CSContext context = CSContext.Current;
return FromServerToClientFactor(context.User.Profile.Timezone,context.SiteSettings.TimezoneOffset);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -