@@ -372,9 +372,13 @@ def create_dim(dtype, values, full_domain, tile, **kwargs):
372372 if np .issubdtype (dtype , np .datetime64 ):
373373 date_unit = np .datetime_data (dtype )[0 ]
374374 dim_min = np .datetime64 (dtype_min , date_unit )
375- tile_max = np .iinfo (np .uint64 ).max - tile
376- if np .uint64 (dtype_max - dtype_min ) > tile_max :
377- dim_max = np .datetime64 (dtype_max - tile , date_unit )
375+ # Subtract in Python ints; a full-domain datetime64 difference
376+ # overflows int64.
377+ dtype_min_i = int (dtype_min .astype (np .int64 ))
378+ dtype_max_i = int (dtype_max .astype (np .int64 ))
379+ tile_max = int (np .iinfo (np .uint64 ).max ) - int (tile )
380+ if (dtype_max_i - dtype_min_i ) > tile_max :
381+ dim_max = np .datetime64 (dtype_max_i - int (tile ), date_unit )
378382 elif np .issubdtype (dtype , np .integer ):
379383 tile_max = np .iinfo (np .uint64 ).max - tile
380384 if np .uint64 (dtype_max - dtype_min ) > tile_max :
@@ -383,9 +387,14 @@ def create_dim(dtype, values, full_domain, tile, **kwargs):
383387 dim_min , dim_max = None , None
384388
385389 # TODO: simplify this logic and/or move to DataType.cast_tile_extent
386- if np .issubdtype (dtype , np .integer ) or np . issubdtype ( dtype , np . datetime64 ) :
390+ if np .issubdtype (dtype , np .integer ):
387391 # we can't make a tile larger than the dimension range or lower than 1
388392 tile = max (1 , min (tile , 1 + np .uint64 (dim_max - dim_min )))
393+ elif np .issubdtype (dtype , np .datetime64 ):
394+ # Clamp as for integers, computing the range in Python ints since a
395+ # full-domain datetime64 difference overflows int64.
396+ dim_range = int (dim_max .astype (np .int64 )) - int (dim_min .astype (np .int64 ))
397+ tile = max (1 , min (tile , 1 + dim_range ))
389398 elif np .issubdtype (dtype , np .floating ):
390399 # this difference can be inf
391400 with np .errstate (over = "ignore" ):
0 commit comments