0

我如何发布一个功能来向用户显示某个人在网站上的最后一次访问?我从 Grails in Action 一书中写了一些方法,但它不能正常工作。

static String getNiceDate(Date date) {
    def now = new Date()
    def diff = Math.abs(now.time - date.time)
    final long second = 1000
    final long minute = second * 60
    final long hour = minute * 60
    final long day = hour * 24

    def niceTime = ""
    long calc = 0;
    calc = Math.floor(diff/day)
    if(calc) {
        niceTime += calc + " day" + (calc > 1 ? "s " : " ")
        diff %= day
    }

    calc = Math.floor(diff/hour)
    if(calc){
        niceTime += calc + " hour" + (calc > 1 ? "s " : " ")
        diff %= hour
    }

    calc = Math.floor(diff/minute)
    if(calc) {
        niceTime =+ calc + " minute" + (calc > 1 ? "s " : " ")
        diff %= minute
    }

    if(!niceTime) {
        niceTime = "Right now"
    } else {
        niceTime += (date.time > now.time) ? "from now" : "ago"
    }

    return niceTime

}

这段代码有什么问题?每次都显示 20 分钟前或 10 分钟前的内容,但用户的最后一次访问是昨天。

4

3 回答 3

1

试试这种方式:

/**
   * Returns differnce b/w new Date and old date as Map holding difference in years, weeks, days, hrs, mins & secs 
   */
  public static Map getDiffernceInDates(Date oldDate, Date newDate = new Date()) {
    Long difference = newDate.time - oldDate.time
    Map diffMap =[:]
    difference = difference / 1000
    diffMap.seconds = difference % 60
    difference = (difference - diffMap.seconds) / 60
    diffMap.minutes = difference % 60
    difference = (difference - diffMap.minutes) / 60
    diffMap.hours = difference % 24
    difference = (difference - diffMap.hours) / 24
    diffMap.years = (difference / 365).toInteger()
    if(diffMap.years)
       difference = (difference) % 365
    diffMap.days = difference % 7
    diffMap.weeks = (difference - diffMap.days) / 7  
    return diffMap
  }

下面给出了详细显示日期差异的标记库:

def timeDiffInDetail = {attrs ->
        Map diff = DateUtil.getDiffernceInDates(attrs.oldDate, attrs.newDate ?: new Date())
        String result =  diff.years ? diff.years + " years " : ""
        result += diff.weeks ? diff.weeks + " weeks " : ""
        result += diff.days ? diff.days + " days " : ""
        result += diff.hours ? diff.hours + " hours " : ""
        result += diff.minutes ? diff.minutes + " minutes ago" : ""
        if (result)
          out << result
        else
          out << " 0 minutes ago"
}
于 2012-07-18T10:47:44.060 回答
1

在注销函数中添加一个简单的行来记录该时刻的日期/时间。(使用new Date())。使用 java 中的SimpleDateFormat将其转换为您想要的格式,并将其作为新字段记录在数据库中(无论您存储用户信息的位置)。当用户重新登录并显示它时检索此信息!

于 2012-07-18T08:22:33.950 回答
0

我对以前的代码进行了一些更改,并且运行良好。如果有人需要,这是一个工作版本:

class LastVisitTagLib {

static namespace = "own"

def lastVisit = {attrs ->
    def date = attrs.date
    def niceDate = getNiceDate(date)
    out << niceDate
}

static String getNiceDate(Date date) {

    def now = new Date()

    def diff = Math.abs(now.getTime() - date.getTime())

    long second = 1000
    long minute = 1000 * 60
    long hour = minute * 60
    long day = hour * 24

    def niceTime = ""

    long calc = 0L;

    calc = Math.floor(diff / day)
    if (calc > 0) {
        niceTime += calc + " day" + (calc > 1 ? "s " : " ")
        diff = diff % day
    }

    calc = Math.floor(diff / hour)
    if (calc > 0) {
        niceTime += calc + " hour" + (calc > 1 ? "s " : " ")
        diff = diff % hour
    }

    calc = Math.floor(diff / minute)
    if (calc > 0) {
        niceTime += calc + " minute" + (calc > 1 ? "s " : " ")
        diff = diff % minute
    }

    if (niceTime.length() == 0) {
        niceTime = "Right now"
    } else {
        niceTime += (date.getTime() > now.getTime()) ? "from now" : "ago"
    }

    return niceTime

}

}

命名空间“拥有”的标签,如

感谢所有帮助过我的人

于 2012-07-18T17:07:21.550 回答