none
请问YYYY[,DDD,HH,MM,SS.FFFFFF]这个字符串格式用c#怎么表示啊? RRS feed

  • 问题

  • /***************************************************************************
     * ms_seedtimestr2hptime:
     *
     * Convert a SEED time string (day-of-year style) to a high precision
     * epoch time.  The time format expected is
     * "YYYY[,DDD,HH,MM,SS.FFFFFF]", the delimiter can be a dash [-],
     * comma [,], colon [:] or period [.].  Additionally a [T] or space
     * may be used to seprate the day and hour fields.  The fractional
     * seconds ("FFFFFF") must begin with a period [.] if present.
     *
     * The time string can be "short" in which case the omitted values are
     * assumed to be zero (with the exception of DDD which is assumed to
     * be 1): "YYYY,DDD,HH" assumes MM, SS and FFFF are 0.  The year is
     * required, otherwise there wouldn't be much for a date.
     *
     * Ranges are checked for each value.
     *
     * Returns epoch time on success and HPTERROR on error.
     ***************************************************************************/
    hptime_t
    ms_seedtimestr2hptime (char *seedtimestr)
    {
      int fields;
      int year    = 0;
      int day     = 1;
      int hour    = 0;
      int min     = 0;
      int sec     = 0;
      float fusec = 0.0;
      int usec    = 0;

      fields = sscanf (seedtimestr, "%d%*[-,:.]%d%*[-,:.Tt ]%d%*[-,:.]%d%*[-,:.]%d%f",
                       &year, &day, &hour, &min, &sec, &fusec);

      /* Convert fractional seconds to microseconds */
      if (fusec != 0.0)
      {
        usec = (int)(fusec * 1000000.0 + 0.5);
      }

      if (fields < 1)
      {
        ms_log (2, "ms_seedtimestr2hptime(): Error converting time string: %s\n", seedtimestr);
        return HPTERROR;
      }

      if (year < 1800 || year > 5000)
      {
        ms_log (2, "ms_seedtimestr2hptime(): Error with year value: %d\n", year);
        return HPTERROR;
      }

      if (day < 1 || day > 366)
      {
        ms_log (2, "ms_seedtimestr2hptime(): Error with day value: %d\n", day);
        return HPTERROR;
      }

      if (hour < 0 || hour > 23)
      {
        ms_log (2, "ms_seedtimestr2hptime(): Error with hour value: %d\n", hour);
        return HPTERROR;
      }

      if (min < 0 || min > 59)
      {
        ms_log (2, "ms_seedtimestr2hptime(): Error with minute value: %d\n", min);
        return HPTERROR;
      }

      if (sec < 0 || sec > 60)
      {
        ms_log (2, "ms_seedtimestr2hptime(): Error with second value: %d\n", sec);
        return HPTERROR;
      }

      if (usec < 0 || usec > 999999)
      {
        ms_log (2, "ms_seedtimestr2hptime(): Error with fractional second value: %d\n", usec);
        return HPTERROR;
      }

      return ms_time2hptime_int (year, day, hour, min, sec, usec);
    } /* End of ms_seedtimestr2hptime() */

    请问这个方法的这个参数,用c#需要传进去什么格式的字符串,用c#如何表示啊?


    please verify my account

    2017年11月30日 3:45

全部回复

  • Hi,

    欢迎在MSDN论坛发帖。

    >>请问YYYY[,DDD,HH,MM,SS.FFFFFF]这个字符串格式用c#怎么表示啊

    这在C#中是没有办法表示的,首先YYYY这c# 就不支持,C#中支持的是小写的yyyy,所以经过测试,我把你的格式改写如下:

    yyyy [ddd,HH,MM,ss.FFFFFF ]

    你可以使用下面相关代码,进行测试。

                DateTime thisDate1 = new DateTime(2011, 6, 10);
                Console.WriteLine("Today is " + thisDate1.ToString("MMMM [dd], yyyy") + ".");
    
                Console.WriteLine("Today is " + thisDate1.ToString("yyyy [ddd,HH,MM,ss.FFFFFF ]") + ".");
                //YYYY[,DDD,HH,MM,SS.FFFFFF] 
    
                DateTimeOffset thisDate2 = new DateTimeOffset(2011, 6, 10, 15, 24, 16,
                                                              TimeSpan.Zero);
                Console.WriteLine("The current date and time: {0:MM[dd]/yy H:mm:ss zzz}",
                                   thisDate2);

    >>请问这个方法的这个参数,用c#需要传进去什么格式的字符串,用c#如何表示啊

    分析你提供的代码,函数的入参是一个char 类型的指针,可指针在C#中,是没有对应的类型。如果你要改写这个代码,用C#,我建议使用string 类型代替这个char 类型的指针。

    关于C# 支持的时间格式,你可以参考这边文档,里面有详细介绍。

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年11月30日 7:57
    版主