@@ -8,6 +8,7 @@ fn main() {
8
8
test_localtime_r_gmt ( ) ;
9
9
test_localtime_r_pst ( ) ;
10
10
test_localtime_r_epoch ( ) ;
11
+ test_localtime_r_verify_string_deduplication ( ) ;
11
12
// Architecture-specific tests.
12
13
#[ cfg( target_pointer_width = "32" ) ]
13
14
test_localtime_r_future_32b ( ) ;
@@ -125,7 +126,7 @@ fn test_localtime_r_gmt() {
125
126
fn test_localtime_r_pst ( ) {
126
127
let key = "TZ" ;
127
128
env:: set_var ( key, "PST8PDT" ) ;
128
- const TIME_SINCE_EPOCH : libc:: time_t = 1712475836 ; // 2024-04-07 07:43:56 GMT
129
+ const TIME_SINCE_EPOCH : libc:: time_t = 1712475836 ; // 2024-04-07 07:43:56 GMT
129
130
let custom_time_ptr = & TIME_SINCE_EPOCH ;
130
131
let mut tm = create_empty_tm ( ) ;
131
132
@@ -276,3 +277,52 @@ fn test_localtime_r_future_32b() {
276
277
assert ! ( ptr:: eq( res, & mut tm) ) ;
277
278
env:: remove_var ( key) ;
278
279
}
280
+
281
+ fn test_localtime_r_verify_string_deduplication ( ) {
282
+ let key = "TZ" ;
283
+ env:: set_var ( key, "PST8PDT" ) ;
284
+
285
+ // Two timestamps that are in the same timezone (PST/PDT).
286
+ const TIME_SINCE_EPOCH_TZ_CONST1 : libc:: time_t = 1712475836 ; // 2024-04-07 07:43:56 GMT
287
+ const TIME_SINCE_EPOCH_TZ_CONST2 : libc:: time_t = 1712575836 ; // 2024-04-08 11:23:56 GMT
288
+ const TIME_SINCE_EPOCH_TZ_CONST3 : libc:: time_t = 1712675836 ; // 2024-04-09 11:23:56 GMT
289
+
290
+ let mut tm1 = create_empty_tm ( ) ;
291
+ let mut tm2 = create_empty_tm ( ) ;
292
+ let mut tm3 = create_empty_tm ( ) ;
293
+
294
+ unsafe {
295
+ let res1 = libc:: localtime_r ( & TIME_SINCE_EPOCH_TZ_CONST1 , & mut tm1) ;
296
+ let res2 = libc:: localtime_r ( & TIME_SINCE_EPOCH_TZ_CONST2 , & mut tm2) ;
297
+ let res3 = libc:: localtime_r ( & TIME_SINCE_EPOCH_TZ_CONST3 , & mut tm3) ;
298
+
299
+ assert ! ( res1. is_null( ) == false , "localtime_r failed for first timestamp" ) ;
300
+ assert ! ( res2. is_null( ) == false , "localtime_r failed for second timestamp" ) ;
301
+ assert ! ( res3. is_null( ) == false , "localtime_r failed for third timestamp" ) ;
302
+
303
+ #[ cfg( any(
304
+ target_os = "linux" ,
305
+ target_os = "macos" ,
306
+ target_os = "freebsd" ,
307
+ target_os = "android"
308
+ ) ) ]
309
+ {
310
+ let tm_zone1 = std:: ffi:: CStr :: from_ptr ( tm1. tm_zone ) ;
311
+ let tm_zone2 = std:: ffi:: CStr :: from_ptr ( tm2. tm_zone ) ;
312
+
313
+ println ! ( "tz res1 :: {:#?}" , tm1. tm_zone) ;
314
+ println ! ( "tz res2 :: {:#?}" , tm2. tm_zone) ;
315
+ println ! ( "tz res3 :: {:#?}" , tm3. tm_zone) ;
316
+
317
+ assert_eq ! (
318
+ tm_zone1, tm_zone2,
319
+ "tm_zone strings are not equal, indicating different values."
320
+ ) ;
321
+
322
+ assert_eq ! (
323
+ tm1. tm_zone, tm2. tm_zone,
324
+ "tm_zone pointers are not equal, string deduplication is not happening."
325
+ ) ;
326
+ }
327
+ }
328
+ }
0 commit comments