Skip to content

-daystart option does not calculate midnight with local time zone #466

Closed

Description

@NaviHX

When using the -daystart option with the find command, today's midnight is incorrect for users whose local time zone differs from UTC. Specifically, instead of considering the local time zone, the implementation calculates midnight by using the following code:

let midnight_seconds = seconds - (seconds % 86400);
// eprintln!("Midnight seconds: {}", midnight_seconds);

This code causes the calculated midnight_seconds is always based on UNIX_EPOCH (00:00 +0000). However, UNIX_EPOCH is not midnight in other time zones. For example, run find -mtime 0 at Wed, 16 Oct 2024 14:27:57 +0800 and the above code will output:

Midnight seconds: 1729036800
# > date -d "@1729036800" -R
# Wed, 16 Oct 2024 08:00:00 +0800

Activity

  1. NaviHX commented on Oct 17, 2024

    @NaviHX
    ContributorAuthor

    I have a workaround here:

    dce8b2d...fcb8da2

     fn get_time(matcher_io: &mut MatcherIO, today_start: bool) -> SystemTime {
         if today_start {
             // the time at 00:00:00 of today
    -        let duration = matcher_io.now().duration_since(UNIX_EPOCH).unwrap();
    -        let seconds = duration.as_secs();
    -        let midnight_seconds = seconds - (seconds % 86400);
    -        UNIX_EPOCH + Duration::from_secs(midnight_seconds)
    +        let duration_since_unix_epoch = matcher_io.now().duration_since(UNIX_EPOCH).unwrap();
    +        let seconds_since_unix_epoch = duration_since_unix_epoch.as_secs();
    +        let utc_time = DateTime::from_timestamp(seconds_since_unix_epoch as i64, 0).unwrap();
    +        let local_time = utc_time.with_timezone(&Local);
    +        let local_midnight = local_time.date().and_hms(0, 0, 0);
    +        let local_midnight_seconds = local_midnight.timestamp();
    +
    +        UNIX_EPOCH + Duration::from_secs(local_midnight_seconds as u64)
         } else {
             matcher_io.now()
         }
    
  2. sylvestre commented on Oct 17, 2024

    @sylvestre
    SponsorContributor

    please submit a PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions