timezone - In Go language the API values are stored in UTC but I want to convert the UTC time to EST while accepting the API req

For example, at 10:30 PM Thursday, Eastern Time (ET) is 2:30 AM Friday, Universal Time (UTC)My Api ser

For example, at 10:30 PM Thursday, Eastern Time (ET) is 2:30 AM Friday, Universal Time (UTC)

My Api server is in EST time zone, so now I want to accept all the request until 11:59 PM EST time, but the problem is since my API is doing validation based on UTC time , my current API request it is getting erred stating – Only current Date request is allowed not the future date request (this is my application functionality)

So, I want to know how to achieve this in Go language , I should accept all the API request until 11:59 PM EST it should not throw me off with above error message - – “Only current Date request is allowed not the future date request” Even though API is called from PST or any other time zone, i should accept the request as long as date is within the same date until 11:59 PM EST

Example API parameters : { "PaymentDate": "2025-03-19" }

Any hints will be much appreciated

This is what i have tried, i don't see its working

timeZone, _ := time.LoadLocation("America/New_York")   // Define the time zone
    const timeFormat = "2006-01-02"                        // Define the time format
    s := paymentRequest.PaymentDate.ToString()             // Assuming `s` should be the string representation of the payment date
    t, _ := time.ParseInLocation(timeFormat, s, timeZone)          // Parse the string to time

    fmt.Printf("paymentRequest.PaymentDate Date: %v\n", time.Time(APIRequest.PaymentDate)) // this is API request date
    fmt.Printf("t Date: %v\n", t)


    if t < time.Time(APIRequest.PaymentDate) {
        fmt.Println("IF block ", t)
     } else {
        fmt.Println("ELSE block", t)
     }

I just want to know if there is any fix for my problem , if the timezone doesn't matter (EST,PST,MST etc), but always compare the date against UTC-5 , if it s belong to same day accept or else reject, do you suggest me if there are ant solution for this

For example, at 10:30 PM Thursday, Eastern Time (ET) is 2:30 AM Friday, Universal Time (UTC)

My Api server is in EST time zone, so now I want to accept all the request until 11:59 PM EST time, but the problem is since my API is doing validation based on UTC time , my current API request it is getting erred stating – Only current Date request is allowed not the future date request (this is my application functionality)

So, I want to know how to achieve this in Go language , I should accept all the API request until 11:59 PM EST it should not throw me off with above error message - – “Only current Date request is allowed not the future date request” Even though API is called from PST or any other time zone, i should accept the request as long as date is within the same date until 11:59 PM EST

Example API parameters : { "PaymentDate": "2025-03-19" }

Any hints will be much appreciated

This is what i have tried, i don't see its working

timeZone, _ := time.LoadLocation("America/New_York")   // Define the time zone
    const timeFormat = "2006-01-02"                        // Define the time format
    s := paymentRequest.PaymentDate.ToString()             // Assuming `s` should be the string representation of the payment date
    t, _ := time.ParseInLocation(timeFormat, s, timeZone)          // Parse the string to time

    fmt.Printf("paymentRequest.PaymentDate Date: %v\n", time.Time(APIRequest.PaymentDate)) // this is API request date
    fmt.Printf("t Date: %v\n", t)


    if t < time.Time(APIRequest.PaymentDate) {
        fmt.Println("IF block ", t)
     } else {
        fmt.Println("ELSE block", t)
     }

I just want to know if there is any fix for my problem , if the timezone doesn't matter (EST,PST,MST etc), but always compare the date against UTC-5 , if it s belong to same day accept or else reject, do you suggest me if there are ant solution for this

Share Improve this question edited Mar 21 at 14:21 KshanStag asked Mar 20 at 23:48 KshanStagKshanStag 171 silver badge3 bronze badges 3
  • 2 Please try to provide a reproducable example (i.e. if you are receiving a time as a string, just hardcode that string into your example). Your current example would not compile (you can't use < with time.Time - use time.Before) and it's unclear what you want to compare against (answers like this might be helpful). – Brits Commented Mar 21 at 1:33
  • My advice - you should fully work out the business logic you want before at all trying to figure out how to do it in Go or any other language. Your requirements as stated aren't very clear yet, because you're talking about times and time zones but you are showing data only having whole dates. As others have said, your example is not complete either. We just can't guess what your paymentRequest and APIRequest objects look like. If you want a solid answer, you have to revise your question so that we can run it in isolation and understand your requirements. – Matt Johnson-Pint Commented Mar 21 at 16:39
  • Is this a statement of the problem: The application has two time.Time values. Determine if the two values have the same date in a specified time zone. – Thundercat Commented Mar 22 at 0:55
Add a comment  | 

2 Answers 2

Reset to default 0

Convert the input values and deadline to time.Time values and compare the time.Time values with the Time.Before and Time.After methods.

time.Time values represent an instant in time and can be compared without considering the time's location.

  1. time.Time(APIRequest.PaymentDate): This type conversion is not legal. time.Time is a struct and you cannot directly convert other types (such as a string) to time.Time through type casting. You need to use time.Parse or time.ParseInLocation to parse the string into a time.Time object.

  2. Like other answers, in Go, you cannot directly use < or > to compare values of type time.Time. Instead, you should use the Before(), After(), or Equal() methods to compare time.

func main() {
    estTimeZone, _ := time.LoadLocation("America/New_York")
    const timeFormat = "2006-01-02"
    s := paymentRequest.PaymentDate.ToString()
    t, _ := time.ParseInLocation(timeFormat, s, estTimeZone)

    // Get the current time and convert it to ET time
    now := time.Now().In(estTimeZone)
    // Extract the date part of the current ET time (remove the hours, minutes, and seconds)
    currentDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, estTimeZone)
    // Extract the date part of PaymentDate (remove the hour, minute, and second)
    paymentDateOnly := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, estTimeZone)

    if paymentDateOnly.Equal(currentDate) {
        fmt.Println("Accept request: PaymentDate is within today's range")
    } else {
        fmt.Println("Request rejected: PaymentDate is not within today's range")
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744378086a4571286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信