代码之家  ›  专栏  ›  技术社区  ›  Mister Epic

在流分析查询中生成反转时间戳

  •  7
  • Mister Epic  · 技术社区  · 7 年前

    我想将流分析作业的输出通过管道传输到表存储对于rowkey,我想使用反转的时间戳在C#中,我只需做以下操作:

    var invertedTimestamp = DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks;
    

    我试图写一个ASA的工作查询,但不知道如何得到类似的东西:

    SELECT
        CONCAT(deviceId, '_', sessionId) as DeviceSession,
        DATEDIFF ( ms , NOW(), MAX_DATE() ) //pseudocode
    INTO
        tablestorage
    FROM
        iothub
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   wolszakp    7 年前

    你可以用 User Defined Functions

    带功能:

    / There are 10000 ticks in a millisecond.
    // And there are 621355968000000000 ticks between 1st Jan 0001 and 1st Jan 1970.
    function tics_to_max() {
    	var convertToTicks = function (date) {
    		return ((date.getTime() * 10000) + 621355968000000000);
    	};
    
    	var max = new Date('9999-12-31T23:59:59.999Z');
    	var maxTics = convertToTicks(max);
    	var currentTics = convertToTicks(new Date());
    
    	return maxTics - currentTics;
    }

    您的查询可以如下所示:

    SELECT
      CONCAT(deviceId, '_', sessionId) as DeviceSession,
      udf.tics_to_max() AS TicsToMax
    INTO
      tablestorage
    FROM
      iothub
    
        2
  •  3
  •   Syed Danish Haider    7 年前

    试试下面的代码,它会起作用的。

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss", 
    Locale.ENGLISH);
    try 
        {
        String datestart="June 14 2018 16:02:37";
        cal.setTime(sdf.parse(datestart));// all done
         Calendar cal1=Calendar.getInstance();
        String formatted = sdf.format(cal1.getTime());//formatted date as i want
        cal1.setTime(sdf.parse(formatted));// all done
        long msDiff = cal1.getTimeInMillis() - cal.getTimeInMillis();
        long daysDiff = TimeUnit.MILLISECONDS.toDays(msDiff);
        Toast.makeText(this, "days="+daysDiff, Toast.LENGTH_SHORT).show();
        } 
        catch (ParseException e) 
        {
        e.printStackTrace();
        }