Skip to content

Commit 802eb9a

Browse files
authored
Merge pull request #1417 from alexjfisher/deprecate_time
Deprecate `time` function
2 parents 7e7ded4 + fea0881 commit 802eb9a

File tree

5 files changed

+56
-109
lines changed

5 files changed

+56
-109
lines changed

REFERENCE.md

+27-45
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ from an array or key from a hash.
4242
* [`delete_undef_values`](#delete_undef_values): Returns a copy of input hash or array with all undefs deleted.
4343
* [`delete_values`](#delete_values): Deletes all instances of a given value from a hash.
4444
* [`deprecation`](#deprecation): Function to print deprecation warnings, Logs a warning once for a given key.
45-
* [`deprecation`](#deprecation): Function to print deprecation warnings (this is the 3.X version of it).
4645
* [`difference`](#difference): This function returns the difference between two arrays.
4746
* [`dirname`](#dirname): Returns the dirname of a path.
4847
* [`dos2unix`](#dos2unix): Returns the Unix version of the given string.
@@ -142,6 +141,7 @@ Puppet structure
142141
* [`stdlib::shell_escape`](#stdlib--shell_escape): Escapes a string so that it can be safely used in a Bourne shell command line.
143142
* [`stdlib::start_with`](#stdlib--start_with): Returns true if str starts with one of the prefixes given. Each of the prefixes should be a String.
144143
* [`stdlib::str2resource`](#stdlib--str2resource): This converts a string to a puppet resource.
144+
* [`stdlib::time`](#stdlib--time): This function is deprecated. It implements the functionality of the original non-namespaced stdlib `time` function.
145145
* [`stdlib::to_json`](#stdlib--to_json): Convert a data structure and output to JSON
146146
* [`stdlib::to_json_pretty`](#stdlib--to_json_pretty): Convert data structure and output to pretty JSON
147147
* [`stdlib::to_python`](#stdlib--to_python): Convert an object into a String containing its Python representation
@@ -161,7 +161,7 @@ OS X versions >= 10.7).
161161
* [`suffix`](#suffix): This function applies a suffix to all elements in an array, or to the keys
162162
in a hash.
163163
* [`swapcase`](#swapcase): This function will swap the existing case of a string.
164-
* [`time`](#time): This function will return the current time since epoch as an integer.
164+
* [`time`](#time): DEPRECATED. Use the native Puppet fuctionality instead of this function. eg `Integer(Timestamp().strftime('%s'))`
165165
* [`to_bytes`](#to_bytes): Converts the argument into bytes, for example 4 kB becomes 4096.
166166
* [`to_json`](#to_json): DEPRECATED. Use the namespaced function [`stdlib::to_json`](#stdlibto_json) instead.
167167
* [`to_json_pretty`](#to_json_pretty): DEPRECATED. Use the namespaced function [`stdlib::to_json_pretty`](#stdlibto_json_pretty) instead.
@@ -1480,20 +1480,6 @@ When `true`, (the default), the function is affected by the puppet setting 'stri
14801480
(outputs as an error message), :off (no message / error is displayed) and :warning
14811481
(default, outputs a warning).
14821482

1483-
### <a name="deprecation"></a>`deprecation`
1484-
1485-
Type: Ruby 3.x API
1486-
1487-
The uniqueness key - can appear once. The msg is the message text including any positional
1488-
information that is formatted by the user/caller of the method.).
1489-
1490-
#### `deprecation()`
1491-
1492-
The uniqueness key - can appear once. The msg is the message text including any positional
1493-
information that is formatted by the user/caller of the method.).
1494-
1495-
Returns: `String` return deprecation warnings
1496-
14971483
### <a name="difference"></a>`difference`
14981484

14991485
Type: Ruby 3.x API
@@ -4111,6 +4097,24 @@ Data type: `String`
41114097

41124098
The string to lookup as a resource
41134099

4100+
### <a name="stdlib--time"></a>`stdlib::time`
4101+
4102+
Type: Puppet Language
4103+
4104+
It is provided for compatability, but users should use the native time related functions directly.
4105+
4106+
#### `stdlib::time(Optional[String] $_timezone = undef)`
4107+
4108+
It is provided for compatability, but users should use the native time related functions directly.
4109+
4110+
Returns: `Integer`
4111+
4112+
##### `_timezone`
4113+
4114+
Data type: `Optional[String]`
4115+
4116+
This parameter doesn't do anything, but exists for compatability reasons
4117+
41144118
### <a name="stdlib--to_json"></a>`stdlib::to_json`
41154119

41164120
Type: Ruby 4.x API
@@ -4876,43 +4880,21 @@ Would result in: "AbCd"
48764880

48774881
### <a name="time"></a>`time`
48784882

4879-
Type: Ruby 3.x API
4880-
4881-
> *Note:* that since Puppet 4.8.0 the Puppet language has the data types Timestamp (a point in time) and
4882-
Timespan (a duration). The following example is equivalent to calling time() without
4883-
any arguments:
4884-
4885-
```Timestamp()```
4886-
4887-
#### Examples
4888-
4889-
##### **Usage**
4890-
4891-
```puppet
4892-
4893-
time()
4894-
Will return something like: 1311972653
4895-
```
4883+
Type: Ruby 4.x API
48964884

4897-
#### `time()`
4885+
DEPRECATED. Use the native Puppet fuctionality instead of this function. eg `Integer(Timestamp().strftime('%s'))`
48984886

4899-
> *Note:* that since Puppet 4.8.0 the Puppet language has the data types Timestamp (a point in time) and
4900-
Timespan (a duration). The following example is equivalent to calling time() without
4901-
any arguments:
4887+
#### `time(Any *$args)`
49024888

4903-
```Timestamp()```
4889+
The time function.
49044890

4905-
Returns: `Any` the current time since epoch as an integer.
4891+
Returns: `Any`
49064892

4907-
##### Examples
4893+
##### `*args`
49084894

4909-
###### **Usage**
4895+
Data type: `Any`
49104896

4911-
```puppet
49124897

4913-
time()
4914-
Will return something like: 1311972653
4915-
```
49164898

49174899
### <a name="to_bytes"></a>`to_bytes`
49184900

functions/time.pp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @summary This function is deprecated. It implements the functionality of the original non-namespaced stdlib `time` function.
2+
#
3+
# It is provided for compatability, but users should use the native time related functions directly.
4+
#
5+
# @param _timezone
6+
# This parameter doesn't do anything, but exists for compatability reasons
7+
function stdlib::time(Optional[String] $_timezone = undef) >> Integer {
8+
# Note the `timezone` parameter doesn't do anything and didn't in the ruby implementation for _years_ (pre 1.8.7 perhaps ???)
9+
deprecation('time', 'The stdlib `time` function is deprecated. Please direcly use native Puppet functionality instead. eg. `Integer(Timestamp().strftime(\'%s\'))`', false)
10+
Integer(Timestamp().strftime('%s'))
11+
}

lib/puppet/functions/time.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
# @summary DEPRECATED. Use the native Puppet fuctionality instead of this function. eg `Integer(Timestamp().strftime('%s'))`
4+
Puppet::Functions.create_function(:time) do
5+
dispatch :call_puppet_function do
6+
repeated_param 'Any', :args
7+
end
8+
def call_puppet_function(*args)
9+
# Note, `stdlib::time` calls `deprecation`, so we don't also do that here.
10+
call_function('stdlib::time', *args)
11+
end
12+
end

lib/puppet/parser/functions/time.rb

-57
This file was deleted.

spec/functions/time_spec.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44

55
describe 'time' do
66
it { is_expected.not_to be_nil }
7-
it { is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
87

98
context 'when running at a specific time' do
109
before(:each) do
1110
# get a value before stubbing the function
1211
test_time = Time.utc(2006, 10, 13, 8, 15, 11)
13-
allow(Time).to receive(:new).with(no_args).and_return(test_time).once
12+
allow(Time).to receive(:now).and_return(test_time)
1413
end
1514

1615
it { is_expected.to run.with_params.and_return(1_160_727_311) }
1716
it { is_expected.to run.with_params('').and_return(1_160_727_311) }
18-
it { is_expected.to run.with_params([]).and_return(1_160_727_311) }
19-
it { is_expected.to run.with_params({}).and_return(1_160_727_311) }
20-
it { is_expected.to run.with_params('foo').and_return(1_160_727_311) }
21-
it { is_expected.to run.with_params('UTC').and_return(1_160_727_311) }
22-
it { is_expected.to run.with_params('America/New_York').and_return(1_160_727_311) }
17+
18+
describe('Timezone is irrelevant') do
19+
it { is_expected.to run.with_params('UTC').and_return(1_160_727_311) }
20+
it { is_expected.to run.with_params('America/New_York').and_return(1_160_727_311) }
21+
end
2322
end
2423
end

0 commit comments

Comments
 (0)