蓓蓓's profileYBB的小站PhotosBlogLists Tools Help

Blog


    November 04

    日期/时间的存储与解析

        最近做的项目中有这样一个需求:将一个uint32的整数解析到一个结构体中去.
        uint32整数的"组装"方式为:
    time = (CLFS_time_t)(FileCreationHour << 11) | (CLFS_time_t)(FileCreationMinute << 5) | (CLFS_time_t)(FileCreationSecond);
    data = (CLFS_time_t)((FileCreationYear - 1980) << 9) | (CLFS_time_t)(FileCreationMonth << 5) | (CLFS_time_t)(FileCreationDay);
    uint32st_ctime = (data << 16) | (time & 0xFFFF);
        用图片表示的话就是:
    a(1090 * 83的图片so小...)
        结构体为:
    typedef struct
    {
      uint16                        year;  /* Year [1980..2100] */
      uint16                        month;  /* Month of year [1..12] */
      uint16                        day;  /* Day of month [1..31] */
      uint16                        hour;  /* Hour of day [0..23] */
      uint16                        minute;  /* Minute of hour [0..59] */
      uint16                        second;  /* Second of minute [0..59] */
      uint16                        day_of_week;  /* Day of the week [0..6] [Monday .. Sunday] */
    }time_julian_type;
        很明显,如果我想解析上面的uint32到下面的结构体中去,必须做与"组装"相反的工作:先解析出16bits 的data = uint32st_ctime >> 16,则year = data >> 9,month = (data >> 5) & 0xF,day = data & (1 << 6 -1),time与此类似.
        可以看出,上面的步骤手工计算的地方比较多,所以容易出错,而且复查起来也比较耗时间.那么,我们采取另外一种办法,直接把每个item的bit数列出来,这样比较直观,而且不会出错.
    typedef struct time_struct_type
    {
     uint16 year : 7;
     uint16 month : 4;
     uint16 day : 5;
     uint16 hour : 5;
     uint16 minute : 6;
     uint16 second : 5;
    }time_struct_t;
        这个结构体用到了bitset,一目了然.好,直接把uint32的值赋给它:*((uint32 *)&time_struct) = uint32st_ctime;
        OK,现在就可以把time_struct里的各项内容直接赋给time_julian_type结构体啦,任务完成 :)

    Comments (2)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    zw shaowrote:
    俺来踢个馆:)

    uint16 second : 5; 只能表示 [0..31]
    我想博主当时也为这个稍微伤了一下神,本来是要写成uint16 second : 6的,不得已削足适履了。大概博主的程序对时间的要求并不需要精确到秒吧。
    这个方法能凑效应该说是相当的巧合(当然也巧妙!),假如你的程序需要处理的时间跨度超过128年的话(顺祝博主的程序能够流芳千古^_^),那么year这个脚趾头就足够难缠了,即便只增长1个bit,改成uint16 year : 8,呵呵,博主可以试试看,继续再砍掉脚后跟second 1个bit也还是穿不下32bit的鞋,bit field的脾气是出了名的古怪,非不得已咱都是敬而远之哦。
    3 days ago
    Ericwrote:
    这个bitset一般用在什么地方?
    Mar. 24

    Trackbacks

    The trackback URL for this entry is:
    http://copygirl2005.spaces.live.com/blog/cns!4AB62DF15ACD0B5D!796.trak
    Weblogs that reference this entry
    • None