英文:
Getting user timezone on an api deployed to Cloudhub
问题
我正在尝试动态获取用户的时区并将其应用于我的负载中的dateTime值。
我目前在DataWeave中使用以下函数:
var dateTimeZone = now() as TimeZone
fun formatDate(dtValue :String, dateTimeZone: TimeZone) : DateTime =
dtValue as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"} as DateTime {"format" : "dd/MM/yyyy HH:mm", timezone: dateTimeZone}
当我在本地运行这段代码时,它按预期工作。
但是,当我部署我的API到CloudHub时,我发现它使用的是UTC时区(我想这是服务器的时区)。
然而,我需要获取用户的时区,无论他们身在何处,所以如果用户在加利福尼亚,例如,我需要将其格式化为"PST"。
是否有一种在代码中实现的解决这个问题的方法,而不需要添加查询参数或更改除这个函数之外的任何内容?
英文:
I'm trying to dynamically get the user timezone and apply it to the dateTime values in my payload.
I'm currently using this function in dataweave:
var dateTimeZone = now() as TimeZone
fun formatDate(dtValue :String, dateTimeZone: TimeZone) : DateTime =
dtValue as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"} as DateTime {"format" : "dd/MM/yyyy HH:mm", timezone: dateTimeZone}
When I run this code locally it works as expected.
When I deploy my API to CloudHub I found out it uses UTC (which I suppose is the server's timezone)
I however need to get the user timezone wherever they are, so if the user is in California for example I need it to format it to "PST".
Is there a workaround to this problem that I can implement in code without adding query parameters or changing anything but this function?
答案1
得分: 1
客户端 API 的时区不是您的 API 实现知道的内容,无论使用什么技术。您需要从请求中接收它,作为查询参数或从请求体中获取。
英文:
Timezone of the client of an API is not something your API implementation knows, whatever the technology. You need to receive it from the request, as a query parameter or from a the body of the request.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论