Skip to content

Commit

Permalink
Implement returnage of DateTime object
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Dec 22, 2016
1 parent 1ba7957 commit 4d2db2c
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,4 +1,7 @@
Revision History for 'WWW-Google-Time' Perl 6 Distribution

1.002001 2016-12-22
- Added key with the DateTime object

1.001001 2015-12-07
- First version released on an unsuspecting world
2 changes: 1 addition & 1 deletion META6.json
@@ -1,7 +1,7 @@
{
"perl" : "6.c",
"name" : "WWW::Google::Time",
"version" : "1.001001",
"version" : "1.002001",
"description" : "Get time for various locations using Google",
"depends" : [ "HTTP::Tinyish", "URI::Escape" ],
"test-depends" : [ "Test" ],
Expand Down
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -23,6 +23,7 @@ WWW::Google::Time - Perl 6 module to get time for various locations using Google
Day of the month: %time<month-day>
Year: %time<year>
Full time string: %time<str>
DateTime object: %time<DateTime>
END

# Prints:
Expand All @@ -34,6 +35,7 @@ WWW::Google::Time - Perl 6 module to get time for various locations using Google
# Day of the month: 7
# Year: 2015
# Full time string: 9:31 AM EST, Monday, December 7, 2015
# DateTime object: 2016-12-22T09:31:48.651773-05:00
```

# DESCRIPTION
Expand Down Expand Up @@ -119,6 +121,23 @@ The year.
Convenience key that combines most of the above keys into a single
human-readable string.

### `DateTime`

```perl6
# DateTime object: 2016-12-22T09:31:48.651773-05:00
say "DateTime object: %time<DateTime>";
```

Contains a [`DateTime`](https://docs.perl6.org/type/DateTime) object,
representing the time. Since seconds are not available from Google, the seconds
in the object are set via `59.999 min DateTime.now.utc.second` and thus depend
on the local time of the machine the code is running on.

*Note*: the timezone offset is derived from a hardcoded map of timezone
abbreviation-to-offset and if the abbreviation cannot be decoded, offset of
0 is used. I have no guarantees that map includes every possible timezone
abbreviation Google may return.

# SEE ALSO

Google's Terms of Service: https://www.google.com/intl/en/policies/terms/
Expand Down
2 changes: 2 additions & 0 deletions examples/time.p6
Expand Up @@ -17,6 +17,7 @@ say qq:to/END/
Day of the month: %time<month-day>
Year: %time<year>
Full time string: %time<str>
DateTime object: %time<DateTime>
END

# Prints:
Expand All @@ -28,3 +29,4 @@ END
# Day of the month: 7
# Year: 2015
# Full time string: 9:31 AM EST, Monday, December 7, 2015
# DateTime object: 2015-12-07T09:31:13.182417Z
223 changes: 223 additions & 0 deletions lib/WWW/Google/Time.pm6
Expand Up @@ -2,6 +2,9 @@ use HTTP::Tinyish;
use URI::Escape;
unit package WWW::Google::Time:ver<1.001001>;

my %months = <DUMMY January February March April May June July August
September October November December>.antipairs;

sub google-time-in (Str:D $location) is export {
my %res = HTTP::Tinyish.new( agent => "Mozilla/5.0" ).get:
'http://google.com/search?num=100&hl=en&safe=off&btnG=Search'
Expand All @@ -26,5 +29,225 @@ sub google-time-in (Str:D $location) is export {
%time<where> ~~ s:g/'<em>' | '</em>' | '<b>' | '</b>'//;
%time<str> = "%time<time> %time<tz>, %time<week-day>, %time<month> "
~ "%time<month-day>, %time<year>";

my ($hour, $minute) = %time<time>.match(/(\d+) ':' (\d+)/).caps».value».Int;
%time<DateTime> = DateTime.new:
year => %time<year>,
month => %months{ %time<month> },
day => %time<month-day>,
hour => $hour + (12 if %time<time>.match: /«PM»/),
minute => $minute,
second => (59.999 min DateTime.now.utc.second),
timezone => (3600 * tz-offset-for %time<tz>).Int;

return %time;
};

sub tz-offset-for ($tz) {
state %tzs = <
ACDT 10.5
ACST 9.5
ACT -5
ADT -3
AEDT 11
AEST 10
AFT 4.5
AKDT -8
AKST -9
AMST -3
AMT -4
AMT 4
ART -3
AST 3
AST -4
AWST 8
AZOST 0
AZOT -1
AZT 4
BDT 8
BIOT 6
BIT -12
BOT -4
BRST -2
BRT -3
BST 6
BST 11
BST 1
BTT 6
CAT 2
CCT 6.5
CDT -5
CDT -4
CEST 2
CET 1
CHADT 13.75
CHAST 12.75
CHOT 8
CHOST 9
CHST 10
CHUT 10
CIST -8
CIT 8
CKT -10
CLST -3
CLT -4
COST -4
COT -5
CST -6
CST 8
ACST 9.5
ACDT 10.5
CST -5
CT 8
CVT -1
CWST 8.75
CXT 7
DAVT 7
DDUT 10
DFT 1
EASST -5
EAST -6
EAT 3
ECT -4
ECT -5
EDT -4
AEDT 11
EEST 3
EET 2
EGST 0
EGT -1
EIT 9
EST -5
AEST 10
FET 3
FJT 12
FKST -3
FKT -4
FNT -2
GALT -6
GAMT -9
GET 4
GFT -3
GILT 12
GIT -9
GMT 0
GST -2
GST 4
GYT -4
HADT -9
HAEC 2
HAST -10
HKT 8
HMT 5
HOVST 8
HOVT 7
ICT 7
IDT 3
IOT 3
IRDT 4.5
IRKT 8
IRST 3.5
IST 5.5
IST 1
IST 2
JST 9
KGT 6
KOST 11
KRAT 7
KST 9
LHST 10.5
LHST 11
LINT 14
MAGT 12
MART -9.5
MAWT 5
MDT -6
MET 1
MEST 2
MHT 12
MIST 11
MIT -9.5
MMT 6.5
MSK 3
MST 8
MST -7
MUT 4
MVT 5
MYT 8
NCT 11
NDT -2.5
NFT 11
NPT 5.75
NST -3.5
NT -3.5
NUT -11
NZDT 13
NZST 12
OMST 6
ORAT 5
PDT -7
PET -5
PETT 12
PGT 10
PHOT 13
PHT 8
PKT 5
PMDT -2
PMST -3
PONT 11
PST -8
PST 8
PYST -3
PYT -4
RET 4
ROTT -3
SAKT 11
SAMT 4
SAST 2
SBT 11
SCT 4
SGT 8
SLST 5.5
SRET 11
SRT -3
SST -11
SST 8
SYOT 3
TAHT -10
THA 7
TFT 5
TJT 5
TKT 13
TLT 9
TMT 5
TRT 3
TOT 13
TVT 12
ULAST 9
ULAT 8
USZ1 2
UTC 0
UYST -2
UYT -3
UZT 5
VET -4
VLAT 10
VOLT 4
VOST 6
VUT 11
WAKT 12
WAST 2
WAT 1
WEST 1
WET 0
WIT 7
WST 8
YAKT 9
YEKT 5
>;

if $tz ~~ /^ 'GMT' [ $<neg>='-' | '+' ] $<h>=\d+ [':' $<m>=\d+ ]?/ {
return ($<neg> ?? -1 !! 1) * ($<h> + ($<m>//0)/60);
}
(%tzs{ $tz } // 0).Numeric;
}
5 changes: 4 additions & 1 deletion t/01-time.t
Expand Up @@ -12,7 +12,7 @@ my %time = ( google-time-in 'Toronto' orelse do {
~ $_.exception.message
});

is %time.keys.elems, 8, 'Our time hash has 8 items in it';
is %time.keys.elems, 9, 'Our time hash has 9 items in it';

like %time<month>, /^[January|February|March|April|May|June|July|August|
September|October|November|December]$/, 'month looks right';
Expand All @@ -23,6 +23,9 @@ like %time<time>, /^ \d\d? ':' \d**2 ' ' [AM | PM] $/, 'time looks right';
like %time<tz>, /^ [EST | EDT] $/, 'timezone looks right';
like %time<where>, /'Toronto, ON'/, 'where looks right';
like %time<year>, /^20 \d**2 $/, 'year looks right';
isa-ok %time<DateTime>, DateTime, 'DateTime key is a DateTime object';
cmp-ok %time<DateTime>.year, '==', %time<year>,
'DateTime key seems to have correctish date time';

like %time<week-day>,
/^[Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday]$/,
Expand Down

0 comments on commit 4d2db2c

Please sign in to comment.