Re: PosgreSQL & NSTimestamp
Re: PosgreSQL & NSTimestamp
- Subject: Re: PosgreSQL & NSTimestamp
- From: LD <email@hidden>
- Date: Sat, 15 Oct 2005 11:58:52 +1000
Hi there,
On 15/10/2005, at 6:44 AM, Colin Shreffler wrote:
I'm using the PostgresPlugin to try to save NSTimestamp information
to my
PostgreSQL database.
The times continue to get chopped off, however and only the date
makes it
to the database.
Has anyone else had this same problem?
A couple of years ago when first starting with WO with Postgres.
What column type are you using?
http://www.postgresql.org/docs/8.0/static/datatype-datetime.html
It's worth noting: NSTimestamp does not maintain time zone information.
That will also drive you crazy until you realise that fact.
What to do: Make the timezone for everything (eomodel/database/
application/JVM) "GMT" and convert on the fly.
Here's something I dug up from when I was beginning WO a couple of
years ago -- so it might not be brilliant...
public class Application extends WOApplication {
/** @TypeInfo String */
protected final NSArray possibleTimeZones;
// Local TimeZone equals:
// - false --> GMT
// - true --> systemTimeZone
public static final boolean LOCAL_TZ = false;
public static void main(String argv[]) {
WOApplication.main(argv, Application.class);
}
public Application() {
super();
System.out.println("Welcome to " + this.name() + "!");
// set default TimeZone for java system
// WARNING: make sure that this is what you want!!
String tzname = (LOCAL_TZ ? NSTimeZone.systemTimeZone
().getDisplayName() : "GMT");
java.util.TimeZone tz = java.util.TimeZone.getTimeZone(tzname);
java.util.TimeZone.setDefault(tz);
NSTimeZone.setDefault(tz);
// create NSArray with known TimeZone names
NSArray temp1 = NSTimeZone.knownTimeZoneNames();
NSArray temp2;
try {
temp2 = temp1.sortedArrayUsingComparator
(NSComparator.AscendingStringComparator);
} catch (NSComparator.ComparisonException e) {
temp2 = temp1;
}
possibleTimeZones = temp2;
}
}
// Then I think I used a formatter to display/receive the right dates
for the user:
NSTimestampFormatter formatter = LDTimestamp.timestampFormatter("%d-%
b-%Y %H:%M", session.sessionTimeZone());
//
// LDTimestamp.java
// Project LDTimestampTest
//
// Created by Lachlan Deck on Fri Dec 13 2002
// Copyright (c) 2002 Lachlan Deck. All rights reserved.
//
import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import java.util.*;
import java.text.*;
public class LDTimestamp extends NSTimestamp {
public LDTimestamp() {
this(new NSTimestamp());
}
public LDTimestamp(NSTimestamp timestamp) {
super(timestamp);
}
public LDTimestamp(Date date) {
super(date);
}
public LDTimestamp(long time) {
this(new NSTimestamp(time));
}
public LDTimestamp(long time, NSTimestamp date) {
this(new NSTimestamp(time, date));
}
public LDTimestamp(long time, TimeZone tz) {
this(new NSTimestamp(time, tz));
}
public LDTimestamp(java.sql.Timestamp sqlTimestamp) {
this(new NSTimestamp(sqlTimestamp));
}
public LDTimestamp(int year, int month, int date, int hour, int
minute, int second, TimeZone tz) {
this(new NSTimestamp(year, month, date, hour, minute,
second, tz));
}
public LDTimestamp(NSTimestampFormatter formatter, String date)
throws java.text.ParseException {
this((NSTimestamp)formatter.parseObject(date));
}
// -------- LDTimestamp [Overridden] METHODS --------
public int compare(NSTimestamp ts) {
return (super.compare(ts));
}
public Object clone() {
return (new LDTimestamp(this));
}
public LDTimestamp getInstance(long offset) {
return (new LDTimestamp(offset, this));
}
public long getTime() { return (super.getTime()); }
public int secondsFromGMTForTimeZone(NSTimeZone tz) {
return (tz.secondsFromGMTForTimestamp(this));
}
public long timeIntervalSinceSystemTime() {
long sys = System.currentTimeMillis();
long me = this.getTime();
return (sys - me);
}
public long timeIntervalSinceNSTimestamp(NSTimestamp ts) {
return Math.abs(this.getTime() - ts.getTime());
}
public String toString() { return (super.toString()); }
public String toString(NSTimestampFormatter f) {
try {
return (String) (f.format((NSTimestamp)this));
} catch (Exception e) {
return null;
}
}
// -------- LDTimestamp CALENDAR METHODS --------
// get calendar time field value (as GMT)
public int get(int calendarType) { return (this.get
(calendarType, null)); }
// get calendar time field value (as tz)
public int get(int calendarType, NSTimeZone tz) {
Calendar cal = GetCal(this, tz);
return (cal.get(calendarType));
}
// returns Calendar.AM or Calendar.PM
public int antiOrPostMeridiem (NSTimeZone tz) { return (this.get
(Calendar.AM_PM, tz)); }
public int calendarDate (NSTimeZone tz) { return (this.get
(Calendar.DATE, tz)); }
public int calendarEra (NSTimeZone tz) { return (this.get
(Calendar.ERA, tz)); }
public int calendarYear (NSTimeZone tz) { return (this.get
(Calendar.YEAR, tz)); }
public int dayOfMonth (NSTimeZone tz) { return (this.get
(Calendar.DAY_OF_MONTH, tz)); }
public int dayOfWeek (NSTimeZone tz) { return (this.get
(Calendar.DAY_OF_WEEK, tz)); }
public int dayOfYear (NSTimeZone tz) { return (this.get
(Calendar.DAY_OF_YEAR, tz)); }
public int dstOffset (NSTimeZone tz) { return (this.get
(Calendar.DST_OFFSET, tz)); }
public int dstOffsetHours (NSTimeZone tz) { return
(this.dstOffset(tz) / (60 * 60 * 1000)); }
public int hourOfDay (NSTimeZone tz) { return (this.get
(Calendar.HOUR_OF_DAY, tz)); }
public int meridiemHour (NSTimeZone tz) { return (this.get
(Calendar.HOUR, tz)); }
public int minuteOfHour (NSTimeZone tz) { return (this.get
(Calendar.MINUTE, tz)); }
public int monthOfYear (NSTimeZone tz) { return (this.get
(Calendar.MONTH, tz)); }
public int secondOfMinute (NSTimeZone tz) { return (this.get
(Calendar.SECOND, tz)); }
public int weekOfMonth (NSTimeZone tz) { return (this.get
(Calendar.WEEK_OF_MONTH, tz)); }
public int weekOfYear (NSTimeZone tz) { return (this.get
(Calendar.WEEK_OF_YEAR, tz)); }
public int zoneOffset (NSTimeZone tz) { return (this.get
(Calendar.ZONE_OFFSET, tz)); }
public int zoneOffsetHours (NSTimeZone tz) { return
(this.zoneOffset(tz) / (60 * 60 * 1000)); }
// -------- STATIC METHODS --------
private static Calendar GetCal(Date date, NSTimeZone tz) {
String abbreviation = (tz == null ? "GMT" : tz.abbreviation());
Calendar cal = new GregorianCalendar
(java.util.TimeZone.getTimeZone(abbreviation));
cal.setTime(date);
return cal;
}
public static NSTimestampFormatter timestampFormatter(String
format) {
// uses default timezone from Application constructor (e.g.,
GMT)
return (new NSTimestampFormatter(format));
}
public static NSTimestampFormatter timestampFormatter(String
format, NSTimeZone tz) {
NSTimestampFormatter formatter = new NSTimestampFormatter
(format);
formatter.setDefaultFormatTimeZone(tz);
formatter.setDefaultParseTimeZone(tz);
return formatter;
}
public static String formatNSTimestamp(NSTimestampFormatter
formatter, NSTimestamp date) throws NullPointerException {
return (String) (formatter.format(date));
}
public static NSTimestamp parseNSTimestamp(NSTimestampFormatter
formatter, String date) throws ParseException {
return (NSTimestamp) (formatter.parseObject(date));
}
}
HTH.
I can send you the basic project if you like...
with regards,
--
LD
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden