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 Answers
Reset to default 0Convert 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.
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) totime.Time
through type casting. You need to usetime.Parse
ortime.ParseInLocation
to parse the string into atime.Time
object.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
<
withtime.Time
- usetime.Before
) and it's unclear what you want to compare against (answers like this might be helpful). – Brits Commented Mar 21 at 1:33paymentRequest
andAPIRequest
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:39time.Time
values. Determine if the two values have the same date in a specified time zone. – Thundercat Commented Mar 22 at 0:55