using System; namespace Siia.WxPlatform.Web.Weixin.Helpers { /// <summary> /// 微信 API 工具类 /// </summary> public class ApiUtility { public static int GetTimeStamp { get { //var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); var startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);//等价的建议写法 return (int)(DateTime.Now - startTime).TotalSeconds; } } /// <summary> /// 获取过期时间 /// </summary> /// <param name="expireInSeconds">有效时间(秒)</param> /// <returns></returns> public static DateTimeOffset GetExpireTime(int expireInSeconds) { return DateTime.Now.Add(GetExpiryTimeSpan(expireInSeconds));//提前x分钟重新获取 } /// <summary> /// 获取过期 TimeSpan /// </summary> /// <param name="expireInSeconds">有效时间(秒)</param> /// <returns></returns> public static TimeSpan GetExpiryTimeSpan(int expireInSeconds) { if (expireInSeconds > 3600) { expireInSeconds -= 600;//提前10分钟过期 } else if (expireInSeconds > 1800) { expireInSeconds -= 300;//提前5分钟过期 } else if (expireInSeconds > 300) { expireInSeconds -= 30;//提前1分钟过期 } return TimeSpan.FromSeconds(expireInSeconds); } } }