php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38938 more useful initialization for strptime
Submitted: 2006-09-24 00:21 UTC Modified: 2021-10-20 12:13 UTC
From: soletan at toxa dot de Assigned: cmb (profile)
Status: Wont fix Package: Date/time related
PHP Version: 5CVS-2006-09-23 (snap) OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
1 + 33 = ?
Subscribe to this entry?

 
 [2006-09-24 00:21 UTC] soletan at toxa dot de
Description:
------------
Hi,


in addition to bug #38524 I'd prefer initializing call to strptime in a more useful way. While it is easiest way to memset all with 0's, some fields like tm_mon can't be interpreted properly. 

My proposal uses 0xFF for initialization to get all values set -1.

Then in addition, I add array element value NULL on every field that wasn't touched by strptime. I consider this to be quite common behaviour in PHP ...

Below is my proposal for a patch on latest CVS snapshot.


Best Regards,

Thomas Urban


*** datetime.c.orig	2006-08-24 14:30:57.000000000 +0200
--- datetime.c	2006-09-24 02:16:09.000000000 +0200
***************
*** 101,107 ****
  		return;
  	}
  
! 	memset(&parsed_time, 0, sizeof(parsed_time));
  
  	unparsed_part = strptime(ts, format, &parsed_time);
  	if (unparsed_part == NULL) {
--- 101,107 ----
  		return;
  	}
  
! 	memset(&parsed_time, 0xFF, sizeof(parsed_time));
  
  	unparsed_part = strptime(ts, format, &parsed_time);
  	if (unparsed_part == NULL) {
***************
*** 109,122 ****
  	}
  
  	array_init(return_value);
! 	add_assoc_long(return_value, "tm_sec",   parsed_time.tm_sec);
! 	add_assoc_long(return_value, "tm_min",   parsed_time.tm_min);
! 	add_assoc_long(return_value, "tm_hour",  parsed_time.tm_hour);
! 	add_assoc_long(return_value, "tm_mday",  parsed_time.tm_mday);
! 	add_assoc_long(return_value, "tm_mon",   parsed_time.tm_mon);
! 	add_assoc_long(return_value, "tm_year",  parsed_time.tm_year);
! 	add_assoc_long(return_value, "tm_wday",  parsed_time.tm_wday);
! 	add_assoc_long(return_value, "tm_yday",  parsed_time.tm_yday);
  	add_assoc_string(return_value, "unparsed", unparsed_part, 1);
  }
  /* }}} */
--- 109,146 ----
  	}
  
  	array_init(return_value);
! 	if ( parsed_time.tm_sec < 0 )
! 		add_assoc_null(return_value, "tm_sec");
! 	else
! 		add_assoc_long(return_value, "tm_sec",   parsed_time.tm_sec);
! 	if ( parsed_time.tm_min < 0 )
! 		add_assoc_null(return_value, "tm_min");
! 	else
! 		add_assoc_long(return_value, "tm_min",   parsed_time.tm_min);
! 	if ( parsed_time.tm_hour < 0 )
! 		add_assoc_null(return_value, "tm_hour");
! 	else
! 		add_assoc_long(return_value, "tm_hour",  parsed_time.tm_hour);
! 	if ( parsed_time.tm_mday < 0 )
! 		add_assoc_null(return_value, "tm_mday");
! 	else
! 		add_assoc_long(return_value, "tm_mday",  parsed_time.tm_mday);
! 	if ( parsed_time.tm_mon < 0 )
! 		add_assoc_null(return_value, "tm_mon");
! 	else
! 		add_assoc_long(return_value, "tm_mon",   parsed_time.tm_mon);
! 	if ( parsed_time.tm_year < 0 )
! 		add_assoc_null(return_value, "tm_year");
! 	else
! 		add_assoc_long(return_value, "tm_year",  parsed_time.tm_year);
! 	if ( parsed_time.tm_wday < 0 )
! 		add_assoc_null(return_value, "tm_wday");
! 	else
! 		add_assoc_long(return_value, "tm_wday",  parsed_time.tm_wday);
! 	if ( parsed_time.tm_yday < 0 )
! 		add_assoc_null(return_value, "tm_yday");
! 	else
! 		add_assoc_long(return_value, "tm_yday",  parsed_time.tm_yday);
  	add_assoc_string(return_value, "unparsed", unparsed_part, 1);
  }
  /* }}} */



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-11 02:44 UTC] datibbaw@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2013-10-11 02:44 UTC] datibbaw@php.net
Doing this would cause a regression in test case ext/standard/tests/time/strptime_basic.phpt
 [2018-02-11 17:59 UTC] cmb@php.net
-Package: *General Issues +Package: Date/time related
 [2021-10-20 12:13 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-10-20 12:13 UTC] cmb@php.net
Given that strptime() is deprecated as of PHP 8.1.0 anyway, I
don't see the point in changing the behavior now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC