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

Add Timezone Support fol WFE not using UTC #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ site = 'https://myspsite/site_a/Default.aspx'
scli = Viewpoint::SPWSClient.new(site)
# Otherwise you can specify a user/pass
scli = Viewpoint::SPWSClient.new(site, user, pass)
# You can also specify a timezone (more on this below)
scli = Viewpoint::SPWSClient.new(site, user, pass, 'Australia/Melbourne')
```

### Getting Lists
Expand Down Expand Up @@ -68,6 +70,11 @@ doclib = scli.get_list 'Personal Documents'
doclib.add_file! :file => '/path/to/file'
```

## Timezones
SPWS servers return times in the zone the Sharepoint Web Front End (WFE) is configured in. For example, say I have a Sharepoint WFE located in Perth, Australia (GMT+8, no DST), with its system time set to local time. If Sharepoint wishes to express the actual time `2014-01-01T00:00:00Z`, the string `2014-01-01T08:00:00Z` will be returned by the web service, which is incorrect unless the timezone fragment is ignored.

To work around this, a timezone can be specified while creating the SPWS client object. This will cause the times returned by SharePoint to be converted correctly.

### My Links
- [Twitter | https://twitter.com/zentourist](https://twitter.com/#!/zentourist)
- [BLOG | http://distributed-frostbite.blogspot.com/](http://distributed-frostbite.blogspot.com/)
Expand Down
1 change: 1 addition & 0 deletions lib/viewpoint/spws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require 'nokogiri'
require 'logging'
require 'pathname'
require 'tzinfo'

# This is the base module for all other classes
module Viewpoint
Expand Down
7 changes: 4 additions & 3 deletions lib/viewpoint/spws/connection.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
class Viewpoint::SPWS::Connection
include Viewpoint::SPWS

attr_reader :site_base
attr_reader :site_base, :server_timezone
# @param [String] site_base the base URL of the site not including the
# web service part.
# @example https://<site>/mysite/<default.aspx>
def initialize(site_base)
# @param [TZInfo::Timezone] server_tz Timezone Sharepoint WFE is set to.
def initialize(site_base, server_tz = nil)
@log = Logging.logger[self.class.name.to_s.to_sym]
@httpcli = HTTPClient.new
# Up the keep-alive so we don't have to do the NTLM dance as often.
@httpcli.keep_alive_timeout = 60
@site_base = URI.parse(normalize_site_name(site_base))
@server_timezone = server_tz
end

def set_auth(user,pass)
Expand Down Expand Up @@ -40,7 +42,6 @@ def post(websvc, xmldoc)
check_response( @httpcli.post(url, xmldoc, headers) )
end


private


Expand Down
10 changes: 7 additions & 3 deletions lib/viewpoint/spws/spws_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@
class Viewpoint::SPWSClient
include Viewpoint::SPWS

attr_reader :server_timezone

# Initialize the SPWSClient instance.
# @param [String] endpoint The SPWS endpoint we will be connecting to
# @param [String] user The user to authenticate as. If you are using
# NTLM or Negotiate authentication you do not need to pass this parameter.
# @param [String] pass The user password. If you are using NTLM or
# Negotiate authentication you do not need to pass this parameter.
def initialize(endpoint, user = nil, pass = nil)
@con = Connection.new(endpoint)
# @param [Boolean] server_tz Set this to a string representing the
# time zone the sharepoint WFE server is set to, e.g. 'Australia/Melbourne'
def initialize(endpoint, user = nil, pass = nil, server_tz = nil)
@server_timezone = server_tz ? TZInfo::Timezone.get(server_tz) : nil
@con = Connection.new(endpoint, @server_timezone)
@con.set_auth(user,pass) if(user && pass)
end

Expand All @@ -43,7 +48,6 @@ def usergroup_ws
@usergroupws ||= Websvc::UserGroup.new(@con)
end


# ========= List Accessor Proxy Methods =========

# Available list types that can be used for #add_list
Expand Down
6 changes: 3 additions & 3 deletions lib/viewpoint/spws/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def initialize(ws, xml)
@title = xml['Title']
@description = xml['Description']
@hidden = (xml['Hidden'] == 'True')
@created = DateTime.parse(xml['Created'])
@modified = DateTime.parse(xml['Modified'])
@last_deleted = DateTime.parse(xml['LastDeleted'])
@created = @ws.parse_time(xml['Created'])
@modified = @ws.parse_time(xml['Modified'])
@last_deleted = @ws.parse_time(xml['LastDeleted'])
@item_count = xml['ItemCount']
@server_template= xml['ServerTemplate'].to_i
@feature_id = xml['FeatureId']
Expand Down
2 changes: 1 addition & 1 deletion lib/viewpoint/spws/types/list_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def set_field(vname, fname, mapsrc = @xmldoc)
def transform(newvar)
case newvar
when /[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}/ # Transform DateTime
return DateTime.parse(newvar)
return @ws.parse_time(newvar)
else
return newvar
end
Expand Down
2 changes: 1 addition & 1 deletion lib/viewpoint/spws/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Viewpoint
module SPWS
VERSION = "0.5.0"
VERSION = "0.6.0"
end
end
14 changes: 13 additions & 1 deletion lib/viewpoint/spws/websvc/web_service_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@ module WebServiceBase
attr_reader :spcon

# @param [Viewpoint::SPWS::Connection] spcon A connection to a Sharepoint Site
def initialize(spcon)
# @param [TZInfo::Timezone] server_tz Server timezone of Sharepoint WFE
def initialize(spcon, server_tz = nil)
@server_timezone = server_tz
@log = Logging.logger[self.class.name.to_s.to_sym]
@spcon = spcon
raise "Auth failure" unless(@spcon.authenticate(@ws_endpoint))
end

def server_timezone
spcon.server_timezone
end

def parse_time(str)
datetime = DateTime.parse(str)
datetime = server_timezone.local_to_utc(datetime) if server_timezone
datetime
end

private

def build_soap_envelope
Expand Down