Jack Daniel
Jack Daniel

Reputation: 2467

Last Visit date function in grails

how I can release a function to show the users the last visit of some person in the site? I wrote some method from the book Grails in Action, but it's not working correctly.

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

}

what is wrong with this code? it's every time show something like 20 min ago or 10 min ago, but user's last visit is yesterday.

Upvotes: 0

Views: 375

Answers (3)

Jack Daniel
Jack Daniel

Reputation: 2467

I did some changes in ,y previous code and it's working nice. this is a working version if somebody will need it:

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

}

}

namespace "own" for tag like

thanks for everybody who helped for me

Upvotes: 0

COD3BOY
COD3BOY

Reputation: 12092

Try it this way :

/**
   * 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
  }

The taglib to display the date difference in detail is given below :

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"
}

Upvotes: 1

srjit
srjit

Reputation: 526

Add a simple line in the logout function to record the date/time at that instant.( use new Date() ) . Convert it into the format you want using SimpleDateFormat in java and record this in the database (wherever you store the user's information) as a new field. Retrieve this information when the user logs back in and display it !

Upvotes: 1

Related Questions