-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change timezone to only accept valid timezones #354
Change timezone to only accept valid timezones #354
Conversation
Codecov Report
@@ Coverage Diff @@
## master #354 +/- ##
============================================
+ Coverage 89.55% 89.64% +0.08%
+ Complexity 995 994 -1
============================================
Files 112 112
Lines 2586 2589 +3
Branches 304 303 -1
============================================
+ Hits 2316 2321 +5
Misses 183 183
+ Partials 87 85 -2
Continue to review full report at Codecov.
|
docs/UserGuide.md
Outdated
@@ -535,7 +535,7 @@ Parameter | Prefix | Constraints, Examples | |||
**EMAIL** | `e/` | Emails should be of the format local-part@domain. <br> e.g. `e/katya@yahoo.com` | |||
**ADDRESS** |`a/` | Addresses can take any values, and it should not be blank. <br> e.g. `Vladivostok, Nevelskogo, bld. 15, appt. 256` | |||
**COUNTRY_CODE** | `c/` | A 2-letter country code that follows the ISO3166 specification <br> This [finding tool](https://www.countrycode.org/) can be used. <br> e.g. `c/SG` (Singapore) | |||
**TIMEZONE** | `tz/` | Timezone should be given in offsets relative to [Greenwich Mean Time](https://en.wikipedia.org/wiki/Greenwich_Mean_Time). <br> e.g. `tz/GMT+8` | |||
**TIMEZONE** | `tz/` | Timezone should be given in offsets relative to [Greenwich Mean Time](https://en.wikipedia.org/wiki/Greenwich_Mean_Time) in the format `GMT+HH:MM` where `HH` refers to the offset in hours and `MM` refers to the offset in minutes. The full list of valid timezones can be found [here](https://www.timeanddate.com/time/current-number-time-zones.html) <br> e.g. `tz/GMT+08:00 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the link leads to a table with UTC timezones instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be used instead? https://greenwichmeantime.com/time-gadgets/time-zone-converter/
+ "-" + SMALLEST_NEGATIVE_OFFSET; | ||
public static final Set<String> VALID_TIMEZONES = new HashSet<>(); | ||
|
||
static { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should standardize (forgot which other file was it) and just remove the static, and add a comment above saying this is some special static init block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only found the one in TbmManager.java
but that one doesn't seem to be for class-level variables
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right ok
List<String> zoneList = new ArrayList<>(allZones); | ||
for (String s : zoneList) { | ||
ZoneOffset offset = LocalDateTime.now().atZone(ZoneId.of(s)).getOffset(); | ||
String out = String.format("%s", offset); | ||
VALID_TIMEZONES.add(out); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a need to convert to arraylist? Cannot just iterate through the set?
VALID_TIMEZONES.remove("Z"); | ||
VALID_TIMEZONES.add("+00:00"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably add a comment stating what this does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bump
if (zoneOffsetIdString.equals(GMT_STRING)) { | ||
zoneOffsetIdString = GMT_STRING + "+00:00"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this some special case? Should add a comment if that's the case
long totalHours = totalMinutes / 60; | ||
long currentHour = ((totalHours + timezoneOffset) % 24); | ||
long totalHours = (totalMinutes + minutesOffset) / 60; | ||
long currentHour = ((totalHours + hoursOffset) % 24); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
long currentHour = ((totalHours + hoursOffset) % 24); | |
long currentHour = (totalHours + hoursOffset) % 24; |
assertFalse(Timezone.isValidTimezone("GM+00:00")); // misspelled GMT | ||
assertFalse(Timezone.isValidTimezone("GMT+15:00")); // out of range (positive) | ||
assertFalse(Timezone.isValidTimezone("GMT-13:00")); // out of range (negative) | ||
assertFalse(Timezone.isValidTimezone("GMT+13:30")); // unrecognised timezone | ||
assertFalse(Timezone.isValidTimezone("GMT+654657987654456")); // large number |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add a few more negative test cases like "GMT" and "GMT+" and "GMT-", and maybe "gmt+08:00", and "GMT+8" and "GMT+08", and "GMT+8:00", and "GMT+08:0"
@@ -29,6 +29,7 @@ public void setUp() { | |||
public void execute_newClient_success() { | |||
Client validClient = new ClientBuilder().build(); | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra newline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small comments, but LGTM
VALID_TIMEZONES.remove("Z"); | ||
VALID_TIMEZONES.add("+00:00"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bump
@@ -41,11 +53,9 @@ | |||
public Timezone(String timezone) { | |||
requireNonNull(timezone); | |||
checkArgument(isValidTimezone(timezone), MESSAGE_CONSTRAINTS); | |||
String offsetString = timezone.substring(GMT_STRING.length()); | |||
int offsetValue = Integer.parseInt(offsetString); | |||
assert (VALID_TIMEZONES.contains(timezone.substring(UTC_STRING.length()))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert (VALID_TIMEZONES.contains(timezone.substring(UTC_STRING.length()))); | |
assert VALID_TIMEZONES.contains(timezone.substring(UTC_STRING.length())); |
btw what does this assert do? Isit rly needed lmao
Description
Timezone
class now references a set of valid timezones for validation. The format for parameters is nowGMT+HH:MM
and the list of valid timezones can be found here.Fixes #282
Testing
As per UserGuide:
client add n/Katya p/98123456 e/katya@yahoo.com a/Vladivostok, Nevelskogo, bld. 15, appt. 256 c/RU tz/GMT+03:00 ce/22-12-2020
client edit 3 c/JP tz/GMT+07:00
Also manual testing with
client view 1
and changing the timezones to verify they are of the correct timing.