All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Li Wang <liwang@redhat.com>
Cc: Eirik Fuller <efuller@redhat.com>, ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2] rtc02: loosen the compare precision with few seconds
Date: Tue, 10 May 2022 16:08:54 +0200	[thread overview]
Message-ID: <Ynpx9suOjKrwggmy@yuki> (raw)
In-Reply-To: <20220508030524.2072035-1-liwang@redhat.com>

Hi!
>  static int rtc_tm_cmp(struct rtc_time *set_tm, struct rtc_time *read_tm)
>  {
> -	return !((set_tm->tm_sec == read_tm->tm_sec)
> -		&& (set_tm->tm_min == read_tm->tm_min)
> -		&& (set_tm->tm_hour == read_tm->tm_hour)
> -		&& (set_tm->tm_mday == read_tm->tm_mday)
> +	long delta, seconds1, seconds2;
> +
> +	/*
> +	 * Convert hour/min/sec into seconds to handle the normal
> +	 * and special situations:
> +	 * 1#
> +	 *       set_tm:  2022-04-28 13:00:50
> +	 *       read_tm: 2022-04-28 13:00:50
> +	 * 2#
> +	 *       set_tm:  2022-04-28 13:00:50
> +	 *       read_tm: 2022-04-28 13:00:51
> +	 * 3#
> +	 *       set_tm:  2022-04-28 13:00:59
> +	 *       read_tm: 2022-04-28 13:01:00
> +	 * 4#
> +	 *       set_tm:  2022-04-28 13:59:59
> +	 *       read_tm: 2022-04-28 14:00:00
> +	 *
> +	 * Note: as we have avoided testing around the zero
> +	 * clock, so it's impossible to hit situation 5#
> +	 *       set_tm:  2022-04-28 23:59:59
> +	 *       read_tm: 2022-04-29 00:00:00
> +	 */
> +	if (!(set_tm->tm_sec == read_tm->tm_sec)
> +		|| !(set_tm->tm_min == read_tm->tm_min)
> +		|| !(set_tm->tm_hour == read_tm->tm_hour)) {
> +
> +		seconds1 = (set_tm->tm_hour  * 3600) + (set_tm->tm_min  * 60) + set_tm->tm_sec;
> +		seconds2 = (read_tm->tm_hour * 3600) + (read_tm->tm_min * 60) + read_tm->tm_sec;
> +
> +		delta = seconds2 - seconds1;
> +
> +		if (delta < 0 || delta > 3)
> +			return 1;
> +	}
> +
> +	return !((set_tm->tm_mday == read_tm->tm_mday)
>  		&& (set_tm->tm_mon == read_tm->tm_mon)
>  		&& (set_tm->tm_year == read_tm->tm_year));
>  }

I would have done this a bit differently, first chek for day, mon, year
then do the calculation as:

	if (set_tm->tm_year != read_tm->tm_year)
		return 1;

	if (set_tm->tm_mon != read_tm->tm_mon)
		return 1;

	if (set_tm->tm_mday != read_tm->tm_mday)
		return 1;

	seconds1 = ....
	seconds2 = ....
	delta = ...

	if (delta < 0 || delta > 3)
		return 1;

	return 0;


I find this a bit clearer to read.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2022-05-10 14:07 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-28 13:26 [LTP] [PATCH 0/3] fix some bugs in rct02 Li Wang
2022-04-28 13:26 ` [LTP] [PATCH 1/3] rtc02: skip test with unsupport set RTC platform Li Wang
2022-04-29 11:07   ` Cyril Hrubis
2022-04-29 11:12     ` Li Wang
2022-04-28 13:26 ` [LTP] [PATCH 2/3] rtc02: reset backward 1 hour to RTC time Li Wang
2022-04-29 11:09   ` Cyril Hrubis
2022-04-28 13:26 ` [LTP] [PATCH 3/3] rtc02: loosen the compare precision with few seconds Li Wang
2022-05-05 14:05   ` Cyril Hrubis
2022-05-08  2:22     ` Li Wang
2022-05-08  3:05       ` [LTP] [PATCH v2] " Li Wang
2022-05-08  4:19         ` Li Wang
2022-05-10 14:08         ` Cyril Hrubis [this message]
2022-05-11  1:54           ` Li Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Ynpx9suOjKrwggmy@yuki \
    --to=chrubis@suse.cz \
    --cc=efuller@redhat.com \
    --cc=liwang@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.