Skip to content

Commit e912db7

Browse files
committed
PHPC-552: Implement ObjectID::getTimestamp() method
1 parent 49cb39f commit e912db7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/BSON/ObjectID.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ PHP_METHOD(ObjectID, __construct)
134134
}
135135
/* }}} */
136136

137+
/* {{{ proto integer ObjectID::getTimestamp()
138+
*/
139+
PHP_METHOD(ObjectID, getTimestamp)
140+
{
141+
php_phongo_objectid_t *intern;
142+
bson_oid_t tmp_oid;
143+
144+
intern = Z_OBJECTID_OBJ_P(getThis());
145+
146+
if (zend_parse_parameters_none() == FAILURE) {
147+
return;
148+
}
149+
150+
bson_oid_init_from_string(&tmp_oid, intern->oid);
151+
RETVAL_LONG(bson_oid_get_time_t(&tmp_oid));
152+
}
153+
/* }}} */
154+
137155
/* {{{ proto ObjectID::__set_state(array $properties)
138156
*/
139157
PHP_METHOD(ObjectID, __set_state)
@@ -206,6 +224,7 @@ ZEND_END_ARG_INFO()
206224

207225
static zend_function_entry php_phongo_objectid_me[] = {
208226
PHP_ME(ObjectID, __construct, ai_ObjectID___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
227+
PHP_ME(ObjectID, getTimestamp, ai_ObjectID_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
209228
PHP_ME(ObjectID, __set_state, ai_ObjectID___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
210229
PHP_ME(ObjectID, __toString, ai_ObjectID_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
211230
PHP_ME(ObjectID, __wakeup, ai_ObjectID_void, ZEND_ACC_PUBLIC)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectID::getTimestamp
3+
--FILE--
4+
<?php
5+
date_default_timezone_set( "UTC" );
6+
7+
$objectid = new MongoDB\BSON\ObjectID('564b07d6619d4b3d668b45e3');
8+
$ts = $objectid->getTimestamp();
9+
echo $ts, "\n";
10+
echo date_create( "@{$ts}" )->format( "Y-m-d H:i:s" ), "\n";
11+
?>
12+
--EXPECTF--
13+
1447757782
14+
2015-11-17 10:56:22

0 commit comments

Comments
 (0)