获取部署在Cloudhub上的API用户时区。

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月1日 15:53:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定