Skip to content

Instantly share code, notes, and snippets.

@rgiaviti
Last active September 30, 2016 18:31
Show Gist options
  • Select an option

  • Save rgiaviti/d27646ae492d594f6cb1 to your computer and use it in GitHub Desktop.

Select an option

Save rgiaviti/d27646ae492d594f6cb1 to your computer and use it in GitHub Desktop.
This simple function converts LDAP Timestamp to Ruby DateTime object
# The MIT License (MIT)
#
# Copyright (c) 2016 Ricardo Giaviti
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
require 'date'
# Ruby Docs for DateTime object: http://ruby-doc.org/stdlib-2.2.3/libdoc/date/rdoc/DateTime.html
#
# LDAP Timestamp uses this format: <year><month><day><hour><minutes><seconds>.<tz>
# So, we just slice the string in correct positions to match the fields.
def ldap_timestamp_converter(ad_timestamp)
year = ad_timestamp[0..3].to_i
month = ad_timestamp[4..5].to_i
day = ad_timestamp[6..7].to_i
hours = ad_timestamp[8..9].to_i
minutes = ad_timestamp[10..11].to_i
seconds = ad_timestamp[12..13].to_i
timezone = '0'
DateTime.new(year, month, day, hours, minutes, seconds, timezone)
end
# Testing
ad_timestamp = '20151110110950.0Z'
date = ldap_timestamp2date(ad_timestamp)
puts date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment