Class: Jamf::Timestamp

Inherits:
Time show all
Defined in:
lib/jamf/api/jamf_pro/other_classes/timestamp.rb

Overview

A timestamp as used in the Jamf Pro API JSON data

Instantiate with any of:

- A string parsable by Time.parse. Timestamps from the API are always
  strings in iso6801 format
- a Time or Jamf::Timestamp instance
- an Integer, or Stringified Integer, a unix epoch value. If it is
  1_000_000_000_000 or higher, it is treated as a Jamf-stype epoch,
  meaning the last 3 digits are milliseconds.
- nil or an empty string, which will 'unset' a time value with an empty
  string when sent back to the API

To unset a timestamp value in the API, instantiate one of these with nil or an empty string. The Time value will be ‘1970-01-01 00:00:00 -0000’, the unix epoch, and the to_jamf method will return an empty string, which is what will be sent to the API

NOTE: Passing ‘1970-01-01 00:00:00 -0000’ or the equivalent explicitly will NOT be treated as an empty timestamp, but as that actual value. You must pass nil or an empty string to indicate an empty value

TODO: Find out: will an empty string work, e.g. in ext attrs with a DATE value, when used in criteria?

This class is a subclass of Time, so all Time methods are available.

  • use .to_i for a unix epoch in seconds

  • use .to_f for a unix epoch with fractions

Use #to_jamf to get the formated string to use in JSON for sending to the API - it should always be in ISO8601 format, or an empty string.

Constant Summary collapse

NIL_TIMESTAMP =

When we are unsetting a timestamp by intializing with nil, we still have to have a time object - so use the unix epoch

Time.at 0
J_EPOCH_INT_START =

Integers with this value or higher are a jamf-style epoch, meaning the first 10 digits are a unix epoch, and the last 3 are milliseconds. Integers below this shouldn’t appear, but will be treated as a regular unix epoch. (999_999_999_999 = 33658-09-27 01:46:39 UTC)

1_000_000_000_000
J_EPOCH_STR_LEN =

Stings containing integers of this length are a jamf-style epoch, meaning the first 10 digits are a unix epoch, and the last 3 are milliseconds. This length-test will be valid until the year 2286.

13

Instance Method Summary collapse

Constructor Details

#initialize(tstamp) ⇒ Timestamp

Returns a new instance of Timestamp.

Parameters:

  • tstamp (String, Integer, Time)

    A representation of a timestampe



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jamf/api/jamf_pro/other_classes/timestamp.rb', line 79

def initialize(tstamp)
  # use a Time object to parse the input and generate our own
  # object
  time = parse_init_tstamp(tstamp)

  super(
    time.year,
    time.month,
    time.day,
    time.hour,
    time.min,
    (time.sec + (time.usec / 1_000_000.0)).round(3),
    time.utc_offset
  )
end

Instance Method Details

#msecInteger

Returns the milliseconds of the Time.

Returns:

  • (Integer)

    the milliseconds of the Time



96
97
98
99
100
# File 'lib/jamf/api/jamf_pro/other_classes/timestamp.rb', line 96

def msec
  return 0 if @empty_timestamp

  (usec / 1000.0).round
end

#to_jamfString

Returns the timestamp formatted for passing to the API as a string.

Returns:

  • (String)

    the timestamp formatted for passing to the API as a string.



103
104
105
106
107
# File 'lib/jamf/api/jamf_pro/other_classes/timestamp.rb', line 103

def to_jamf
  return Jamf::BLANK if @empty_timestamp

  iso8601
end

#to_jamf_epochObject



109
110
111
# File 'lib/jamf/api/jamf_pro/other_classes/timestamp.rb', line 109

def to_jamf_epoch
  (to_f.round(3) * 1000).to_i
end