Skip to content
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

tweak android date format #377

Merged
merged 4 commits into from
Jun 10, 2018
Merged

Conversation

KazuCocoa
Copy link
Member

@KazuCocoa KazuCocoa commented Jun 9, 2018

addressed : appium/appium#10832

I made sure the format worked with emulator OS 4, 5, 6, 7, 8 and real device with 6.

Wha do you think?


1|generic_x86:/ $ date --help
usage: date [-u] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-D SET_FORMAT] [SET]

Set/get the current date/time. With no SET shows the current date.

Default SET format is "MMDDhhmm[[CC]YY][.ss]", that's (2 digits each)
month, day, hour (0-23), and minute. Optionally century, year, and second.
Also accepts "@UNIXTIME[.FRACTION]" as seconds since midnight Jan 1 1970.

-d	Show DATE instead of current time (convert date format)
-D	+FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
-r	Use modification time of FILE instead of current date
-u	Use UTC instead of current timezone

+FORMAT specifies display format string using strftime(3) syntax:

%% literal %             %n newline              %t tab
%S seconds (00-60)       %M minute (00-59)       %m month (01-12)
%H hour (0-23)           %I hour (01-12)         %p AM/PM
%y short year (00-99)    %Y year                 %C century
%a short weekday name    %A weekday name         %u day of week (1-7, 1=mon)
%b short month name      %B month name           %Z timezone name
%j day of year (001-366) %d day of month (01-31) %e day of month ( 1-31)
%N nanosec (output only)

%U Week of year (0-53 start sunday)   %W Week of year (0-53 start monday)
%V Week of year (1-53 start monday, week < 4 days not part of this year)

%D = "%m/%d/%y"    %r = "%I : %M : %S %p"   %T = "%H:%M:%S"   %h = "%b"
%x locale date     %X locale time           %c locale date/time

@KazuCocoa
Copy link
Member Author

KazuCocoa commented Jun 9, 2018

In Ruby, both formats can parse as same, so this change doesn't affect Ruby client though.

# "Sat Jun  9 10:31:40 JST 2018"
Date.parse "Sat Jun  9 10:31:40 JST 2018"
=> #<Date: 2018-06-09 ((2458279j,0s,0n),+0s,2299161j)>

# "Sat Jun 09 2018 10:30:13 GMT+0900 (JST)"
Date.parse "Sat Jun 09 2018 10:30:13 GMT+0900 (JST)"
=> #<Date: 2018-06-09 ((2458279j,0s,0n),+0s,2299161j)>

In JavaScript, Date.parse("Sat Jun 9 10:31:40 JST 2018") failed to parse

@mykola-mokhnach
Copy link
Contributor

mykola-mokhnach commented Jun 9, 2018

The initial proposal was to be able to provide a custom format string as function argument, so we could alter this is base driver first. Also, I'd love to see the default format as iso-8601, since its parsing is for sure supported by the majority of the corresponding clients.

@KazuCocoa
Copy link
Member Author

yeah.
Android returns "Sat Jun 9 10:31:40 JST 2018" by default and JS can't parse the string like new Date("Sat Jun 9 10:31:40 JST 2018") nor Date.parse. So, I customised shell date format first to make the return value manageable by JS. (And I thought the format we could get by new Data() was the most JS friendly)
I'll update the format to iso-8601, anyway.

@@ -28,8 +28,8 @@ commands.doSendKeys = async function (params) {
commands.getDeviceTime = async function () {
log.info('Attempting to capture android device date and time');
try {
// format: Sat Jun 09 2018 11:17:42 GTM+0900 (JST)
let out = await this.adb.shell(['date', '\"+%a\ %b\ %d\ %Y\ %X\ GTM%z\ \(%Z\)\"']);
// format: 2018-06-09T16:21:54+0900
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it probably makes sense to mention this in the docstring, that the resulting date string format conforms to ISO-8601 by default.

@mykola-mokhnach
Copy link
Contributor

@KazuCocoa Would you like to include the optional format argument into this PR or create a new one?

@KazuCocoa
Copy link
Member Author

I'd like to create another PR for the option to keep change small.

And I'll add docstring later 👍

/**
* Get device time
* @return {String} Return value following iso-8601 format like `2018-06-09T16:21:54+0900`
* @throws {Error} If the adb command doesn't return device date, error throes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is enough to keep "If the adb command doesn't return a device date" part

commands.getDeviceTime = async function () {
log.info('Attempting to capture android device date and time');
try {
let out = await this.adb.shell(['date']);
let out = await this.adb.shell(['date', '+%Y-%m-%dT%T%z']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (await this.adb.shell(['date', '+%Y-%m-%dT%T%z'])).trim()

@KazuCocoa
Copy link
Member Author

thanks for the review!

@KazuCocoa KazuCocoa merged commit 647051d into appium:master Jun 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants