date comparisson

Hi, first of all, many thanks, I thinkl some of your suggestions help me depploy (successfully) my open-source project, so MANY THANKS TO THE OPEN COMMUNITY!!!

today’s affair: I need to compare dates (by two), this is, I need to compare 2 dates and determine “which happened before” .
this is, I have to compare 2 variables and find out which one “happened earlier” than the other, this means, which one is bigger, but regarding dates it is more difficult.

I would probably need to:
1)transform these dates to “date variables” (as we transform strings to integers)
2) would need to find a method that compares these dates, in terms of “which happened earlier”. I am not sure I can do this by using “greater than” or “less than”.
thanks to everybody!!

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If you are using dates in a good format (per standards) you can just
compare strings:

2009-10-11 came after 2008-11-23 when comparing strings. We know that
because the format option is greater-than the latter regardless of how you
compare it (numerically or by string).

How are you getting your dates? In which format do you have the dates?
Which language are you using for getting the dates and which for
processing them? What is the purpose of knowing the date difference?

Good luck.

instaler wrote:
> Hi, first of all, many thanks, I thinkl some of your suggestions help me
> depploy (successfully) my open-source project, so MANY THANKS TO THE
> OPEN COMMUNITY!!!
>
> today’s affair: I need to compare dates (by two), this is, I need to
> compare 2 dates and determine “which happened before” .
> this is, I have to compare 2 variables and find out which one “happened
> earlier” than the other, this means, which one is bigger, but regarding
> dates it is more difficult.
>
> I would probably need to:
> 1)transform these dates to “date variables” (as we transform strings to
> integers)
> 2) would need to find a method that compares these dates, in terms of
> “which happened earlier”. I am not sure I can do this by using “greater
> than” or “less than”.
> thanks to everybody!!
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJK/G3XAAoJEF+XTK08PnB5w+oQALj35ZikXI6JuKevNa4eO+MQ
IKMw8f3UEqA9BZ2acYyKudAETN5pJMABO8VqrwpUB0eBoALlxi6uR81kv7uWIceg
vuIOeunAethtyupAb9jHBx+VkSNrL59i/gK+IWwnB27MXIKlSZOIrZGkC3M4HjB2
XqV6sEmr11FB3Go2E7TT1x3UxeiOuXrYC5Mr/klpTrU7k4nm9kjS3bZ/9O5Vn/7X
TxsJjPFG+AfK878fUJkL8LFbJsvtnzGUZMfGvICP8vdy3+TVm1uLQ41iM97jYxoK
17zjvmAaQKvbS+TgahI94Ygj6uDEjwlalZbeXePzu9qecAZo2tUn1miQoImtzRU3
sM9fz2K/p3LLhbHRGkiYRMkgBwyauqiHI7pGA9NAosSIS9LtFaRgYgeSMQsI04mh
FOjEGwr4as2/7bXG/IObwyaiHdAnnx8GtsWj/sv0RYKVKnKGwrYq0AwoueJVjwjb
EQPFT8Txh6dr8x3d3ZCvT4HXysOriX4p04MvkGSatWWEE9UctihcknZMIBrmBRmU
0CRmNnOZ5LWATcfjeyOy9xwviiIku6eaOkC2g+NoQNBJrvCh4DACYmQ3DTW4i9s/
RBl1XkQ8R35m1EmP4amh+avZXV7bCYzvlIGssuQGehvXRXtZJbh6TM3IlyzTWW1z
A+pGPrbIdszJhxUTYuj4
=tHK9
-----END PGP SIGNATURE-----

“We know that
because the format option is greater-than the latter regardless of how you
compare it (numerically or by string).” got a bit confused here but I think what you mean is “greater-than” works fine for dates, regardless of them being integers or strings.

Dates’ format: YYYY-MM-DD:HH:MM:SS, but I can change the format, no problem with that, only thing is I have to compare till seconds.

I will get the dat from a 2-column list, so I can use either “for-list” or use awk, think awk could be better.

I am using simple bash-unix language and I do not need to know the difference, only know which one is greter.
It is a simple script, just to read an Excel sheet with calls (telephonic central)made at different times of the day/month/year.

Thanks again!!

With awk it’s much faster if used correctly and I can help you if you can give me an example of how the line looks like( i understand that the second field is the date) and what you are comparing with.

I’ve used awk before and it is really great, yes, I just have to give it some thought. Anyway, here is part of the list:

2008-11-05 11:47:57 2008-11-05 11:51:39
2008-11-05 11:48:11 2008-11-05 11:55:41
2008-11-05 11:49:05 2008-11-05 12:01:28
2008-11-05 11:51:06 2008-11-05 11:53:24
2008-11-05 11:52:33 2008-11-05 11:54:42
2008-11-05 11:53:14 2008-11-05 11:56:07
2008-11-05 11:53:22 2008-11-05 11:54:41

and I can also change format before using the script
Thanks!!!

instaler wrote:
> I’ve used awk before and it is really great, yes, I just have to give it
> some thought. Anyway, here is part of the list:
>
> 2008-11-05 11:47:57 2008-11-05 11:51:39
> 2008-11-05 11:48:11 2008-11-05 11:55:41
> 2008-11-05 11:49:05 2008-11-05 12:01:28
> 2008-11-05 11:51:06 2008-11-05 11:53:24
> 2008-11-05 11:52:33 2008-11-05 11:54:42
> 2008-11-05 11:53:14 2008-11-05 11:56:07
> 2008-11-05 11:53:22 2008-11-05 11:54:41
>
> and I can also change format before using the script
> Thanks!!!

Convert the date to Unix time and compare.
$ $(date -d “2008-11-05 11:47:57” +%s) -lt $(date -d “2008-11-05 11:51:39” +%s) ]&& echo earlier || later
earlier

$ $(date -d “2008-11-05 11:51:39” +%s) -lt $(date -d “2008-11-05 11:47:57” +%s) ]&& echo earlier || echo later
later

Theo

Essentially you have 4 fields: date1 time1 date2 time2

Since all the fields are padded to full width, string comparison will yield the correct answer. In pseudo-code:

compare(date1,time1,date2,time2) {
  if (date1 > date2)
    return AFTER
  else if (date1 < date2)
    return BEFORE
  else {
     if (time1 > time2)
        return AFTER
     else if (time1 < time2)
        return BEFORE
     else
        return SAME
  }
}

I leave it to you to work out the actual code in awk, or whatever your preferred language is.


awk '
{
  d1=$1$2
  d2=$3$4
  gsub(/:|-/,"",d1)
  gsub(/:|-/,"",d2)
  print (d1 > d2)==1 ? d1" greater than "d2 : d2" greater than "d1
}

' file