Class: Jamf::Timestamp

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

Overview

A timestamp as used in the JAMF API JSON data

Instantiate with a String in iso6801 format (used in the API JSON for all(?) time/date values), or with a Time or Jamf::Timestamp instance, or with an Integer unix epoch, which is treated Jamf-style if 1_000_000_000_000 or higher

To unset a timestamp value, instantiate 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.

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

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, **_args) ⇒ Timestamp

Returns a new instance of Timestamp.

Parameters:

  • tstamp (String, Integer, Time)

    A representation of a timestampe

  • _args (void)

    unused, but required for JSONObject init.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jamf/api/attribute_classes/timestamp.rb', line 75

def initialize(tstamp, **_args)
  # 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



92
93
94
95
96
# File 'lib/jamf/api/attribute_classes/timestamp.rb', line 92

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.



99
100
101
102
103
# File 'lib/jamf/api/attribute_classes/timestamp.rb', line 99

def to_jamf
  return Jamf::BLANK if @empty_timestamp

  iso8601
end

#to_jamf_epochObject



105
106
107
# File 'lib/jamf/api/attribute_classes/timestamp.rb', line 105

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