Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Use local time zone when issuing a code.
Browse files Browse the repository at this point in the history
Rather than requiring the user to use UTC when requesting a code, use
the client's local timezone.

Fixes #63
  • Loading branch information
jeremyfaller committed Aug 24, 2020
1 parent 20947bc commit 7e40dae
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
13 changes: 7 additions & 6 deletions cmd/get-code/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import (
)

var (
testFlag = flag.String("type", "", "diagnosis test type: confirmed, likely, negative")
onsetFlag = flag.String("onset", "", "Symptom onset date, YYYY-MM-DD format")
apikeyFlag = flag.String("apikey", "", "API Key to use")
addrFlag = flag.String("addr", "http://localhost:8080", "protocol, address and port on which to make the API call")
timeoutFlag = flag.Duration("timeout", 5*time.Second, "request time out duration in the format: 0h0m0s")
testFlag = flag.String("type", "", "diagnosis test type: confirmed, likely, negative")
onsetFlag = flag.String("onset", "", "Symptom onset date, YYYY-MM-DD format")
tzOffsetFlag = flag.Int("tzOffset", 0, "timezone adjustment (minutes) from UTC for request")
apikeyFlag = flag.String("apikey", "", "API Key to use")
addrFlag = flag.String("addr", "http://localhost:8080", "protocol, address and port on which to make the API call")
timeoutFlag = flag.Duration("timeout", 5*time.Second, "request time out duration in the format: 0h0m0s")
)

func main() {
Expand All @@ -58,7 +59,7 @@ func main() {
func realMain(ctx context.Context) error {
logger := logging.FromContext(ctx)

request, response, err := clients.IssueCode(ctx, *addrFlag, *apikeyFlag, *testFlag, *onsetFlag, *timeoutFlag)
request, response, err := clients.IssueCode(ctx, *addrFlag, *apikeyFlag, *testFlag, *onsetFlag, *tzOffsetFlag, *timeoutFlag)
logger.Infow("sent request", "request", request)
if err != nil {
return fmt.Errorf("failed to get token: %w", err)
Expand Down
15 changes: 3 additions & 12 deletions cmd/server/assets/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,15 @@ <h1>Create verification code</h1>
<div class="card-body">
<div class="form-row">
<div class="form-group col-md-6">
<label for="testDate">Testing date</label>
<label for="testDate">Testing date (local time)</label>
<input type="date" id="test-date" name="testDate" min="{{.minDate}}" max="{{.maxDate}}" class="form-control" />
<small class="form-text text-muted">
<strong>Recommended.</strong>
This is the <a href="https://www.timeanddate.com/worldclock/timezone/utc" target="_blank">UTC date</a> when the test was performed.
Click here to <a href="#" data-fill-target="test-date" data-fill-value="{{.maxDate}}">use today's UTC date</a>.
</small>
</div>

<div class="form-group col-md-6">
<label for="symptomDate">Symptoms onset</label>
<label for="symptomDate">Symptoms onset (local time)</label>
<div class="input-group">
<input type="date" id="symptom-date" name="symptomDate" min="{{.minDate}}" max="{{.maxDate}}" class="form-control" />
</div>
<small class="form-text text-muted">
<strong>Recommended.</strong>
This is the <a href="https://www.timeanddate.com/worldclock/timezone/utc" target="_blank">UTC date</a> when symptoms began. It cannot be more than {{.maxSymptomDays}} days ago.
Click here to <a href="#" data-fill-target="symptom-date" data-fill-value="{{.maxDate}}">use today's UTC date</a>.
</small>
</div>
</div>
</div>
Expand Down Expand Up @@ -270,6 +260,7 @@ <h1>Create verification code</h1>
$($form.serializeArray()).each(function(i, obj) {
data[obj.name] = obj.value
});
data.tzOffset = new Date().getTimezoneOffset();

getCode(data);
});
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type IssueCodeRequest struct {
SymptomDate string `json:"symptomDate"` // ISO 8601 formatted date, YYYY-MM-DD
TestDate string `json:"testDate"`
TestType string `json:"testType"`
TZOffset int `json:"tzOffset"` // offset in minutes of the user's timezone.
Phone string `json:"phone"`
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/clients/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import (

// IssueCode uses the ADMIN API to issue a verification code.
// Currently does not accept the SMS param.
func IssueCode(ctx context.Context, hostname string, apiKey, testType, symptomDate string, timeout time.Duration) (*api.IssueCodeRequest, *api.IssueCodeResponse, error) {
func IssueCode(ctx context.Context, hostname string, apiKey, testType, symptomDate string, tzMinOffset int, timeout time.Duration) (*api.IssueCodeRequest, *api.IssueCodeResponse, error) {
url := hostname + "/api/issue"
request := api.IssueCodeRequest{
TestType: testType,
SymptomDate: symptomDate,
TZOffset: tzMinOffset,
}
client := &http.Client{
Timeout: timeout,
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/issueapi/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (c *Controller) HandleIssue() http.Handler {
return
}

// Max date is today (local time) and min date is AllowedTestAge ago, truncated.
maxDate := time.Now().Local()
// Max date is today (UTC time) and min date is AllowedTestAge ago, truncated.
maxDate := time.Now().UTC()
minDate := maxDate.Add(-1 * c.config.GetAllowedSymptomAge()).Truncate(24 * time.Hour)

var symptomDate *time.Time
Expand All @@ -107,6 +107,7 @@ func (c *Controller) HandleIssue() http.Handler {
return
} else {
parsed = parsed.Local()
parsed = parsed.Add(time.Duration(request.TZOffset) * time.Minute) // adjust to UTC
if minDate.After(parsed) || parsed.After(maxDate) {
err := fmt.Errorf("symptom onset date must be on/after %v and on/before %v",
minDate.Format("2006-01-02"),
Expand Down

0 comments on commit 7e40dae

Please sign in to comment.