How do we convert UUID to date in perl

旧街凉风 提交于 2020-01-02 08:11:14

问题


I am very new to Perl language. How to convert the UUID to date format 2011-04-22 ?

For example, I have UUID like this

118ffe80-466b-11e1-b5a5-5732cf729524.

How to convert this to date format?


回答1:


The module UUID::Tiny has a method called time_of_UUID() that might help:

time_of_UUID()

This function accepts UUIDs and UUID strings. 
Returns the time as a   floating point value, so use int() 
to get a time() compatible value.

Returns undef if the UUID is not version 1.

my $uuid_time = time_of_UUID($uuid);


The Timestamp section of RFC4122 can also complement the Wikipedia article about UUID.




回答2:


maybe this javascript API can help you to convert the UUID to date format,

this API convert UUID v1 to sec from 1970-01-01

UUID_to_Date



all of you need:

    get_time_int = function (uuid_str) {
        var uuid_arr = uuid_str.split( '-' ),
            time_str = [
                uuid_arr[ 2 ].substring( 1 ),
                uuid_arr[ 1 ],
                uuid_arr[ 0 ]
            ].join( '' );
        return parseInt( time_str, 16 );
    };

    get_date_obj = function (uuid_str) {
        var int_time = this.get_time_int( uuid_str ) - 122192928000000000,
            int_millisec = Math.floor( int_time / 10000 );
        return new Date( int_millisec );
    };


Example:

    var date_obj = get_date_obj(  '8bf1aeb8-6b5b-11e4-95c0-001dba68c1f2' );
    date_obj.toLocaleString( );// '11/13/2014, 9:06:06 PM'


来源:https://stackoverflow.com/questions/15127648/how-do-we-convert-uuid-to-date-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!