在django中更改时区感知对象的时区

时间:2015-12-15 00:47:32

标签: django timezone

我有一个时区感知对象:

date = timezone.now()

然后我使用一些中间件获取用户的时区,所以我把它作为一个字符串,如:

tz = "America/New_York"

这可能是一个非常简单的问题,但我无法在任何地方找到答案:如何将日期对象的时区更改为用户的时区?我试过date.localize(tz),但这给了我一个错误。我做错了什么。

1 个答案:

答案 0 :(得分:0)

您需要将字符串转换为时区对象,然后使用 static void Main(string[] args) { using (ClientContext context = new ClientContext(sp_site_address)) { context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; context.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(username, pwd); List list = context.Web.Lists.GetByTitle(requests_list_name); CamlQuery query = new CamlQuery(); query.ViewXml = "<View><RowLimit>100</RowLimit></View>"; ListItemCollection items = list.GetItems(query); context.Load(items); context.ExecuteQuery(); foreach (ListItem oListItem in items) { FileCollection files = GetAttachments(context, list, oListItem); foreach (Microsoft.SharePoint.Client.File f in files) { Download(f.ServerRelativeUrl, FolderToSaveTo, context, f.Name); } lstRequests.Add(Agreement); } } } public static FileCollection GetAttachments(ClientContext ctx, List list, ListItem item) { ctx.Load(list, l => l.RootFolder.ServerRelativeUrl); ctx.Load(ctx.Site, s => s.Url); ctx.ExecuteQuery(); Folder attFolder = ctx.Web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id); FileCollection files = attFolder.Files; ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl, f => f.Name, f => f.ServerRelativeUrl)); ctx.ExecuteQuery(); return files; } public static void Download(string serverFilePath, string destPath, ClientContext context, string filename) { using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, serverFilePath)) { if (System.IO.Directory.Exists(destPath)) { using (FileStream fileStream = System.IO.File.Create(destPath + "\\" + filename)) { using (MemoryStream stream = new MemoryStream()) { ffl.Stream.CopyTo(stream); stream.WriteTo(fileStream); } } } } }

astimezone()