Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting string var to clock var, where seconds don't agree

    I am trying to compare two times, measured in YMD, hms format, to see if they are the same.

    As the times are listed as string variables, I first have to convert them to clock variables. However, I convert the string to a clock variable, the string and the clock variable are not providing the same seconds value. Please see the code I am using at the end of post, and note that "time1" and "rtime1" do not agree in terms of seconds. (I also cannot change how the dates are being inputed, as they have been given to me as string variables).

    A secondary problem I am having is converting the clock variable to a date format so that I can compare dates (I want to know if "time1"="time2"), but first I would like to know why "time1" and "rtime1" do not agree.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str19(time1 time2)
    "2020-12-13 02:58:57" "2020-12-13 03:20:51"
    "2020-12-13 02:58:58" "2020-12-13 03:20:53"
    "2020-12-13 15:10:16" "2020-12-13 15:51:29"
    "2020-12-13 15:10:43" "2020-12-13 15:51:24"
    "2020-12-13 15:16:09" "2020-12-13 15:51:15"
    "2020-12-13 15:39:52" "2020-12-13 16:11:05"
    "2020-12-13 15:49:06" "2020-12-13 16:25:31"
    "2020-12-13 16:38:09" "2020-12-13 17:09:48"
    "2020-12-13 16:42:47" "2020-12-13 17:12:52"
    "2020-12-13 16:42:49" "2020-12-13 17:12:55"
    "2020-12-13 16:42:50" "2020-12-13 17:12:58"
    "2020-12-13 16:45:41" "2020-12-13 17:15:03"
    "2020-12-13 16:54:41" "2020-12-13 17:43:29"
    "2020-12-13 17:09:09" "2020-12-13 17:44:02"
    "2020-12-13 17:17:23" "2020-12-13 18:01:57"
    "2020-12-13 17:17:57" "2020-12-13 18:01:52"
    "2020-12-13 17:23:47" "2020-12-13 18:01:48"
    "2020-12-13 17:36:47" "2020-12-13 18:27:42"
    "2020-12-13 17:41:09" "2020-12-13 19:01:47"
    "2020-12-13 18:19:16" "2020-12-13 19:09:43"
    "2020-12-13 18:27:13" "2020-12-13 19:09:34"
    "2020-12-13 18:27:14" "2020-12-13 19:09:40"
    "2020-12-13 18:32:01" "2020-12-13 19:09:38"
    "2020-12-13 18:56:54" "2020-12-13 20:00:49"
    "2020-12-13 18:56:57" "2020-12-13 20:06:45"
    "2020-12-13 18:56:49" "2020-12-13 20:02:07"
    "2020-12-13 18:57:34" "2020-12-13 20:14:05"
    "2020-12-13 19:20:09" "2020-12-13 19:54:11"
    "2020-12-13 19:20:10" "2020-12-13 19:54:14"
    "2020-12-13 19:20:12" "2020-12-13 19:54:19"
    "2020-12-13 19:21:54" "2020-12-13 19:54:17"
    "2020-12-13 19:59:40" "2020-12-13 22:07:44"
    "2020-12-13 19:53:12" "2020-12-13 20:43:48"
    "2020-12-13 20:05:28" "2020-12-13 21:49:55"
    "2020-12-13 20:08:00" "2020-12-13 21:10:58"
    "2020-12-13 20:08:03" "2020-12-13 21:10:51"
    "2020-12-13 20:11:51" "2020-12-13 21:51:23"
    "2020-12-13 20:12:49" "2020-12-13 22:06:50"
    "2020-12-13 20:12:14" "2020-12-13 22:08:10"
    "2020-12-13 20:13:03" "2020-12-13 22:10:38"
    "2020-12-13 20:13:41" "2020-12-13 22:12:46"
    "2020-12-13 20:14:27" "2020-12-13 21:10:46"
    "2020-12-13 20:55:04" "2020-12-13 21:20:47"
    "2020-12-13 21:04:09" "2020-12-13 21:27:16"
    "2020-12-13 21:23:08" "2020-12-13 22:03:05"
    "2020-12-13 21:23:11" "2020-12-13 22:03:03"
    "2020-12-13 21:23:13" "2020-12-13 22:02:59"
    "2020-12-13 21:23:15" "2020-12-13 22:02:56"
    "2020-12-13 21:23:19" "2020-12-13 22:02:52"
    end
    gen rtime1=clock(time1,"YMDhms")
    format rtime1 %tC

    . list *time1 in 1/5, noobs separator(0)


    +------------------------------------------+
    | time1 rtime1 |
    |------------------------------------------|
    1. | 2020-12-13 02:58:57 13dec2020 02:58:14 |
    2. | 2020-12-13 02:58:58 13dec2020 02:58:14 |
    3. | 2020-12-13 15:10:16 13dec2020 15:10:03 |
    4. | 2020-12-13 15:10:43 13dec2020 15:10:03 |
    5. | 2020-12-13 15:16:09 13dec2020 15:16:36 |
    +------------------------------------------+



    Thank you for any assistance.

    Best,
    Christina

  • #2
    You need to specify that the generated variable is in double format because that's the required precision for datetime/c variables:

    Code:
    gen double doublertime1=clock(time1,"YMDhms")
    gen rtime1=clock(time1,"YMDhms")
    format rtime1 doublertime1 %tc
    list time1 doublertime1 rtime1 in 1/5,noobs divider sepby()
    
      +---------------------------------------------------------------+
      |               time1 |       doublertime1 |             rtime1 |
      |---------------------+--------------------+--------------------|
      | 2020-12-13 02:58:57 | 13dec2020 02:58:57 | 13dec2020 02:58:41 |
      | 2020-12-13 02:58:58 | 13dec2020 02:58:58 | 13dec2020 02:58:41 |
      | 2020-12-13 15:10:16 | 13dec2020 15:10:16 | 13dec2020 15:10:30 |
      | 2020-12-13 15:10:43 | 13dec2020 15:10:43 | 13dec2020 15:10:30 |
      | 2020-12-13 15:16:09 | 13dec2020 15:16:09 | 13dec2020 15:17:03 |
      +---------------------------------------------------------------+
    Last edited by Ali Atia; 07 Jan 2021, 07:49.

    Comment


    • #3
      Thank you for the suggestion, but that does not solve the problem. The seconds are still listed differently.

      gen double rtime1=clock(time1,"YMDhms")
      format rtime1 %tC


      . list *time1 in 1/5, separator(0)

      +------------------------------------------+
      | time1 rtime1 |
      |------------------------------------------|
      1. | 2020-12-13 02:58:57 13dec2020 02:58:30 |
      2. | 2020-12-13 02:58:58 13dec2020 02:58:31 |
      3. | 2020-12-13 15:10:16 13dec2020 15:09:49 |
      4. | 2020-12-13 15:10:43 13dec2020 15:10:16 |
      5. | 2020-12-13 15:16:09 13dec2020 15:15:42 |
      +------------------------------------------+





      Comment


      • #4
        Try formatting as %tc instead of %tC.

        Comment


        • #5
          Thank you - that worked. I don't understand why using %tc instead of %tC made such a difference, but I appreciate the help.

          Comment


          • #6
            %tc ignores leap seconds and assumes 86400 seconds per day, whereas %tC accounts for leap seconds. See this helpfile for more details: https://www.stata.com/manuals/ddatetimetranslation.pdf.

            Comment

            Working...
            X